2017-05-11 22:40:48 +02:00
|
|
|
from django.apps import apps
|
2016-12-16 11:03:40 +01:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-05-12 13:52:01 +02:00
|
|
|
from django.utils.translation import get_language
|
2016-12-16 11:03:40 +01:00
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
from c3nav.mapdata.fields import JSONField
|
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-27 18:29:36 +02:00
|
|
|
slug = models.SlugField(_('slug'), unique=True, null=True, max_length=50)
|
2017-05-10 18:03:57 +02:00
|
|
|
|
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:
|
2017-05-27 18:29:36 +02:00
|
|
|
verbose_name = _('Location with Slug')
|
|
|
|
verbose_name_plural = _('Lucation with Slug')
|
2017-05-10 18:03:57 +02:00
|
|
|
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'))
|
2017-05-27 18:37:26 +02:00
|
|
|
color = models.CharField(null=True, blank=True, max_length=16, verbose_name=_('background color'))
|
2017-05-10 18:03:57 +02:00
|
|
|
public = models.BooleanField(verbose_name=_('public'), default=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2017-05-12 13:21:41 +02:00
|
|
|
def serialize(self, detailed=True, **kwargs):
|
|
|
|
result = super().serialize(**kwargs)
|
|
|
|
if not detailed:
|
|
|
|
for key in set(result.keys()) - {'type', 'id', 'slug', 'title', 'target'}:
|
|
|
|
result.pop(key)
|
|
|
|
return result
|
|
|
|
|
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()
|
2017-05-16 18:11:33 +02:00
|
|
|
if self.titles:
|
|
|
|
if lang in self.titles:
|
|
|
|
return self.titles[lang]
|
|
|
|
return next(iter(self.titles.values()))
|
|
|
|
if self.slug:
|
|
|
|
return self._meta.verbose_name + ' ' + self.slug
|
|
|
|
return super().title
|
2017-05-10 18:03:57 +02:00
|
|
|
|
2017-05-13 16:39:01 +02:00
|
|
|
def get_color(self):
|
|
|
|
if self.color:
|
|
|
|
return self.color
|
2017-05-13 20:21:45 +02:00
|
|
|
color_group = self.groups.filter(color__isnull=False).order_by('compiled_area', 'compiled_room').first()
|
2017-05-13 16:39:01 +02:00
|
|
|
if color_group:
|
|
|
|
return color_group.color
|
|
|
|
return None
|
|
|
|
|
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
|
|
|
|
|
2017-05-16 14:50:36 +02:00
|
|
|
@property
|
|
|
|
def title_for_forms(self):
|
|
|
|
attributes = []
|
|
|
|
if self.can_search:
|
|
|
|
attributes.append(_('search'))
|
|
|
|
if self.can_describe:
|
|
|
|
attributes.append(_('describe'))
|
|
|
|
if self.color:
|
|
|
|
attributes.append(_('color'))
|
|
|
|
if not attributes:
|
|
|
|
attributes.append(_('internal'))
|
|
|
|
if self.compiled_room:
|
|
|
|
attributes.append(_('comp. room'))
|
|
|
|
if self.compiled_area:
|
|
|
|
attributes.append(_('comp. area'))
|
|
|
|
return self.title + ' ('+', '.join(str(s) for s in attributes)+')'
|
|
|
|
|
|
|
|
|
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'
|