2016-10-12 15:25:00 +02:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
from django.db import models
|
2016-11-27 23:51:44 +01:00
|
|
|
from django.db.models.base import ModelBase
|
2016-10-12 15:25:00 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2016-12-07 16:11:33 +01:00
|
|
|
from c3nav.mapdata.lastupdate import set_last_mapdata_update
|
|
|
|
|
2016-11-27 23:51:44 +01:00
|
|
|
MAPITEM_TYPES = OrderedDict()
|
2016-10-12 15:25:00 +02:00
|
|
|
|
2016-11-27 23:51:44 +01:00
|
|
|
|
|
|
|
class MapItemMeta(ModelBase):
|
|
|
|
def __new__(mcs, name, bases, attrs):
|
|
|
|
cls = super().__new__(mcs, name, bases, attrs)
|
2016-11-28 16:55:34 +01:00
|
|
|
if not cls._meta.abstract and name != 'Source':
|
2016-11-27 23:51:44 +01:00
|
|
|
MAPITEM_TYPES[name.lower()] = cls
|
|
|
|
return cls
|
|
|
|
|
|
|
|
|
|
|
|
class MapItem(models.Model, metaclass=MapItemMeta):
|
2016-10-15 10:51:46 +02:00
|
|
|
name = models.SlugField(_('Name'), unique=True, max_length=50)
|
2016-10-12 15:25:00 +02:00
|
|
|
package = models.ForeignKey('mapdata.Package', on_delete=models.CASCADE, verbose_name=_('map package'))
|
|
|
|
|
2016-11-14 21:15:20 +01:00
|
|
|
EditorForm = None
|
|
|
|
|
2016-10-12 15:25:00 +02:00
|
|
|
@classmethod
|
|
|
|
def get_path_prefix(cls):
|
|
|
|
return cls._meta.default_related_name + '/'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_path_regex(cls):
|
|
|
|
return r'^' + cls.get_path_prefix()
|
|
|
|
|
|
|
|
def get_filename(self):
|
|
|
|
return self._meta.default_related_name + '/' + self.name + '.json'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def fromfile(cls, data, file_path):
|
|
|
|
kwargs = {}
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def tofile(self):
|
|
|
|
return OrderedDict()
|
|
|
|
|
2016-12-07 16:11:33 +01:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
with set_last_mapdata_update():
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2016-12-08 18:15:40 +01:00
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
with set_last_mapdata_update():
|
|
|
|
super().delete(*args, **kwargs)
|
|
|
|
|
2016-10-12 15:25:00 +02:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|