first stuff for custom positions
This commit is contained in:
parent
e144156b8a
commit
407e3ba06b
12 changed files with 655 additions and 267 deletions
|
@ -1,11 +1,14 @@
|
|||
import string
|
||||
from contextlib import suppress
|
||||
from decimal import Decimal
|
||||
from operator import attrgetter
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.validators import MaxValueValidator, MinValueValidator, RegexValidator
|
||||
from django.db import models
|
||||
from django.db.models import FieldDoesNotExist, Prefetch
|
||||
from django.urls import reverse
|
||||
from django.utils.crypto import get_random_string
|
||||
from django.utils.functional import cached_property
|
||||
from django.utils.text import format_lazy
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
@ -15,6 +18,7 @@ from c3nav.mapdata.fields import I18nField
|
|||
from c3nav.mapdata.grid import grid
|
||||
from c3nav.mapdata.models.access import AccessRestrictionMixin
|
||||
from c3nav.mapdata.models.base import SerializableMixin, TitledMixin
|
||||
from c3nav.mapdata.utils.fields import LocationById
|
||||
from c3nav.mapdata.utils.models import get_submodels
|
||||
|
||||
|
||||
|
@ -452,3 +456,52 @@ class LabelSettings(SerializableMixin, models.Model):
|
|||
verbose_name_plural = _('Label Settings')
|
||||
default_related_name = 'labelsettings'
|
||||
ordering = ('min_zoom', '-font_size')
|
||||
|
||||
|
||||
class DynamicLocation(SpecificLocation, models.Model):
|
||||
position_secret = models.CharField(_('position secret'), max_length=32, null=True, blank=True)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Dynamic location')
|
||||
verbose_name_plural = _('Dynamic locations')
|
||||
default_related_name = 'dynamic_locations'
|
||||
|
||||
"""
|
||||
def _serialize(self, **kwargs):
|
||||
result = super()._serialize(**kwargs)
|
||||
return result
|
||||
|
||||
@property
|
||||
def grid_square(self):
|
||||
return grid.get_squares_for_bounds(self.geometry.bounds) or ''
|
||||
|
||||
def details_display(self, editor_url=True, **kwargs):
|
||||
result = super().details_display(**kwargs)
|
||||
if editor_url:
|
||||
result['editor_url'] = reverse('editor.areas.edit', kwargs={'space': self.space_id, 'pk': self.pk})
|
||||
return result
|
||||
"""
|
||||
|
||||
|
||||
def get_position_secret():
|
||||
return get_random_string(32, string.ascii_letters+string.digits)
|
||||
|
||||
|
||||
def get_position_api_secret():
|
||||
return get_random_string(64, string.ascii_letters+string.digits)
|
||||
|
||||
|
||||
class Position(models.Model):
|
||||
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
name = models.CharField(_('name'), max_length=32)
|
||||
secret = models.CharField(_('secret'), unique=True, max_length=32, default=get_position_secret)
|
||||
last_location_update = models.DateTimeField(_('last location update'), null=True)
|
||||
location_id = models.CharField(_('location'), null=True, max_length=48)
|
||||
api_secret = models.CharField(_('api secret'), max_length=64, default=get_position_api_secret)
|
||||
|
||||
coordinates = LocationById()
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Dynamic position')
|
||||
verbose_name_plural = _('Dynamic position')
|
||||
default_related_name = 'dynamic_positions'
|
||||
|
|
|
@ -3,7 +3,6 @@ import string
|
|||
from django.conf import settings
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.cache import cache
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
from django.urls import reverse
|
||||
|
@ -15,7 +14,7 @@ from c3nav.mapdata.fields import I18nField
|
|||
from c3nav.mapdata.models.geometry.level import LevelGeometryMixin
|
||||
from c3nav.mapdata.models.geometry.space import SpaceGeometryMixin
|
||||
from c3nav.mapdata.models.locations import SpecificLocation
|
||||
from c3nav.mapdata.utils.locations import get_location_by_id_for_request
|
||||
from c3nav.mapdata.utils.fields import LocationById
|
||||
from c3nav.mapdata.utils.models import get_submodels
|
||||
from c3nav.site.tasks import send_report_notification
|
||||
|
||||
|
@ -24,39 +23,6 @@ def get_report_secret():
|
|||
return get_random_string(32, string.ascii_letters)
|
||||
|
||||
|
||||
class LocationById():
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.name = None
|
||||
self.cached_id = None
|
||||
self.cached_value = None
|
||||
|
||||
def __set_name__(self, owner, name):
|
||||
self.name = name
|
||||
|
||||
def __get__(self, instance, owner=None):
|
||||
value_id = getattr(instance, self.name+'_id')
|
||||
if value_id is None:
|
||||
self.cached_pk = None
|
||||
self.cached_value = None
|
||||
return None
|
||||
|
||||
if value_id == self.cached_id:
|
||||
return self.cached_value
|
||||
|
||||
value = get_location_by_id_for_request(value_id, getattr(instance, 'request', None))
|
||||
if value is None:
|
||||
raise ObjectDoesNotExist
|
||||
self.cached_id = value_id
|
||||
self.cached_value = value
|
||||
return value
|
||||
|
||||
def __set__(self, instance, value):
|
||||
self.cached_id = value.pk
|
||||
self.cached_value = value
|
||||
setattr(instance, self.name+'_id', value.pk)
|
||||
|
||||
|
||||
class Report(models.Model):
|
||||
CATEGORIES = (
|
||||
('location-issue', _('location issue')),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue