remodeled infoinstances and infoitems
This commit is contained in:
+57
-23
@@ -1,28 +1,71 @@
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
class InfoItem(models.Model):
|
||||
class __meta__:
|
||||
abstract = True
|
||||
name = models.CharField(max_length=255)
|
||||
template_url = models.CharField(max_length=512)
|
||||
expire_date = models.DateTimeField(blank=True,null=True) # None means never expiring item
|
||||
|
||||
def get_template_url(self):
|
||||
raise NotImplementedError("inheriting classes must implement get_template_url")
|
||||
def get_edit_template_url(self):
|
||||
raise NotImplementedError("inheriting classes must implement get_template_url")
|
||||
|
||||
def get_dict(self):
|
||||
# fetch options
|
||||
opts = {}
|
||||
for o in self.options.all():
|
||||
opts[o.key] = o.value
|
||||
# parse dict
|
||||
return {
|
||||
'name': self.name,
|
||||
'template_url': self.template_url,
|
||||
'options': opts
|
||||
'template_url': self.get_template_url(),
|
||||
'edit_template_url': self.get_edit_template_url(),
|
||||
'options': {}
|
||||
}
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class ABBInfoItem(InfoItem):
|
||||
def get_template_url(self):
|
||||
return "/static/html/abb.html"
|
||||
def get_edit_template_url(self):
|
||||
return "/static/html/generic_edit.html"
|
||||
|
||||
class ImageInfoItem(InfoItem):
|
||||
img = models.ImageField(upload_to="infoimages/")
|
||||
|
||||
def get_template_url(self):
|
||||
#get param to avoid angular from optimizing same template with different options
|
||||
return "/static/html/generic_image.html?img={}".format(self.name)
|
||||
|
||||
def get_edit_template_url(self):
|
||||
return "/static/html/generic_image_edit.html"
|
||||
|
||||
def get_dict(self):
|
||||
d = super().get_dict()
|
||||
d["options"] = {'img': self.img.url}
|
||||
return d
|
||||
|
||||
class ExternalImageInfoItem(InfoItem):
|
||||
url = models.TextField()
|
||||
|
||||
def get_template_url(self):
|
||||
return "/static/html/generic_image.html?img={}".format(self.name)
|
||||
|
||||
def get_edit_template_url(self):
|
||||
return "/static/html/generic_external_image_edit.html"
|
||||
|
||||
def get_dict(self):
|
||||
d = super().get_dict()
|
||||
d["options"] = {'img': self.url}
|
||||
return d
|
||||
|
||||
class InfoInstance(models.Model):
|
||||
rotation = models.ForeignKey('Rotation', related_name='instances')
|
||||
item = models.ForeignKey('InfoItem')
|
||||
duration = models.FloatField(default=15.0) # seconds
|
||||
# generic relation to somekind of InfoItem
|
||||
item_id = models.PositiveIntegerField()
|
||||
item_type = models.ForeignKey(ContentType,on_delete=models.CASCADE)
|
||||
item = GenericForeignKey('item_type','item_id')
|
||||
|
||||
def get_dict(self):
|
||||
return {
|
||||
@@ -32,24 +75,15 @@ class InfoInstance(models.Model):
|
||||
def __str__(self):
|
||||
return "{}: {} ({}s)".format(self.rotation.name, self.item.name, self.duration)
|
||||
|
||||
class InfoOption(models.Model):
|
||||
class __meta__:
|
||||
unique_together = ("item", "key")
|
||||
|
||||
item = models.ForeignKey('InfoItem', related_name='options')
|
||||
key = models.CharField(max_length=255)
|
||||
value = models.CharField(max_length=255)
|
||||
|
||||
def __str__(self):
|
||||
return "{}: {} -> {}".format(self.item.name, self.key, self.value)
|
||||
|
||||
class Rotation(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
|
||||
def get_dict(self):
|
||||
# exclude expired items from rotation (note: using filter would exclude items with no expide_date)
|
||||
instances = self.instances.exclude(item__expire_date__lt=timezone.now())
|
||||
instance_list = list(map(lambda i:i.get_dict(), instances))
|
||||
# exclude expired items from rotation (note: using tricky syntax to avoid excluding items with no expire_date)
|
||||
now = timezone.now()
|
||||
instances = self.instances.all()
|
||||
filtered = filter(lambda i: (i.item.expire_date or now) >= now, list(instances))
|
||||
instance_list = list(map(lambda i:i.get_dict(), filtered))
|
||||
|
||||
return {
|
||||
'name': self.name,
|
||||
|
||||
Reference in New Issue
Block a user