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-12-12 13:21:09 +01:00
|
|
|
from django.utils.translation import get_language
|
2016-10-12 15:25:00 +02:00
|
|
|
|
2016-12-07 16:11:33 +01:00
|
|
|
from c3nav.mapdata.lastupdate import set_last_mapdata_update
|
|
|
|
|
2017-05-05 12:32:35 +02:00
|
|
|
FEATURE_TYPES = OrderedDict()
|
2016-10-12 15:25:00 +02:00
|
|
|
|
2016-11-27 23:51:44 +01:00
|
|
|
|
2017-05-05 12:32:35 +02:00
|
|
|
class FeatureBase(ModelBase):
|
2016-11-27 23:51:44 +01:00
|
|
|
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':
|
2017-05-05 12:32:35 +02:00
|
|
|
FEATURE_TYPES[name.lower()] = cls
|
2016-11-27 23:51:44 +01:00
|
|
|
return cls
|
|
|
|
|
|
|
|
|
2017-05-05 12:32:35 +02:00
|
|
|
class Feature(models.Model, metaclass=FeatureBase):
|
2016-11-14 21:15:20 +01:00
|
|
|
EditorForm = None
|
|
|
|
|
2016-12-12 13:21:09 +01:00
|
|
|
@property
|
|
|
|
def title(self):
|
|
|
|
if not hasattr(self, 'titles'):
|
|
|
|
return self.name
|
|
|
|
lang = get_language()
|
|
|
|
if lang in self.titles:
|
|
|
|
return self.titles[lang]
|
|
|
|
return next(iter(self.titles.values())) if self.titles else self.name
|
|
|
|
|
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
|
2017-05-05 12:32:35 +02:00
|
|
|
|
|
|
|
|