Fix a bunch of PEP8 violations

This commit is contained in:
Jan Tuomi
2017-01-19 17:55:59 +02:00
parent 16a3d03a32
commit d8dc064eb6
7 changed files with 208 additions and 139 deletions
+35 -20
View File
@@ -5,41 +5,44 @@ from datetime import datetime
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)
expire_date = models.DateTimeField(blank=True,null=True) # None means never expiring item
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")
@staticmethod
def get_create_template_url():
raise NotImplementedError("inheriting classes must implement get_create_template_url")
@classmethod
def create_from_dict(cls,d):
def create_from_dict(cls, d):
item = cls()
item.update_from_dict(d)
return item
def update_from_dict(self,d):
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():
for k, v in d.items():
try:
self.__setattr__(dmap[k],v)
self.__setattr__(dmap[k], v)
except KeyError:
pass
self.save()
def get_dict(self):
return {
'id': self.id,
@@ -51,7 +54,7 @@ class InfoItem(models.Model):
}
def delete(self):
# since generic foreignkeys suck. delete infoitems pointing here manually
# since generic foreign keys suck, delete info items pointing here manually
InfoInstance.objects.filter(item_id=self.id, item_type=ContentType.objects.get_for_model(self)).delete()
super().delete()
@@ -68,22 +71,26 @@ class InfoItem(models.Model):
class ABBInfoItem(InfoItem):
def get_template_url(self):
return "/static/html/abb.html"
@staticmethod
def get_create_template_url():
return "/static/html/abb_create.html"
class SossoInfoItem(InfoItem):
def get_template_url(self):
return "/static/html/sosso.html"
@staticmethod
def get_create_template_url():
return "/static/html/sosso_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
# get param to avoid angular from optimizing same template with different options
return "/static/html/generic_image.html?img={}".format(self.name)
@staticmethod
@@ -95,13 +102,16 @@ class ImageInfoItem(InfoItem):
d["options"] = {'img': self.img.url}
return d
class HslInfoItem(InfoItem):
def get_template_url(self):
return "/static/html/hsl.html"
@staticmethod
def get_create_template_url():
return "/static/html/hsl_create.html"
class ExternalImageInfoItem(InfoItem):
url = models.TextField()
@@ -118,12 +128,12 @@ class ExternalImageInfoItem(InfoItem):
return d
@classmethod
def create_from_dict(cls,d):
def create_from_dict(cls, d):
item = cls()
item.update_from_dict(d)
return item
def update_from_dict(self,d):
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")
@@ -134,23 +144,24 @@ class ExternalImageInfoItem(InfoItem):
'name': 'name',
'url': 'url',
}
for k,v in d.items():
for k, v in d.items():
try:
self.__setattr__(dmap[k],v)
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
# generic relation to somekind of InfoItem
duration = models.FloatField(default=15.0) # seconds
# generic relation to some kind of InfoItem
item_id = models.PositiveIntegerField()
item_type = models.ForeignKey(ContentType,on_delete=models.CASCADE)
item = GenericForeignKey('item_type','item_id')
item_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
item = GenericForeignKey('item_type', 'item_id')
@classmethod
def create_from_dict(cls,d):
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"]))
@@ -169,13 +180,15 @@ class InfoInstance(models.Model):
def get_dict(self):
return {
'id':self.id,
'id': self.id,
'item': self.item.get_dict(),
'duration': self.duration,
}
def __str__(self):
return "{}: {} ({}s)".format(self.rotation.name, self.item.name, self.duration)
class Rotation(models.Model):
name = models.CharField(max_length=255)
@@ -184,13 +197,14 @@ class Rotation(models.Model):
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))
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,
@@ -200,6 +214,7 @@ class Rotation(models.Model):
def __str__(self):
return self.name
class ImageUploadForm(forms.Form):
'''
Form used to handle imageuploads to
@@ -208,6 +223,6 @@ class ImageUploadForm(forms.Form):
name = forms.CharField()
image = forms.ImageField()
class HSLDataModel(models.Model):
data = models.TextField(default="", editable=False)