introducing group hierarchy

This commit is contained in:
Laura Klünder 2019-12-21 18:09:59 +01:00
parent 289017de00
commit c47d937c1d
4 changed files with 30 additions and 8 deletions

View file

@ -122,10 +122,9 @@ class EditorViewSet(EditorViewSetMixin, ViewSet):
@staticmethod
def area_sorting_func(area):
groups = tuple(area.groups.all())
print(groups)
if not groups:
return (0, 0, 0)
return (1, groups[0].category.priority, groups[0].priority)
return (1, groups[0].category.priority, groups[0].hierarchy, groups[0].priority)
# noinspection PyPep8Naming
@action(detail=False, methods=['get'])
@ -165,7 +164,7 @@ class EditorViewSet(EditorViewSetMixin, ViewSet):
Q(access_restriction__isnull=True) | ~Column.q_for_request(request)
).only('geometry', 'space')),
Prefetch('spaces__groups', LocationGroup.objects.only(
'color', 'category', 'priority', 'category__priority', 'category__allow_spaces'
'color', 'category', 'priority', 'hierarchy', 'category__priority', 'category__allow_spaces'
)),
Prefetch('buildings', Building.objects.only('geometry', 'level')),
Prefetch('spaces__holes', Hole.objects.only('geometry', 'space')),
@ -228,7 +227,7 @@ class EditorViewSet(EditorViewSetMixin, ViewSet):
'geometry', 'level'
).prefetch_related(
Prefetch('groups', LocationGroup.objects.only(
'color', 'category', 'priority', 'category__priority', 'category__allow_spaces'
'color', 'category', 'priority', 'hierarchy', 'category__priority', 'category__allow_spaces'
).filter(color__isnull=False))
)
@ -287,8 +286,10 @@ class EditorViewSet(EditorViewSetMixin, ViewSet):
areas = space.areas.filter(Area.q_for_request(request)).only(
'geometry', 'space'
).prefetch_related(
Prefetch('groups', LocationGroup.objects.only(
'color', 'category', 'priority', 'category__priority', 'category__allow_areas'
Prefetch('groups', LocationGroup.objects.order_by(
'-category__priority', '-hierarchy', '-priority'
).only(
'color', 'category', 'priority', 'hierarchy', 'category__priority', 'category__allow_areas'
))
)
for area in areas:

View file

@ -267,7 +267,7 @@ class EditorFormBase(I18nModelFormMixin, ModelForm):
def create_editor_form(editor_model):
possible_fields = ['slug', 'name', 'title', 'title_plural', 'help_text', 'icon', 'join_edges', 'up_separate',
'walk', 'ordering', 'category', 'width', 'groups', 'color', 'priority', 'icon_name',
'walk', 'ordering', 'category', 'width', 'groups', 'color', 'priority', 'hierarchy', 'icon_name',
'base_altitude', 'waytype', 'access_restriction', 'height', 'default_height', 'door_height',
'outside', 'can_search', 'can_describe', 'geometry', 'single', 'altitude', 'short_label',
'origin_space', 'target_space', 'data', 'comment', 'slow_down_factor',

View file

@ -0,0 +1,18 @@
# Generated by Django 2.2.8 on 2019-12-21 16:24
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('mapdata', '0072_make_wifi_measurement_author_nullable'),
]
operations = [
migrations.AddField(
model_name='locationgroup',
name='hierarchy',
field=models.IntegerField(db_index=True, default=0, verbose_name='hierarchy'),
),
]

View file

@ -150,7 +150,7 @@ class Location(LocationSlug, AccessRestrictionMixin, TitledMixin, models.Model):
instance = self
for group in instance.groups.all():
if group.color and getattr(group.category, 'allow_'+self.__class__._meta.default_related_name):
return (0, group.category.priority, group.priority), group.color
return (0, group.category.priority, group.hierarchy, group.priority), group.color
return None
def get_icon(self):
@ -289,6 +289,7 @@ class LocationGroup(Location, models.Model):
category = models.ForeignKey(LocationGroupCategory, related_name='groups', on_delete=models.PROTECT,
verbose_name=_('Category'))
priority = models.IntegerField(default=0, db_index=True)
hierarchy = models.IntegerField(default=0, db_index=True, verbose_name=_('hierarchy'))
color = models.CharField(null=True, blank=True, max_length=32, verbose_name=_('background color'))
objects = LocationGroupManager()
@ -302,6 +303,7 @@ class LocationGroup(Location, models.Model):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.orig_priority = self.priority
self.orig_hierarchy = self.hierarchy
self.orig_category_id = self.category_id
self.orig_color = self.color
@ -364,6 +366,7 @@ class LocationGroup(Location, models.Model):
def save(self, *args, **kwargs):
if self.pk and (self.orig_color != self.color or
self.priority != self.orig_priority or
self.hierarchy != self.orig_hierarchy or
self.category_id != self.orig_category_id):
self.register_changed_geometries()
super().save(*args, **kwargs)