move source bounds to BoundsMixin

This commit is contained in:
Laura Klünder 2017-05-26 21:25:54 +02:00
parent a60a333f42
commit b7333bba52
2 changed files with 30 additions and 19 deletions

View file

@ -1,6 +1,7 @@
from collections import OrderedDict
from django.db import models
from django.utils.translation import ugettext_lazy as _
EDITOR_FORM_MODELS = OrderedDict()
@ -38,3 +39,30 @@ class EditorFormMixin(SerializableMixin, models.Model):
@property
def title(self):
return self._meta.verbose_name+' '+str(self.id)
class BoundsMixin(SerializableMixin, models.Model):
bottom = models.DecimalField(_('bottom coordinate'), max_digits=6, decimal_places=2)
left = models.DecimalField(_('left coordinate'), max_digits=6, decimal_places=2)
top = models.DecimalField(_('top coordinate'), max_digits=6, decimal_places=2)
right = models.DecimalField(_('right coordinate'), max_digits=6, decimal_places=2)
class Meta:
abstract = True
@classmethod
def max_bounds(cls):
result = cls.objects.all().aggregate(models.Min('bottom'), models.Min('left'),
models.Max('top'), models.Max('right'))
return ((float(result['bottom__min']), float(result['left__min'])),
(float(result['top__max']), float(result['right__max'])))
def _serialize(self, section=True, **kwargs):
result = super()._serialize(**kwargs)
result['bounds'] = self.bounds
return result
@property
def bounds(self):
# noinspection PyTypeChecker
return (float(self.bottom), float(self.left)), (float(self.top), float(self.right))

View file

@ -1,18 +1,14 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
from c3nav.mapdata.models.base import EditorFormMixin
from c3nav.mapdata.models.base import BoundsMixin, EditorFormMixin
class Source(EditorFormMixin, models.Model):
class Source(EditorFormMixin, BoundsMixin, models.Model):
"""
A map source, images of levels that can be useful as backgrounds for the map editor
"""
name = models.SlugField(_('Name'), unique=True, max_length=50)
bottom = models.DecimalField(_('bottom coordinate'), max_digits=6, decimal_places=2)
left = models.DecimalField(_('left coordinate'), max_digits=6, decimal_places=2)
top = models.DecimalField(_('top coordinate'), max_digits=6, decimal_places=2)
right = models.DecimalField(_('right coordinate'), max_digits=6, decimal_places=2)
image = models.BinaryField(_('image data')) # todo migrate to better storage
class Meta:
@ -20,20 +16,7 @@ class Source(EditorFormMixin, models.Model):
verbose_name_plural = _('Sources')
default_related_name = 'sources'
@classmethod
def max_bounds(cls):
result = cls.objects.all().aggregate(models.Min('bottom'), models.Min('left'),
models.Max('top'), models.Max('right'))
return ((float(result['bottom__min']), float(result['left__min'])),
(float(result['top__max']), float(result['right__max'])))
def _serialize(self, section=True, **kwargs):
result = super()._serialize(**kwargs)
result['name'] = self.name
result['bounds'] = self.bounds
return result
@property
def bounds(self):
# noinspection PyTypeChecker
return (float(self.bottom), float(self.left)), (float(self.top), float(self.right))