team-3/src/c3nav/mapdata/models/geometry/section.py

92 lines
2.7 KiB
Python
Raw Normal View History

from django.db import models
from django.utils.translation import ugettext_lazy as _
from c3nav.mapdata.fields import GeometryField
2017-05-08 16:40:22 +02:00
from c3nav.mapdata.models.geometry.base import GeometryMixin
2017-05-10 18:03:57 +02:00
from c3nav.mapdata.models.locations import SpecificLocation
2017-05-11 19:36:49 +02:00
SECTION_MODELS = []
2017-05-08 16:40:22 +02:00
class SectionGeometryMixin(GeometryMixin):
section = models.ForeignKey('mapdata.Section', on_delete=models.CASCADE, verbose_name=_('section'))
class Meta:
abstract = True
2017-05-11 19:36:49 +02:00
def get_geojson_properties(self) -> dict:
result = super().get_geojson_properties()
2017-06-11 12:29:12 +02:00
result['section'] = self.section_id
if hasattr(self, 'get_color'):
color = self.get_color()
if color:
result['color'] = color
2017-05-11 19:36:49 +02:00
return result
def _serialize(self, section=True, **kwargs):
result = super()._serialize(**kwargs)
if section:
result['section'] = self.section.id
return result
2017-05-08 16:40:22 +02:00
class Building(SectionGeometryMixin, models.Model):
"""
The outline of a building on a specific level
"""
geometry = GeometryField('polygon')
class Meta:
verbose_name = _('Building')
verbose_name_plural = _('Buildings')
default_related_name = 'buildings'
class Space(SpecificLocation, SectionGeometryMixin, models.Model):
"""
An accessible space. Shouldn't overlap with spaces on same section.
"""
CATEGORIES = (
('normal', _('normal')),
('stairs', _('stairs')),
('escalator', _('escalator')),
('elevator', _('elevator')),
)
2017-05-09 09:36:08 +02:00
geometry = GeometryField('polygon')
category = models.CharField(verbose_name=_('category'), choices=CATEGORIES, default='normal', max_length=16)
outside = models.BooleanField(default=False, verbose_name=_('only outside of building'))
class Meta:
2017-05-09 13:48:55 +02:00
verbose_name = _('Space')
verbose_name_plural = _('Spaces')
default_related_name = 'spaces'
2017-05-11 19:36:49 +02:00
def _serialize(self, space=True, **kwargs):
result = super()._serialize(**kwargs)
if space:
result['category'] = self.category
result['public'] = self.public
return result
2017-05-13 16:39:01 +02:00
def get_color(self):
2017-05-13 21:33:05 +02:00
color = super().get_color()
2017-05-13 16:39:01 +02:00
if not color:
2017-05-13 21:33:05 +02:00
color = {
'stairs': '#dddddd',
'escalator': '#bbbbbb',
'elevator': '#00ffff',
}.get(self.category)
2017-05-13 16:39:01 +02:00
return color
class Door(SectionGeometryMixin, models.Model):
"""
2017-05-09 13:48:55 +02:00
A connection between two spaces
"""
geometry = GeometryField('polygon')
class Meta:
verbose_name = _('Door')
verbose_name_plural = _('Doors')
default_related_name = 'doors'