From 4a58c1afabb0711fde26f3d263fde2c35936e3cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Kl=C3=BCnder?= Date: Mon, 10 Jul 2017 13:54:33 +0200 Subject: [PATCH] create LocationGroupCategory --- src/c3nav/api/urls.py | 5 +- src/c3nav/mapdata/api.py | 7 ++- .../0019_location_group_category.py | 56 +++++++++++++++++++ src/c3nav/mapdata/models/locations.py | 22 +++++++- 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 src/c3nav/mapdata/migrations/0019_location_group_category.py diff --git a/src/c3nav/api/urls.py b/src/c3nav/api/urls.py index 8894b2ff..8b40adfb 100644 --- a/src/c3nav/api/urls.py +++ b/src/c3nav/api/urls.py @@ -9,8 +9,8 @@ from rest_framework.routers import SimpleRouter from c3nav.editor.api import ChangeSetViewSet, EditorViewSet from c3nav.mapdata.api import (AreaViewSet, BuildingViewSet, ColumnViewSet, DoorViewSet, HoleViewSet, LevelViewSet, - LineObstacleViewSet, LocationGroupViewSet, LocationViewSet, ObstacleViewSet, POIViewSet, - SourceViewSet, SpaceViewSet, StairViewSet) + LineObstacleViewSet, LocationGroupCategoryViewSet, LocationGroupViewSet, LocationViewSet, + ObstacleViewSet, POIViewSet, SourceViewSet, SpaceViewSet, StairViewSet) router = SimpleRouter() router.register(r'levels', LevelViewSet) @@ -27,6 +27,7 @@ router.register(r'pois', POIViewSet) router.register(r'sources', SourceViewSet) router.register(r'locations', LocationViewSet) +router.register(r'locationgroupcategories', LocationGroupCategoryViewSet) router.register(r'locationgroups', LocationGroupViewSet) router.register(r'editor', EditorViewSet, base_name='editor') diff --git a/src/c3nav/mapdata/api.py b/src/c3nav/mapdata/api.py index 285263cd..512be953 100644 --- a/src/c3nav/mapdata/api.py +++ b/src/c3nav/mapdata/api.py @@ -16,7 +16,8 @@ from c3nav.mapdata.models import Building, Door, Hole, LocationGroup, Source, Sp from c3nav.mapdata.models.geometry.level import LevelGeometryMixin from c3nav.mapdata.models.geometry.space import POI, Area, Column, LineObstacle, Obstacle, SpaceGeometryMixin, Stair from c3nav.mapdata.models.level import Level -from c3nav.mapdata.models.locations import Location, LocationRedirect, LocationSlug, SpecificLocation +from c3nav.mapdata.models.locations import (Location, LocationGroupCategory, LocationRedirect, LocationSlug, + SpecificLocation) from c3nav.mapdata.utils.models import get_submodels @@ -138,6 +139,10 @@ class POIViewSet(MapdataViewSet): queryset = POI.objects.all() +class LocationGroupCategoryViewSet(MapdataViewSet): + queryset = LocationGroupCategory.objects.all() + + class LocationGroupViewSet(MapdataViewSet): queryset = LocationGroup.objects.all() diff --git a/src/c3nav/mapdata/migrations/0019_location_group_category.py b/src/c3nav/mapdata/migrations/0019_location_group_category.py new file mode 100644 index 00000000..9665f37c --- /dev/null +++ b/src/c3nav/mapdata/migrations/0019_location_group_category.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.2 on 2017-07-10 11:39 +from __future__ import unicode_literals + +import c3nav.mapdata.fields +from django.db import migrations, models +import django.db.models.deletion + + +def create_location_group_category(apps, schema_editor): + LocationGroupCategory = apps.get_model('mapdata', 'LocationGroupCategory') + category = LocationGroupCategory.objects.create(name='groups', titles={ + 'en': 'Location Groups', + 'de': 'Ortgruppen', + }) + LocationGroup = apps.get_model('mapdata', 'LocationGroup') + LocationGroup.objects.update(category=category) + + +class Migration(migrations.Migration): + + dependencies = [ + ('mapdata', '0018_auto_20170708_1752'), + ] + + operations = [ + migrations.CreateModel( + name='LocationGroupCategory', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('titles', c3nav.mapdata.fields.JSONField(default={})), + ('name', models.SlugField(unique=True, verbose_name='Name')), + ], + options={ + 'verbose_name': 'Location Group Category', + 'verbose_name_plural': 'Location Group Categories', + 'default_related_name': 'locationgroupcategories', + }, + ), + migrations.AlterModelOptions( + name='locationslug', + options={'verbose_name': 'Location with Slug', 'verbose_name_plural': 'Location with Slug'}, + ), + migrations.AddField( + model_name='locationgroup', + name='category', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, related_name='groups', to='mapdata.LocationGroupCategory', verbose_name='Location Group Category'), + ), + migrations.RunPython(create_location_group_category, lambda: None), + migrations.AlterField( + model_name='locationgroup', + name='category', + field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='groups', + to='mapdata.LocationGroupCategory', verbose_name='Location Group Category'), + ), + ] diff --git a/src/c3nav/mapdata/models/locations.py b/src/c3nav/mapdata/models/locations.py index 678b6aa5..8c8de5f1 100644 --- a/src/c3nav/mapdata/models/locations.py +++ b/src/c3nav/mapdata/models/locations.py @@ -148,7 +148,25 @@ class SpecificLocation(Location, models.Model): return result +class LocationGroupCategory(TitledMixin, models.Model): + name = models.SlugField(_('Name'), unique=True, max_length=50) + + class Meta: + verbose_name = _('Location Group Category') + verbose_name_plural = _('Location Group Categories') + default_related_name = 'locationgroupcategories' + + def _serialize(self, **kwargs): + result = super()._serialize(**kwargs) + result['name'] = self.name + result.move_to_end('name', last=False) + result.move_to_end('id', last=False) + return result + + class LocationGroup(Location, models.Model): + category = models.ForeignKey(LocationGroupCategory, related_name='groups', on_delete=models.PROTECT, + verbose_name=_('Location Group Category')) compiled_room = models.BooleanField(default=False, verbose_name=_('is a compiled room')) compiled_area = models.BooleanField(default=False, verbose_name=_('is a compiled area')) @@ -159,6 +177,7 @@ class LocationGroup(Location, models.Model): def _serialize(self, **kwargs): result = super()._serialize(**kwargs) + result['category'] = self.category_id result['compiled_room'] = self.compiled_room result['compiled_area'] = self.compiled_area return result @@ -182,7 +201,8 @@ class LocationGroup(Location, models.Model): class LocationRedirect(LocationSlug): - target = models.ForeignKey(LocationSlug, verbose_name=_('target'), related_name='redirects') + target = models.ForeignKey(LocationSlug, related_name='redirects', on_delete=models.CASCADE, + verbose_name=_('target')) def _serialize(self, with_type=True, **kwargs): result = super()._serialize(with_type=with_type, **kwargs)