2017-05-08 21:55:45 +02:00
|
|
|
import numpy as np
|
2017-05-11 22:40:48 +02:00
|
|
|
from django.apps import apps
|
2016-12-19 16:54:11 +01:00
|
|
|
from django.core.cache import cache
|
2016-12-16 11:03:40 +01:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.functional import cached_property
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-05-11 09:06:19 +02:00
|
|
|
from django.utils.translation import get_language, ungettext_lazy
|
2016-12-16 11:03:40 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
from c3nav.mapdata.fields import JSONField
|
2016-12-19 16:54:11 +01:00
|
|
|
from c3nav.mapdata.lastupdate import get_last_mapdata_update
|
2017-05-11 22:40:48 +02:00
|
|
|
from c3nav.mapdata.models.base import EditorFormMixin, SerializableMixin
|
2016-12-16 11:03:40 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
LOCATION_MODELS = []
|
|
|
|
|
2016-12-16 11:03:40 +01:00
|
|
|
|
2017-05-11 22:40:48 +02:00
|
|
|
class LocationSlug(SerializableMixin, models.Model):
|
|
|
|
LOCATION_TYPE_CODES = {
|
|
|
|
'Section': 'se',
|
|
|
|
'Space': 'sp',
|
|
|
|
'Area': 'a',
|
|
|
|
'Point': 'p',
|
|
|
|
'LocationGroup': 'g'
|
|
|
|
}
|
|
|
|
LOCATION_TYPE_BY_CODE = {code: model_name for model_name, code in LOCATION_TYPE_CODES.items()}
|
2017-05-10 18:03:57 +02:00
|
|
|
slug = models.SlugField(_('name'), unique=True, null=True, max_length=50)
|
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
def get_child(self):
|
|
|
|
# todo: cache this
|
2017-05-11 22:40:48 +02:00
|
|
|
for model in LOCATION_MODELS+[LocationRedirect]:
|
2017-05-11 19:36:49 +02:00
|
|
|
try:
|
|
|
|
return getattr(self, model._meta.default_related_name)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|
2017-05-12 12:54:10 +02:00
|
|
|
def get_slug(self):
|
|
|
|
return self.slug
|
|
|
|
|
|
|
|
def _serialize(self, **kwargs):
|
|
|
|
result = super()._serialize(**kwargs)
|
|
|
|
result['slug'] = self.get_slug()
|
|
|
|
return result
|
|
|
|
|
2017-05-10 18:03:57 +02:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Slug for Location')
|
|
|
|
verbose_name_plural = _('Slugs für Locations')
|
|
|
|
default_related_name = 'locationslugs'
|
|
|
|
|
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
class Location(LocationSlug, EditorFormMixin, models.Model):
|
2017-05-10 18:03:57 +02:00
|
|
|
titles = JSONField(default={})
|
|
|
|
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'))
|
|
|
|
color = models.CharField(null=True, blank=True, max_length=16, verbose_name=_('background color'),
|
|
|
|
help_text=_('if set, has to be a valid color for svg images'))
|
|
|
|
public = models.BooleanField(verbose_name=_('public'), default=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
def _serialize(self, **kwargs):
|
|
|
|
result = super()._serialize(**kwargs)
|
|
|
|
result['titles'] = self.titles
|
2017-05-12 13:15:13 +02:00
|
|
|
result['title'] = self.title
|
2017-05-11 19:36:49 +02:00
|
|
|
result['can_search'] = self.can_search
|
|
|
|
result['can_describe'] = self.can_search
|
|
|
|
result['color'] = self.color
|
|
|
|
result['public'] = self.public
|
2017-05-10 18:03:57 +02:00
|
|
|
return result
|
|
|
|
|
2017-05-11 22:40:48 +02:00
|
|
|
def get_slug(self):
|
|
|
|
if self.slug is None:
|
|
|
|
code = self.LOCATION_TYPE_CODES.get(self.__class__.__name__)
|
|
|
|
if code is not None:
|
|
|
|
return code+':'+str(self.id)
|
|
|
|
return self.slug
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_by_slug(cls, slug, queryset=None):
|
|
|
|
if queryset is None:
|
|
|
|
queryset = LocationSlug.objects.all()
|
|
|
|
|
|
|
|
if ':' in slug:
|
|
|
|
code, pk = slug.split(':', 1)
|
|
|
|
model_name = cls.LOCATION_TYPE_BY_CODE.get(code)
|
|
|
|
if model_name is None or not pk.isdigit():
|
|
|
|
return None
|
|
|
|
|
|
|
|
model = apps.get_model('mapdata', model_name)
|
|
|
|
try:
|
|
|
|
location = model.objects.get(pk=pk)
|
|
|
|
except model.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if location.slug is not None:
|
|
|
|
return LocationRedirect(slug=slug, target=location)
|
|
|
|
|
|
|
|
return location
|
|
|
|
|
|
|
|
return queryset.filter(slug=slug).first()
|
|
|
|
|
2017-05-11 09:06:19 +02:00
|
|
|
@property
|
|
|
|
def title(self):
|
|
|
|
lang = get_language()
|
|
|
|
if lang in self.titles:
|
|
|
|
return self.titles[lang]
|
2017-05-12 13:10:48 +02:00
|
|
|
return (next(iter(self.titles.values())) if self.titles else
|
|
|
|
(self._meta.verbose_name+' '+(self.slug or str(self.id))))
|
2017-05-10 18:03:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SpecificLocation(Location, models.Model):
|
|
|
|
groups = models.ManyToManyField('mapdata.LocationGroup', verbose_name=_('Location Groups'), blank=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
def _serialize(self, **kwargs):
|
|
|
|
result = super()._serialize(**kwargs)
|
|
|
|
result['groups'] = list(self.groups.values_list('id', flat=True))
|
|
|
|
return result
|
2016-12-16 11:03:40 +01:00
|
|
|
|
|
|
|
|
2017-05-10 18:03:57 +02:00
|
|
|
class LocationGroup(Location, EditorFormMixin, models.Model):
|
2016-12-24 21:41:57 +01:00
|
|
|
compiled_room = models.BooleanField(default=False, verbose_name=_('is a compiled room'))
|
2017-05-10 21:24:31 +02:00
|
|
|
compiled_area = models.BooleanField(default=False, verbose_name=_('is a compiled area'))
|
2016-12-16 11:03:40 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Location Group')
|
|
|
|
verbose_name_plural = _('Location Groups')
|
|
|
|
default_related_name = 'locationgroups'
|
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
def _serialize(self, **kwargs):
|
|
|
|
result = super()._serialize(**kwargs)
|
|
|
|
result['compiled_room'] = self.compiled_room
|
|
|
|
result['compiled_area'] = self.compiled_area
|
2016-12-23 22:42:42 +01:00
|
|
|
return result
|
|
|
|
|
2016-12-16 11:03:40 +01:00
|
|
|
|
2017-05-11 22:40:48 +02:00
|
|
|
class LocationRedirect(LocationSlug):
|
|
|
|
target = models.ForeignKey(LocationSlug, verbose_name=_('target'), related_name='redirects')
|
|
|
|
|
|
|
|
def _serialize(self, with_type=True, **kwargs):
|
|
|
|
result = super()._serialize(with_type=with_type, **kwargs)
|
|
|
|
if type(self.target) == LocationSlug:
|
|
|
|
result['target'] = self.target.get_child().slug
|
|
|
|
else:
|
|
|
|
result['target'] = self.target.slug
|
|
|
|
if with_type:
|
|
|
|
result['type'] = 'redirect'
|
|
|
|
result.pop('id')
|
|
|
|
return result
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
default_related_name = 'redirect'
|