reimplement GEOMETRY_MODELS etc.
This commit is contained in:
parent
9a1ce01fdb
commit
65ccf1d92f
11 changed files with 53 additions and 20 deletions
|
@ -80,6 +80,6 @@ def create_editor_form(mapitemtype):
|
||||||
|
|
||||||
|
|
||||||
def create_editor_forms():
|
def create_editor_forms():
|
||||||
from c3nav.mapdata.models.base import FEATURE_TYPES
|
from c3nav.mapdata.models.base import EDITOR_FORM_MODELS
|
||||||
for mapitemtype in FEATURE_TYPES.values():
|
for mapitemtype in EDITOR_FORM_MODELS.values():
|
||||||
create_editor_form(mapitemtype)
|
create_editor_form(mapitemtype)
|
||||||
|
|
|
@ -5,7 +5,7 @@ from django.shortcuts import get_object_or_404, redirect, render
|
||||||
|
|
||||||
from c3nav.access.apply import can_access, filter_queryset_by_access
|
from c3nav.access.apply import can_access, filter_queryset_by_access
|
||||||
from c3nav.mapdata.models import AreaLocation, Section
|
from c3nav.mapdata.models import AreaLocation, Section
|
||||||
from c3nav.mapdata.models.base import FEATURE_TYPES
|
from c3nav.mapdata.models.base import EDITOR_FORM_MODELS
|
||||||
|
|
||||||
|
|
||||||
def list_mapitemtypes(request, section):
|
def list_mapitemtypes(request, section):
|
||||||
|
@ -25,13 +25,13 @@ def list_mapitemtypes(request, section):
|
||||||
'title': mapitemtype._meta.verbose_name_plural,
|
'title': mapitemtype._meta.verbose_name_plural,
|
||||||
'has_section': hasattr(mapitemtype, 'section'),
|
'has_section': hasattr(mapitemtype, 'section'),
|
||||||
'count': get_item_count(mapitemtype),
|
'count': get_item_count(mapitemtype),
|
||||||
} for name, mapitemtype in FEATURE_TYPES.items()
|
} for name, mapitemtype in EDITOR_FORM_MODELS.items()
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
def list_mapitems(request, mapitem_type, section=None):
|
def list_mapitems(request, mapitem_type, section=None):
|
||||||
mapitemtype = FEATURE_TYPES.get(mapitem_type)
|
mapitemtype = EDITOR_FORM_MODELS.get(mapitem_type)
|
||||||
if mapitemtype is None:
|
if mapitemtype is None:
|
||||||
raise Http404('Unknown mapitemtype.')
|
raise Http404('Unknown mapitemtype.')
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ def list_mapitems(request, mapitem_type, section=None):
|
||||||
|
|
||||||
|
|
||||||
def edit_mapitem(request, mapitem_type, name=None):
|
def edit_mapitem(request, mapitem_type, name=None):
|
||||||
mapitemtype = FEATURE_TYPES.get(mapitem_type)
|
mapitemtype = EDITOR_FORM_MODELS.get(mapitem_type)
|
||||||
if mapitemtype is None:
|
if mapitemtype is None:
|
||||||
raise Http404()
|
raise Http404()
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
default_app_config = 'c3nav.mapdata.apps.MapdataConfig'
|
|
@ -11,7 +11,7 @@ from rest_framework.viewsets import ReadOnlyModelViewSet, ViewSet
|
||||||
from c3nav.access.apply import filter_arealocations_by_access, filter_queryset_by_access
|
from c3nav.access.apply import filter_arealocations_by_access, filter_queryset_by_access
|
||||||
from c3nav.mapdata.lastupdate import get_last_mapdata_update
|
from c3nav.mapdata.lastupdate import get_last_mapdata_update
|
||||||
from c3nav.mapdata.models import AreaLocation, LocationGroup, Source
|
from c3nav.mapdata.models import AreaLocation, LocationGroup, Source
|
||||||
from c3nav.mapdata.models.geometry.base import GEOMETRY_FEATURE_TYPES
|
from c3nav.mapdata.models.geometry.base import GEOMETRY_MODELS
|
||||||
from c3nav.mapdata.models.geometry.space import Stair
|
from c3nav.mapdata.models.geometry.space import Stair
|
||||||
from c3nav.mapdata.models.section import Section
|
from c3nav.mapdata.models.section import Section
|
||||||
from c3nav.mapdata.search import get_location
|
from c3nav.mapdata.search import get_location
|
||||||
|
@ -30,7 +30,7 @@ class GeometryTypeViewSet(ViewSet):
|
||||||
('name', name),
|
('name', name),
|
||||||
('title', str(mapitemtype._meta.verbose_name)),
|
('title', str(mapitemtype._meta.verbose_name)),
|
||||||
('title_plural', str(mapitemtype._meta.verbose_name_plural)),
|
('title_plural', str(mapitemtype._meta.verbose_name_plural)),
|
||||||
)) for name, mapitemtype in GEOMETRY_FEATURE_TYPES.items()
|
)) for name, mapitemtype in GEOMETRY_MODELS.items()
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ class GeometryViewSet(ViewSet):
|
||||||
"""
|
"""
|
||||||
def list(self, request):
|
def list(self, request):
|
||||||
types = set(request.GET.getlist('type'))
|
types = set(request.GET.getlist('type'))
|
||||||
valid_types = list(GEOMETRY_FEATURE_TYPES.keys())
|
valid_types = list(GEOMETRY_MODELS.keys())
|
||||||
if not types:
|
if not types:
|
||||||
types = valid_types
|
types = valid_types
|
||||||
else:
|
else:
|
||||||
|
@ -60,7 +60,7 @@ class GeometryViewSet(ViewSet):
|
||||||
def _list(self, request, types):
|
def _list(self, request, types):
|
||||||
results = []
|
results = []
|
||||||
for t in types:
|
for t in types:
|
||||||
mapitemtype = GEOMETRY_FEATURE_TYPES[t]
|
mapitemtype = GEOMETRY_MODELS[t]
|
||||||
queryset = mapitemtype.objects.all()
|
queryset = mapitemtype.objects.all()
|
||||||
queryset = filter_queryset_by_access(request, queryset)
|
queryset = filter_queryset_by_access(request, queryset)
|
||||||
queryset = queryset.order_by('id')
|
queryset = queryset.order_by('id')
|
||||||
|
|
31
src/c3nav/mapdata/apps.py
Normal file
31
src/c3nav/mapdata/apps.py
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
from django.apps import AppConfig
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class MapdataConfig(AppConfig):
|
||||||
|
name = 'c3nav.mapdata'
|
||||||
|
|
||||||
|
def _get_submodels(self, cls: type):
|
||||||
|
submodels = []
|
||||||
|
for subcls in cls.__subclasses__():
|
||||||
|
if issubclass(subcls, models.Model):
|
||||||
|
submodels.append(subcls)
|
||||||
|
submodels.extend(self._get_submodels(subcls))
|
||||||
|
return submodels
|
||||||
|
|
||||||
|
def ready(self):
|
||||||
|
from c3nav.mapdata.models.base import EditorFormMixin, EDITOR_FORM_MODELS
|
||||||
|
for cls in self._get_submodels(EditorFormMixin):
|
||||||
|
EDITOR_FORM_MODELS[cls.__name__] = cls
|
||||||
|
|
||||||
|
from c3nav.mapdata.models.geometry.base import GeometryMixin, GEOMETRY_MODELS
|
||||||
|
for cls in self._get_submodels(GeometryMixin):
|
||||||
|
GEOMETRY_MODELS[cls.__name__] = cls
|
||||||
|
|
||||||
|
from c3nav.mapdata.models.geometry.section import SectionGeometryMixin, SECTION_MODELS
|
||||||
|
for cls in self._get_submodels(SectionGeometryMixin):
|
||||||
|
SECTION_MODELS[cls.__name__] = cls
|
||||||
|
|
||||||
|
from c3nav.mapdata.models.geometry.space import SpaceGeometryMixin, SPACE_MODELS
|
||||||
|
for cls in self._get_submodels(SpaceGeometryMixin):
|
||||||
|
SPACE_MODELS[cls.__name__] = cls
|
|
@ -1,4 +1,5 @@
|
||||||
from .section import Section # noqa
|
from .section import Section # noqa
|
||||||
from .source import Source # noqa
|
from .source import Source # noqa
|
||||||
from .geometry.section import Space # noqa
|
from c3nav.mapdata.models.geometry.section import Building, Space, Hole, Door # noqa
|
||||||
|
from c3nav.mapdata.models.geometry.space import StuffedArea, Stair, Obstacle, LineObstacle # noqa
|
||||||
from .locations import AreaLocation, LocationGroup # noqa
|
from .locations import AreaLocation, LocationGroup # noqa
|
||||||
|
|
|
@ -2,10 +2,10 @@ from collections import OrderedDict
|
||||||
|
|
||||||
from django.utils.translation import get_language
|
from django.utils.translation import get_language
|
||||||
|
|
||||||
FEATURE_TYPES = OrderedDict()
|
EDITOR_FORM_MODELS = OrderedDict()
|
||||||
|
|
||||||
|
|
||||||
class EditorFormMixin():
|
class EditorFormMixin:
|
||||||
EditorForm = None
|
EditorForm = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
from c3nav.mapdata.models.geometry.section import Space # noqa
|
|
|
@ -5,7 +5,7 @@ from shapely.geometry import Point, mapping
|
||||||
from c3nav.mapdata.models.base import EditorFormMixin
|
from c3nav.mapdata.models.base import EditorFormMixin
|
||||||
from c3nav.mapdata.utils.json import format_geojson
|
from c3nav.mapdata.utils.json import format_geojson
|
||||||
|
|
||||||
GEOMETRY_FEATURE_TYPES = OrderedDict()
|
GEOMETRY_MODELS = OrderedDict()
|
||||||
|
|
||||||
|
|
||||||
class GeometryMixin(EditorFormMixin):
|
class GeometryMixin(EditorFormMixin):
|
||||||
|
@ -29,7 +29,7 @@ class GeometryMixin(EditorFormMixin):
|
||||||
))
|
))
|
||||||
|
|
||||||
def get_shadow_geojson(self):
|
def get_shadow_geojson(self):
|
||||||
return None
|
pass
|
||||||
|
|
||||||
def contains(self, x, y):
|
def contains(self, x, y):
|
||||||
return self.geometry.contains(Point(x, y))
|
return self.geometry.contains(Point(x, y))
|
||||||
|
|
|
@ -6,7 +6,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||||
from c3nav.mapdata.fields import GeometryField
|
from c3nav.mapdata.fields import GeometryField
|
||||||
from c3nav.mapdata.models.geometry.base import GeometryMixin
|
from c3nav.mapdata.models.geometry.base import GeometryMixin
|
||||||
|
|
||||||
LEVEL_FEATURE_TYPES = OrderedDict()
|
SECTION_MODELS = OrderedDict()
|
||||||
|
|
||||||
|
|
||||||
class SectionGeometryMixin(GeometryMixin):
|
class SectionGeometryMixin(GeometryMixin):
|
||||||
|
@ -33,8 +33,6 @@ class Space(SectionGeometryMixin, models.Model):
|
||||||
"""
|
"""
|
||||||
An accessible space. Shouldn't overlap.
|
An accessible space. Shouldn't overlap.
|
||||||
"""
|
"""
|
||||||
section = models.ForeignKey('mapdata.Section', on_delete=models.CASCADE, verbose_name=_('section'))
|
|
||||||
geometry = GeometryField('polygon')
|
|
||||||
|
|
||||||
CATEGORIES = (
|
CATEGORIES = (
|
||||||
('', _('normal')),
|
('', _('normal')),
|
||||||
|
@ -48,6 +46,8 @@ class Space(SectionGeometryMixin, models.Model):
|
||||||
('lowerr', _('lower')),
|
('lowerr', _('lower')),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
section = models.ForeignKey('mapdata.Section', on_delete=models.CASCADE, verbose_name=_('section'))
|
||||||
|
geometry = GeometryField('polygon')
|
||||||
public = models.BooleanField(verbose_name=_('public'), default=True)
|
public = models.BooleanField(verbose_name=_('public'), default=True)
|
||||||
category = models.CharField(verbose_name=_('category'), choices=CATEGORIES, max_length=16)
|
category = models.CharField(verbose_name=_('category'), choices=CATEGORIES, max_length=16)
|
||||||
layer = models.CharField(verbose_name=_('layer'), choices=LAYERS, max_length=16)
|
layer = models.CharField(verbose_name=_('layer'), choices=LAYERS, max_length=16)
|
||||||
|
|
|
@ -8,7 +8,7 @@ from c3nav.mapdata.fields import GeometryField
|
||||||
from c3nav.mapdata.models.geometry.base import GeometryMixin
|
from c3nav.mapdata.models.geometry.base import GeometryMixin
|
||||||
from c3nav.mapdata.utils.json import format_geojson
|
from c3nav.mapdata.utils.json import format_geojson
|
||||||
|
|
||||||
SPACE_FEATURE_TYPES = OrderedDict()
|
SPACE_MODELS = OrderedDict()
|
||||||
|
|
||||||
|
|
||||||
class SpaceGeometryMixin(GeometryMixin):
|
class SpaceGeometryMixin(GeometryMixin):
|
||||||
|
@ -105,5 +105,6 @@ class LineObstacle(SpaceGeometryMixin, models.Model):
|
||||||
|
|
||||||
def get_geojson_properties(self):
|
def get_geojson_properties(self):
|
||||||
result = super().get_geojson_properties()
|
result = super().get_geojson_properties()
|
||||||
|
# noinspection PyTypeChecker
|
||||||
result['width'] = float(self.width)
|
result['width'] = float(self.width)
|
||||||
return result
|
return result
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue