move all background colors into location groups

This commit is contained in:
Laura Klünder 2017-07-13 13:32:06 +02:00
parent 158ed72b3b
commit 25989836a3
3 changed files with 62 additions and 6 deletions

View file

@ -77,7 +77,6 @@ abstract class Location {
public: bool public: bool
can_search: bool can_search: bool
can_describe: bool can_describe: bool
color: str
} }
abstract class SpecificLocation { abstract class SpecificLocation {
@ -95,7 +94,7 @@ class LocationGroupCategory {
} }
class LocationGroup { class LocationGroup {
color: str
} }
LocationGroup --|> Location LocationGroup --|> Location
SpecificLocation "*" --o "*" LocationGroup: groups SpecificLocation "*" --o "*" LocationGroup: groups

View file

@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-07-13 11:10
from __future__ import unicode_literals
from itertools import chain
from django.db import migrations
from django.utils import translation
def move_all_color_into_groups(apps, schema_editor):
LocationGroupCategory = apps.get_model('mapdata', 'LocationGroupCategory')
category = LocationGroupCategory.objects.get(name='groups')
colors = {}
for model_name in ('Level', 'Space', 'Area', 'POI'):
model = apps.get_model('mapdata', model_name)
for obj in model.objects.filter(color__isnull=False):
colors.setdefault(obj.color, []).append(obj)
from c3nav.mapdata.models import Location
for color, objects in colors.items():
titles = {lang: [] for lang in set(chain(*(obj.titles.keys() for obj in objects)))}
for obj in objects:
for lang in titles.keys():
translation.activate(lang)
titles[lang].append(Location(titles=obj.titles).title)
translation.deactivate_all()
titles = {lang: ', '.join(values) for lang, values in titles.items()}
group = category.groups.create(can_search=False, can_describe=False, color=color, titles=titles)
for obj in objects:
obj.groups.add(group)
class Migration(migrations.Migration):
dependencies = [
('mapdata', '0025_remove_area_stuffed'),
]
operations = [
migrations.RunPython(move_all_color_into_groups),
migrations.RemoveField(
model_name='area',
name='color',
),
migrations.RemoveField(
model_name='level',
name='color',
),
migrations.RemoveField(
model_name='poi',
name='color',
),
migrations.RemoveField(
model_name='space',
name='color',
),
]

View file

@ -53,7 +53,6 @@ class LocationSlug(SerializableMixin, models.Model):
class Location(LocationSlug, TitledMixin, models.Model): class Location(LocationSlug, TitledMixin, models.Model):
can_search = models.BooleanField(default=True, verbose_name=_('can be searched')) can_search = models.BooleanField(default=True, verbose_name=_('can be searched'))
can_describe = models.BooleanField(default=True, verbose_name=_('can be used to describe a position')) can_describe = models.BooleanField(default=True, verbose_name=_('can be used to describe a position'))
color = models.CharField(null=True, blank=True, max_length=16, verbose_name=_('background color'))
public = models.BooleanField(verbose_name=_('public'), default=True) public = models.BooleanField(verbose_name=_('public'), default=True)
class Meta: class Meta:
@ -76,7 +75,6 @@ class Location(LocationSlug, TitledMixin, models.Model):
result = super()._serialize(**kwargs) result = super()._serialize(**kwargs)
result['can_search'] = self.can_search result['can_search'] = self.can_search
result['can_describe'] = self.can_search result['can_describe'] = self.can_search
result['color'] = self.color
result['public'] = self.public result['public'] = self.public
return result return result
@ -118,8 +116,6 @@ class Location(LocationSlug, TitledMixin, models.Model):
return super().title return super().title
def get_color(self, instance=None): def get_color(self, instance=None):
if self.color:
return self.color
# dont filter in the query here so prefetch_related works # dont filter in the query here so prefetch_related works
if instance is None: if instance is None:
instance = self instance = self
@ -180,6 +176,7 @@ class LocationGroup(Location, models.Model):
category = models.ForeignKey(LocationGroupCategory, related_name='groups', on_delete=models.PROTECT, category = models.ForeignKey(LocationGroupCategory, related_name='groups', on_delete=models.PROTECT,
verbose_name=_('Category')) verbose_name=_('Category'))
priority = models.IntegerField(default=0, db_index=True) priority = models.IntegerField(default=0, db_index=True)
color = models.CharField(null=True, blank=True, max_length=16, verbose_name=_('background color'))
objects = LocationGroupManager() objects = LocationGroupManager()
@ -192,6 +189,7 @@ class LocationGroup(Location, models.Model):
def _serialize(self, **kwargs): def _serialize(self, **kwargs):
result = super()._serialize(**kwargs) result = super()._serialize(**kwargs)
result['category'] = self.category_id result['category'] = self.category_id
result['color'] = self.color
return result return result
@property @property