sori pojat...

This commit is contained in:
okalintu
2016-09-26 20:20:31 +03:00
parent 203f5e1b8d
commit 08f85d4211
24 changed files with 10619 additions and 21 deletions
+113 -12
View File
@@ -1,5 +1,7 @@
from django.db import models
from django import forms
from django.utils import timezone
from datetime import datetime
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
@@ -11,54 +13,120 @@ class InfoItem(models.Model):
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")
@staticmethod
def get_create_template_url():
raise NotImplementedError("inheriting classes must implement get_create_template_url")
@classmethod
def create_from_dict(cls,d):
item = cls()
item.update_from_dict(d)
return item
def update_from_dict(self,d):
try:
expire_date = d.pop('expire_date', None)
self.expire_date = datetime.strptime(expire_date, "%Y-%m-%d %H:%M:%S")
except:
pass
dmap = {
'name': 'name',
}
for k,v in d.items():
try:
self.__setattr__(dmap[k],v)
except KeyError:
pass
self.save()
def get_dict(self):
return {
'id': self.id,
'name': self.name,
'item_type': ContentType.objects.get_for_model(self).id,
'template_url': self.get_template_url(),
'edit_template_url': self.get_edit_template_url(),
'create_template_url': self.get_create_template_url(),
'options': {}
}
def delete(self):
# since generic foreignkeys suck. delete infoitems pointing here manually
InfoInstance.objects.filter(item_id=self.id, item_type=ContentType.objects.get_for_model(self)).delete()
super().delete()
@classmethod
def get_subclasses(cls):
for subclass in cls.__subclasses__():
yield from subclass.get_subclasses()
yield subclass
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"
@staticmethod
def get_create_template_url():
return "/static/html/abb_create.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)
return "/static/html/generic_image.html?img={}".format(self.name)
def get_edit_template_url(self):
return "/static/html/generic_image_edit.html"
@staticmethod
def get_create_template_url():
return "/static/html/generic_image_create.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"
@staticmethod
def get_create_template_url():
return "/static/html/generic_external_image_create.html"
def get_dict(self):
d = super().get_dict()
d["options"] = {'img': self.url}
return d
@classmethod
def create_from_dict(cls,d):
item = cls()
item.update_from_dict(d)
return item
def update_from_dict(self,d):
try:
expire_date = d.pop('expire_date', None)
self.expire_date = datetime.strptime(expire_date, "%Y-%m-%d %H:%M:%S")
except:
pass
dmap = {
'name': 'name',
'url': 'url',
}
for k,v in d.items():
try:
self.__setattr__(dmap[k],v)
except KeyError:
pass
self.save()
class InfoInstance(models.Model):
rotation = models.ForeignKey('Rotation', related_name='instances')
duration = models.FloatField(default=15.0) # seconds
@@ -67,8 +135,27 @@ class InfoInstance(models.Model):
item_type = models.ForeignKey(ContentType,on_delete=models.CASCADE)
item = GenericForeignKey('item_type','item_id')
@classmethod
def create_from_dict(cls,d):
try:
rotation = Rotation.objects.get(pk=int(d["rotation_id"]))
ct = ContentType.objects.get_for_id(int(d["item_type"]))
item = ct.get_object_for_this_type(pk=int(d["item_id"]))
duration = float(d["duration"])
except:
raise RuntimeError("invalid parameters supplied supplied")
try:
return cls.objects.create(
rotation=rotation,
item=item,
duration=duration
)
except:
raise RuntimeError("error while adding instance to db")
def get_dict(self):
return {
'id':self.id,
'item': self.item.get_dict(),
'duration': self.duration,
}
@@ -77,7 +164,7 @@ class InfoInstance(models.Model):
class Rotation(models.Model):
name = models.CharField(max_length=255)
def get_dict(self):
# exclude expired items from rotation (note: using tricky syntax to avoid excluding items with no expire_date)
now = timezone.now()
@@ -86,9 +173,15 @@ class Rotation(models.Model):
instance_list = list(map(lambda i:i.get_dict(), filtered))
return {
'id':self.id,
'name': self.name,
'instances': instance_list,
}
def get_list(self):
return {
'id':self.id,
'name':self.name,
}
def __str__(self):
return self.name
@@ -107,3 +200,11 @@ class ABBJob(models.Model):
def __str__(self):
return self.title
class ImageUploadForm(forms.Form):
'''
Form used to handle imageuploads to
infoscreen app
'''
name = forms.CharField()
image = forms.ImageField()