2017-10-27 15:05:41 +02:00
|
|
|
from collections import OrderedDict
|
2017-05-31 02:38:59 +02:00
|
|
|
from contextlib import suppress
|
2017-11-02 13:35:58 +01:00
|
|
|
from operator import attrgetter
|
2017-05-31 02:38:59 +02:00
|
|
|
|
2018-12-23 18:13:37 +01:00
|
|
|
from django.core.validators import RegexValidator
|
2016-12-16 11:03:40 +01:00
|
|
|
from django.db import models
|
2017-10-31 15:22:12 +01:00
|
|
|
from django.db.models import Prefetch
|
2017-11-02 13:35:58 +01:00
|
|
|
from django.urls import reverse
|
2017-10-28 13:31:12 +02:00
|
|
|
from django.utils.functional import cached_property
|
2017-10-28 20:30:27 +02:00
|
|
|
from django.utils.text import format_lazy
|
2016-12-16 11:03:40 +01:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-10-28 20:30:27 +02:00
|
|
|
from django.utils.translation import ungettext_lazy
|
2016-12-16 11:03:40 +01:00
|
|
|
|
2017-11-30 16:51:28 +01:00
|
|
|
from c3nav.mapdata.fields import I18nField
|
2018-12-10 19:35:59 +01:00
|
|
|
from c3nav.mapdata.grid import grid
|
2017-07-13 18:43:03 +02:00
|
|
|
from c3nav.mapdata.models.access import AccessRestrictionMixin
|
2017-07-10 13:01:35 +02:00
|
|
|
from c3nav.mapdata.models.base import SerializableMixin, TitledMixin
|
2017-06-22 19:27:51 +02:00
|
|
|
from c3nav.mapdata.utils.models import get_submodels
|
2017-05-11 19:36:49 +02:00
|
|
|
|
2016-12-16 11:03:40 +01:00
|
|
|
|
2017-06-18 18:42:30 +02:00
|
|
|
class LocationSlugManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
result = super().get_queryset()
|
|
|
|
if self.model == LocationSlug:
|
|
|
|
result = result.select_related(*(model._meta.default_related_name
|
2017-06-22 19:27:51 +02:00
|
|
|
for model in get_submodels(Location)+[LocationRedirect]))
|
2017-11-16 03:03:18 +01:00
|
|
|
return result
|
2017-06-18 18:42:30 +02:00
|
|
|
|
2017-07-19 15:20:12 +02:00
|
|
|
def select_related_target(self):
|
|
|
|
if self.model != LocationSlug:
|
|
|
|
raise TypeError
|
|
|
|
qs = self.get_queryset()
|
|
|
|
qs = qs.select_related('redirect__target', *('redirect__target__'+model._meta.default_related_name
|
|
|
|
for model in get_submodels(Location) + [LocationRedirect]))
|
|
|
|
return qs
|
|
|
|
|
2017-06-18 18:42:30 +02:00
|
|
|
|
2018-12-23 18:13:37 +01:00
|
|
|
validate_slug = RegexValidator(
|
|
|
|
r'^[a-z0-9]+(-[a-z0-9]+)*\Z',
|
|
|
|
# Translators: "letters" means latin letters: a-z and A-Z.
|
|
|
|
_('Enter a valid location slug consisting of lowercase letters, numbers or hyphens, '
|
|
|
|
'not starting or ending with hyphens or containing consecutive hyphens.'),
|
|
|
|
'invalid'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-05-11 22:40:48 +02:00
|
|
|
class LocationSlug(SerializableMixin, models.Model):
|
|
|
|
LOCATION_TYPE_CODES = {
|
2017-06-11 14:43:14 +02:00
|
|
|
'Level': 'l',
|
|
|
|
'Space': 's',
|
2017-05-11 22:40:48 +02:00
|
|
|
'Area': 'a',
|
2017-07-08 16:29:12 +02:00
|
|
|
'POI': 'p',
|
2017-05-11 22:40:48 +02:00
|
|
|
'LocationGroup': 'g'
|
|
|
|
}
|
|
|
|
LOCATION_TYPE_BY_CODE = {code: model_name for model_name, code in LOCATION_TYPE_CODES.items()}
|
2018-12-23 18:13:37 +01:00
|
|
|
slug = models.SlugField(_('Slug'), unique=True, null=True, blank=True, max_length=50, validators=[validate_slug])
|
2017-06-18 16:52:50 +02:00
|
|
|
|
2017-06-18 18:42:30 +02:00
|
|
|
objects = LocationSlugManager()
|
2017-05-10 18:03:57 +02:00
|
|
|
|
2017-12-24 13:23:19 +01:00
|
|
|
def get_child(self, instance=None):
|
2017-06-22 19:27:51 +02:00
|
|
|
for model in get_submodels(Location)+[LocationRedirect]:
|
2017-05-31 02:38:59 +02:00
|
|
|
with suppress(AttributeError):
|
2017-12-24 13:23:19 +01:00
|
|
|
return getattr(instance or self, model._meta.default_related_name)
|
2017-05-11 19:36:49 +02:00
|
|
|
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
|
|
|
|
|
2018-09-19 19:08:47 +02:00
|
|
|
def details_display(self, **kwargs):
|
|
|
|
result = super().details_display(**kwargs)
|
2017-11-30 01:10:49 +01:00
|
|
|
result['display'].insert(2, (_('Slug'), str(self.get_slug())))
|
2017-11-02 13:35:58 +01:00
|
|
|
return result
|
|
|
|
|
2017-10-28 13:31:12 +02:00
|
|
|
@cached_property
|
|
|
|
def order(self):
|
|
|
|
return (-1, 0)
|
|
|
|
|
2017-05-10 18:03:57 +02:00
|
|
|
class Meta:
|
2017-05-27 18:29:36 +02:00
|
|
|
verbose_name = _('Location with Slug')
|
2017-07-10 13:04:02 +02:00
|
|
|
verbose_name_plural = _('Location with Slug')
|
2017-05-10 18:03:57 +02:00
|
|
|
default_related_name = 'locationslugs'
|
|
|
|
|
|
|
|
|
2017-07-13 18:43:03 +02:00
|
|
|
class Location(LocationSlug, AccessRestrictionMixin, TitledMixin, models.Model):
|
2017-05-10 18:03:57 +02:00
|
|
|
can_search = models.BooleanField(default=True, verbose_name=_('can be searched'))
|
2017-11-01 15:55:59 +01:00
|
|
|
can_describe = models.BooleanField(default=True, verbose_name=_('can describe'))
|
2018-12-21 03:07:07 +01:00
|
|
|
icon = models.CharField(_('icon'), max_length=32, null=True, blank=True, help_text=_('any material icons name'))
|
2017-05-10 18:03:57 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2017-11-27 23:38:55 +01:00
|
|
|
def serialize(self, detailed=True, describe_only=False, **kwargs):
|
2017-06-22 19:53:25 +02:00
|
|
|
result = super().serialize(detailed=detailed, **kwargs)
|
2017-05-12 13:21:41 +02:00
|
|
|
if not detailed:
|
2018-12-21 03:07:07 +01:00
|
|
|
fields = ('id', 'type', 'slug', 'title', 'subtitle', 'icon', 'point', 'bounds', 'grid_square',
|
2018-12-10 19:40:25 +01:00
|
|
|
'locations', 'on_top_of')
|
2017-11-27 23:38:55 +01:00
|
|
|
result = OrderedDict(((name, result[name]) for name in fields if name in result))
|
2017-05-12 13:21:41 +02:00
|
|
|
return result
|
|
|
|
|
2017-05-11 19:36:49 +02:00
|
|
|
def _serialize(self, **kwargs):
|
|
|
|
result = super()._serialize(**kwargs)
|
2017-10-28 20:30:27 +02:00
|
|
|
result['subtitle'] = str(self.subtitle)
|
2018-12-21 03:24:58 +01:00
|
|
|
result['icon'] = self.get_icon()
|
2017-05-11 19:36:49 +02:00
|
|
|
result['can_search'] = self.can_search
|
|
|
|
result['can_describe'] = self.can_search
|
2017-05-10 18:03:57 +02:00
|
|
|
return result
|
|
|
|
|
2018-09-19 19:08:47 +02:00
|
|
|
def details_display(self, **kwargs):
|
|
|
|
result = super().details_display(**kwargs)
|
2017-11-02 13:35:58 +01:00
|
|
|
result['display'].extend([
|
2017-11-30 01:10:49 +01:00
|
|
|
(_('searchable'), _('Yes') if self.can_search else _('No')),
|
2018-12-21 03:07:07 +01:00
|
|
|
(_('can describe'), _('Yes') if self.can_describe else _('No')),
|
|
|
|
(_('icon'), self.get_icon()),
|
2017-11-02 13:35:58 +01: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
|
|
|
|
|
2017-10-27 13:47:12 +02:00
|
|
|
@property
|
|
|
|
def subtitle(self):
|
|
|
|
return ''
|
|
|
|
|
2018-12-10 19:06:38 +01:00
|
|
|
@property
|
2018-12-10 19:39:32 +01:00
|
|
|
def grid_square(self):
|
2018-12-10 19:06:38 +01:00
|
|
|
return None
|
|
|
|
|
2017-06-16 18:38:41 +02:00
|
|
|
def get_color(self, instance=None):
|
2018-12-21 19:06:29 +01:00
|
|
|
# dont filter in the query here so prefetch_related works
|
|
|
|
result = self.get_color_sorted(instance)
|
|
|
|
return None if result is None else result[1]
|
|
|
|
|
|
|
|
def get_color_sorted(self, instance=None):
|
2017-05-29 16:36:17 +02:00
|
|
|
# dont filter in the query here so prefetch_related works
|
2017-06-16 18:38:41 +02:00
|
|
|
if instance is None:
|
|
|
|
instance = self
|
2017-07-11 18:01:48 +02:00
|
|
|
for group in instance.groups.all():
|
2017-07-11 19:02:33 +02:00
|
|
|
if group.color and getattr(group.category, 'allow_'+self.__class__._meta.default_related_name):
|
2018-12-21 19:06:29 +01:00
|
|
|
return (0, group.category.priority, group.priority), group.color
|
2017-07-11 18:01:48 +02:00
|
|
|
return None
|
2017-05-13 16:39:01 +02:00
|
|
|
|
2018-12-21 03:07:07 +01:00
|
|
|
def get_icon(self):
|
|
|
|
return self.icon or 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-06-22 19:53:25 +02:00
|
|
|
def _serialize(self, detailed=True, **kwargs):
|
|
|
|
result = super()._serialize(detailed=detailed, **kwargs)
|
2018-12-10 19:35:59 +01:00
|
|
|
if grid.enabled:
|
2018-12-10 19:39:32 +01:00
|
|
|
grid_square = self.grid_square
|
|
|
|
if grid_square is not None:
|
2018-12-10 19:40:25 +01:00
|
|
|
result['grid_square'] = grid_square or None
|
2017-06-22 19:53:25 +02:00
|
|
|
if detailed:
|
2017-07-10 16:30:38 +02:00
|
|
|
groups = {}
|
|
|
|
for group in self.groups.all():
|
2017-07-11 18:53:43 +02:00
|
|
|
groups.setdefault(group.category, []).append(group.pk)
|
|
|
|
groups = {category.name: (items[0] if items else None) if category.single else items
|
|
|
|
for category, items in groups.items()
|
|
|
|
if getattr(category, 'allow_'+self.__class__._meta.default_related_name)}
|
2017-07-10 16:30:38 +02:00
|
|
|
result['groups'] = groups
|
2017-05-11 19:36:49 +02:00
|
|
|
return result
|
2016-12-16 11:03:40 +01:00
|
|
|
|
2018-09-19 19:08:47 +02:00
|
|
|
def details_display(self, **kwargs):
|
|
|
|
result = super().details_display(**kwargs)
|
2017-11-02 13:35:58 +01:00
|
|
|
|
|
|
|
groupcategories = {}
|
|
|
|
for group in self.groups.all():
|
|
|
|
groupcategories.setdefault(group.category, []).append(group)
|
|
|
|
|
2018-12-10 19:35:59 +01:00
|
|
|
if grid.enabled:
|
2018-12-10 19:39:32 +01:00
|
|
|
grid_square = self.grid_square
|
|
|
|
if grid_square is not None:
|
|
|
|
grid_square_title = (_('Grid Squares') if grid_square and '-' in grid_square else _('Grid Square'))
|
|
|
|
result['display'].insert(3, (grid_square_title, grid_square or None))
|
2018-12-10 19:32:28 +01:00
|
|
|
|
2017-11-02 13:35:58 +01:00
|
|
|
for category, groups in sorted(groupcategories.items(), key=lambda item: item[0].priority):
|
2017-11-22 20:50:34 +01:00
|
|
|
result['display'].insert(3, (
|
2017-12-22 21:31:44 +01:00
|
|
|
category.title if category.single else category.title_plural,
|
2017-11-22 20:50:34 +01:00
|
|
|
tuple({
|
|
|
|
'id': group.pk,
|
|
|
|
'slug': group.get_slug(),
|
|
|
|
'title': group.title,
|
|
|
|
'can_search': group.can_search,
|
|
|
|
} for group in sorted(groups, key=attrgetter('priority'), reverse=True))
|
|
|
|
))
|
2017-11-02 13:35:58 +01:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2017-10-27 13:47:12 +02:00
|
|
|
@property
|
|
|
|
def subtitle(self):
|
2017-12-14 21:44:14 +01:00
|
|
|
groups = tuple(self.groups.all() if 'groups' in getattr(self, '_prefetched_objects_cache', ()) else ())
|
2017-12-19 19:30:51 +01:00
|
|
|
groups = tuple(group for group in groups if group.can_describe)
|
2018-12-10 19:06:38 +01:00
|
|
|
subtitle = groups[0].title if groups else self.__class__._meta.verbose_name
|
2018-12-10 19:39:32 +01:00
|
|
|
if self.grid_square:
|
|
|
|
return '%s, %s' % (subtitle, self.grid_square)
|
2018-12-10 19:06:38 +01:00
|
|
|
return subtitle
|
2017-10-27 13:47:12 +02:00
|
|
|
|
2017-10-28 13:31:12 +02:00
|
|
|
@cached_property
|
|
|
|
def order(self):
|
|
|
|
groups = tuple(self.groups.all())
|
|
|
|
if not groups:
|
2017-12-11 01:51:58 +01:00
|
|
|
return (0, 0, 0)
|
2017-10-28 13:31:12 +02:00
|
|
|
return (0, groups[0].category.priority, groups[0].priority)
|
|
|
|
|
2018-12-21 03:07:07 +01:00
|
|
|
def get_icon(self):
|
|
|
|
icon = super().get_icon()
|
2018-12-21 03:24:58 +01:00
|
|
|
if icon:
|
|
|
|
return icon
|
|
|
|
for group in self.groups.all():
|
|
|
|
if group.icon and getattr(group.category, 'allow_' + self.__class__._meta.default_related_name):
|
|
|
|
return group.icon
|
|
|
|
return None
|
2018-12-21 03:07:07 +01:00
|
|
|
|
2016-12-16 11:03:40 +01:00
|
|
|
|
2017-11-30 16:51:28 +01:00
|
|
|
class LocationGroupCategory(SerializableMixin, models.Model):
|
2017-07-10 13:54:33 +02:00
|
|
|
name = models.SlugField(_('Name'), unique=True, max_length=50)
|
2017-07-10 18:55:35 +02:00
|
|
|
single = models.BooleanField(_('single selection'), default=False)
|
2017-11-30 16:51:28 +01:00
|
|
|
title = I18nField(_('Title'), plural_name='titles', fallback_any=True)
|
|
|
|
title_plural = I18nField(_('Title (Plural)'), plural_name='titles_plural', fallback_any=True)
|
2018-12-23 17:52:15 +01:00
|
|
|
help_text = I18nField(_('Help text'), plural_name='help_texts', fallback_any=True, fallback_value='')
|
2017-07-10 18:55:35 +02:00
|
|
|
allow_levels = models.BooleanField(_('allow levels'), db_index=True, default=True)
|
|
|
|
allow_spaces = models.BooleanField(_('allow spaces'), db_index=True, default=True)
|
|
|
|
allow_areas = models.BooleanField(_('allow areas'), db_index=True, default=True)
|
|
|
|
allow_pois = models.BooleanField(_('allow pois'), db_index=True, default=True)
|
|
|
|
priority = models.IntegerField(default=0, db_index=True)
|
2017-07-10 13:54:33 +02:00
|
|
|
|
2017-10-23 19:25:15 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.orig_priority = self.priority
|
|
|
|
|
2017-07-10 13:54:33 +02:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Location Group Category')
|
|
|
|
verbose_name_plural = _('Location Group Categories')
|
|
|
|
default_related_name = 'locationgroupcategories'
|
2017-07-10 19:16:35 +02:00
|
|
|
ordering = ('-priority', )
|
2017-07-10 13:54:33 +02:00
|
|
|
|
2017-11-30 16:51:28 +01:00
|
|
|
def _serialize(self, detailed=True, **kwargs):
|
|
|
|
result = super()._serialize(detailed=detailed, **kwargs)
|
2017-07-10 13:54:33 +02:00
|
|
|
result['name'] = self.name
|
2017-11-30 16:51:28 +01:00
|
|
|
if detailed:
|
|
|
|
result['titles'] = self.titles
|
|
|
|
result['title'] = self.title
|
2017-07-10 13:54:33 +02:00
|
|
|
return result
|
|
|
|
|
2017-10-23 19:25:15 +02:00
|
|
|
def register_changed_geometries(self):
|
|
|
|
from c3nav.mapdata.models.geometry.space import SpaceGeometryMixin
|
2017-10-28 13:04:46 +02:00
|
|
|
query = self.groups.all()
|
2017-10-23 19:25:15 +02:00
|
|
|
for model in get_submodels(SpecificLocation):
|
2017-10-28 13:04:46 +02:00
|
|
|
related_name = model._meta.default_related_name
|
|
|
|
subquery = model.objects.all()
|
2017-10-23 19:25:15 +02:00
|
|
|
if issubclass(model, SpaceGeometryMixin):
|
2017-10-28 13:04:46 +02:00
|
|
|
subquery = subquery.select_related('space')
|
|
|
|
query.prefetch_related(Prefetch('groups__'+related_name, subquery))
|
2017-10-23 19:25:15 +02:00
|
|
|
|
|
|
|
for group in query:
|
|
|
|
group.register_changed_geometries(do_query=False)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2017-12-14 19:42:33 +01:00
|
|
|
if self.pk and self.priority != self.orig_priority:
|
2017-10-23 19:25:15 +02:00
|
|
|
self.register_changed_geometries()
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2017-07-10 13:54:33 +02:00
|
|
|
|
2017-07-11 19:02:09 +02:00
|
|
|
class LocationGroupManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().select_related('category')
|
|
|
|
|
|
|
|
|
2017-06-21 12:47:28 +02:00
|
|
|
class LocationGroup(Location, models.Model):
|
2017-07-10 13:54:33 +02:00
|
|
|
category = models.ForeignKey(LocationGroupCategory, related_name='groups', on_delete=models.PROTECT,
|
2017-07-10 14:10:48 +02:00
|
|
|
verbose_name=_('Category'))
|
2017-07-10 19:16:35 +02:00
|
|
|
priority = models.IntegerField(default=0, db_index=True)
|
2017-09-16 14:19:01 +02:00
|
|
|
color = models.CharField(null=True, blank=True, max_length=32, verbose_name=_('background color'))
|
2016-12-16 11:03:40 +01:00
|
|
|
|
2017-07-11 19:02:09 +02:00
|
|
|
objects = LocationGroupManager()
|
|
|
|
|
2016-12-16 11:03:40 +01:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Location Group')
|
|
|
|
verbose_name_plural = _('Location Groups')
|
|
|
|
default_related_name = 'locationgroups'
|
2017-07-11 17:41:16 +02:00
|
|
|
ordering = ('-category__priority', '-priority')
|
2016-12-16 11:03:40 +01:00
|
|
|
|
2017-10-23 19:25:15 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.orig_priority = self.priority
|
2017-10-27 13:47:23 +02:00
|
|
|
self.orig_category_id = self.category_id
|
2017-10-23 19:25:15 +02:00
|
|
|
self.orig_color = self.color
|
|
|
|
|
2017-10-28 21:36:52 +02:00
|
|
|
def _serialize(self, simple_geometry=False, **kwargs):
|
|
|
|
result = super()._serialize(simple_geometry=simple_geometry, **kwargs)
|
2017-07-10 13:54:33 +02:00
|
|
|
result['category'] = self.category_id
|
2017-07-13 13:32:06 +02:00
|
|
|
result['color'] = self.color
|
2017-10-28 21:36:52 +02:00
|
|
|
if simple_geometry:
|
|
|
|
result['locations'] = tuple(obj.pk for obj in getattr(self, 'locations', ()))
|
2016-12-23 22:42:42 +01:00
|
|
|
return result
|
|
|
|
|
2018-09-19 19:08:47 +02:00
|
|
|
def details_display(self, editor_url=True, **kwargs):
|
|
|
|
result = super().details_display(**kwargs)
|
2017-11-30 01:10:49 +01:00
|
|
|
result['display'].insert(3, (_('Category'), self.category.title))
|
2017-11-02 13:35:58 +01:00
|
|
|
result['display'].extend([
|
2017-11-30 01:10:49 +01:00
|
|
|
(_('color'), self.color),
|
|
|
|
(_('priority'), self.priority),
|
2017-11-02 13:35:58 +01:00
|
|
|
])
|
2018-09-19 19:08:47 +02:00
|
|
|
if editor_url:
|
|
|
|
result['editor_url'] = reverse('editor.locationgroups.edit', kwargs={'pk': self.pk})
|
2017-11-02 13:35:58 +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'))
|
|
|
|
return self.title + ' ('+', '.join(str(s) for s in attributes)+')'
|
|
|
|
|
2017-10-23 19:25:15 +02:00
|
|
|
def register_changed_geometries(self, do_query=True):
|
|
|
|
from c3nav.mapdata.models.geometry.space import SpaceGeometryMixin
|
|
|
|
for model in get_submodels(SpecificLocation):
|
2017-10-28 13:04:46 +02:00
|
|
|
query = getattr(self, model._meta.default_related_name).all()
|
2017-10-23 19:25:15 +02:00
|
|
|
if do_query:
|
|
|
|
if issubclass(model, SpaceGeometryMixin):
|
|
|
|
query = query.select_related('space')
|
|
|
|
for obj in query:
|
|
|
|
obj.register_change(force=True)
|
|
|
|
|
2017-10-28 19:49:28 +02:00
|
|
|
@property
|
|
|
|
def subtitle(self):
|
2017-10-28 20:30:27 +02:00
|
|
|
result = self.category.title
|
|
|
|
if hasattr(self, 'locations'):
|
|
|
|
return format_lazy(_('{category_title}, {num_locations}'),
|
|
|
|
category_title=result,
|
|
|
|
num_locations=(ungettext_lazy('%(num)d location', '%(num)d locations', 'num') %
|
|
|
|
{'num': len(self.locations)}))
|
|
|
|
return result
|
2017-10-28 19:49:28 +02:00
|
|
|
|
2017-10-28 13:31:12 +02:00
|
|
|
@cached_property
|
|
|
|
def order(self):
|
2017-10-28 20:06:44 +02:00
|
|
|
return (1, self.category.priority, self.priority)
|
2017-10-28 13:31:12 +02:00
|
|
|
|
2017-10-23 19:25:15 +02:00
|
|
|
def save(self, *args, **kwargs):
|
2017-12-14 19:47:07 +01:00
|
|
|
if self.pk and (self.orig_color != self.color or
|
|
|
|
self.priority != self.orig_priority or
|
|
|
|
self.category_id != self.orig_category_id):
|
2017-10-23 19:25:15 +02:00
|
|
|
self.register_changed_geometries()
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2017-12-23 03:19:52 +01:00
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
self.register_changed_geometries()
|
|
|
|
super().delete(*args, **kwargs)
|
|
|
|
|
2017-05-16 14:50:36 +02:00
|
|
|
|
2017-05-11 22:40:48 +02:00
|
|
|
class LocationRedirect(LocationSlug):
|
2017-07-10 13:54:33 +02:00
|
|
|
target = models.ForeignKey(LocationSlug, related_name='redirects', on_delete=models.CASCADE,
|
|
|
|
verbose_name=_('target'))
|
2017-05-11 22:40:48 +02:00
|
|
|
|
2017-12-17 03:24:12 +01:00
|
|
|
def _serialize(self, include_type=True, **kwargs):
|
|
|
|
result = super()._serialize(include_type=include_type, **kwargs)
|
2017-05-11 22:40:48 +02:00
|
|
|
if type(self.target) == LocationSlug:
|
|
|
|
result['target'] = self.target.get_child().slug
|
|
|
|
else:
|
|
|
|
result['target'] = self.target.slug
|
2017-12-17 03:24:12 +01:00
|
|
|
if include_type:
|
2017-05-11 22:40:48 +02:00
|
|
|
result['type'] = 'redirect'
|
|
|
|
result.pop('id')
|
|
|
|
return result
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
default_related_name = 'redirect'
|