Convert tag color to be an RGB value instead of a name value. Closes gh-issue #183.

This commit is contained in:
Roberto Rosario
2015-07-17 19:38:58 -04:00
parent 76fef8ab8d
commit d67ed4843a
8 changed files with 99 additions and 47 deletions

View File

@@ -1,40 +0,0 @@
from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _
COLOR_RED = 'red'
COLOR_BLUE = 'blu'
COLOR_MAGENTA = 'mag'
COLOR_CYAN = 'cya'
COLOR_YELLOW = 'yel'
COLOR_GREENYELLOW = 'gry'
COLOR_CORAL = 'crl'
COLOR_KHAKI = 'kki'
COLOR_LIGHTGREY = 'lig'
COLOR_ORANGE = 'org'
COLOR_CHOICES = (
(COLOR_BLUE, _('Blue')),
(COLOR_CYAN, _('Cyan')),
(COLOR_CORAL, _('Coral')),
(COLOR_GREENYELLOW, _('Green-Yellow')),
(COLOR_KHAKI, _('Khaki')),
(COLOR_LIGHTGREY, _('LightGrey')),
(COLOR_MAGENTA, _('Magenta')),
(COLOR_RED, _('Red')),
(COLOR_ORANGE, _('Orange')),
(COLOR_YELLOW, _('Yellow'))
)
COLOR_CODES = (
(COLOR_RED, 'red'),
(COLOR_BLUE, 'blue'),
(COLOR_MAGENTA, 'magenta'),
(COLOR_CYAN, 'cyan'),
(COLOR_YELLOW, 'yellow'),
(COLOR_GREENYELLOW, 'greenyellow '),
(COLOR_CORAL, 'coral'),
(COLOR_KHAKI, 'khaki'),
(COLOR_ORANGE, 'orange'),
(COLOR_LIGHTGREY, 'lightgrey'),
)

View File

@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
import colorful.fields
COLOR_BLUE = 'blu'
COLOR_CORAL = 'crl'
COLOR_CYAN = 'cya'
COLOR_GREENYELLOW = 'gry'
COLOR_KHAKI = 'kki'
COLOR_LIGHTGREY = 'lig'
COLOR_MAGENTA = 'mag'
COLOR_ORANGE = 'org'
COLOR_RED = 'red'
COLOR_YELLOW = 'yel'
RGB_VALUES = {
COLOR_BLUE: '#0000ff',
COLOR_CORAL: '#ff7f50',
COLOR_CYAN: '#00ffff',
COLOR_GREENYELLOW: '#adff2f',
COLOR_KHAKI: '#f0e68c',
COLOR_LIGHTGREY: '#d3d3d3',
COLOR_MAGENTA: '#ff00ff',
COLOR_ORANGE: '#ffa500',
COLOR_RED: '#ff0000',
COLOR_YELLOW: '#ffff00',
}
def convert_color_names_to_rgb(apps, schema_editor):
Tag = apps.get_model('tags', 'Tag')
for tag in Tag.objects.all():
tag.selection = RGB_VALUES[tag.color]
tag.save()
class Migration(migrations.Migration):
dependencies = [
('tags', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='tag',
name='selection',
field=colorful.fields.RGBColorField(default='#FFFFFF'),
preserve_default=False,
),
migrations.RunPython(convert_color_names_to_rgb),
]

View File

@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('tags', '0002_tag_selection'),
]
operations = [
migrations.RemoveField(
model_name='tag',
name='color',
),
]

View File

@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('tags', '0003_remove_tag_color'),
]
operations = [
migrations.RenameField(
model_name='tag',
old_name='selection',
new_name='color',
),
]

View File

@@ -4,15 +4,15 @@ from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from documents.models import Document
from colorful.fields import RGBColorField
from .literals import COLOR_CHOICES, COLOR_CODES
from documents.models import Document
@python_2_unicode_compatible
class Tag(models.Model):
label = models.CharField(db_index=True, max_length=128, unique=True, verbose_name=_('Label'))
color = models.CharField(choices=COLOR_CHOICES, max_length=3, verbose_name=_('Color'))
color = RGBColorField(verbose_name=_('Color'))
documents = models.ManyToManyField(Document, related_name='tags', verbose_name=_('Documents'))
class Meta:
@@ -21,6 +21,3 @@ class Tag(models.Model):
def __str__(self):
return self.label
def get_color_code(self):
return dict(COLOR_CODES)[self.color]

View File

@@ -22,5 +22,5 @@ def widget_single_tag(tag):
return mark_safe(
'''
<span class="label label-tag" style="background: {}">{}</span>
'''.format(tag.get_color_code(), escape(tag.label).replace(' ', '&nbsp;'))
'''.format(tag.color, escape(tag.label).replace(' ', '&nbsp;'))
)

View File

@@ -52,6 +52,7 @@ INSTALLED_APPS = (
# 3rd party
'actstream',
'autoadmin',
'colorful',
'compressor',
'corsheaders',
'djcelery',

View File

@@ -9,6 +9,7 @@ cssmin==0.2.0
django-activity-stream==0.6.0
django-autoadmin==1.0.1
django-celery==3.1.16
django-colorful==1.1.0
django-compressor==1.5
django-cors-headers==1.1.0
django-filetransfers==0.1.0