123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- from django.db import models
- from . import available_themes, default_theme
- from PIL import Image
-
-
- def GetSettings():
- s = Setting.objects.filter(id=1).first()
- if s is None:
- s = Setting(id=1)
- s.save()
- return s
-
-
- def SettingExist(data):
- s = GetSettings()
- if data['page_theme'] == s.page_theme and data['page_title'] == s.page_title and data['page_image'] == s.page_image.name:
- if s.page_image.name:
- return data['page_image_data'] == s.page_image.open().read().hex()
- else:
- return True
- else:
- return False
-
-
- class Setting(models.Model):
- page_image = models.ImageField(
- upload_to='theme',
- default='theme/logo.png'
- )
- page_theme = models.CharField(
- max_length=64,
- choices=available_themes,
- default=default_theme
- )
- page_title = models.CharField(max_length=32, default='MyApp', blank=True)
-
- def save(self, *args, **kwargs):
- super().save(*args, **kwargs)
-
- img = Image.open(self.page_image.path)
- if img.height > 50:
- output_size = (int(img.width * 50 / img.height), 50)
- img.thumbnail(output_size)
- img.save(self.page_image.path)
-
- def export_data(self):
- rv = {}
- rv['page_image'] = self.page_image.name
- rv['page_image_data'] = self.page_image.open().read().hex()
- rv['page_theme'] = self.page_theme
- rv['page_title'] = self.page_title
- return rv
-
- def import_data(self, data):
- self.page_title = data['page_title']
- self.page_theme = data['page_theme']
- self.page_image.name = data['page_image']
- if self.page_image.name:
- with self.page_image.open('wb') as pi:
- pi.write(bytes.fromhex(data['page_image_data']))
- self.save()
-
-
- def BottomBarExist(data):
- name = data.get('name', '')
- icon_name = data.get('icon_name', '')
- icon_data = data.get('icon_data')
- url = data.get('url', '')
- left = data.get('left')
- sequence_number = data.get('sequence_number')
- for bb_entry in BottomBar.objects.filter(name=name, icon=icon_name, url=url, left=left, sequence_number=sequence_number):
- if icon_name == '' or icon_data == bb_entry.icon.open().read().hex():
- return True
- return False
-
-
- class BottomBar(models.Model):
- name = models.CharField(max_length=64, blank=True)
- icon = models.ImageField(upload_to='theme', blank=True)
- url = models.URLField()
- left = models.BooleanField()
- sequence_number = models.SmallIntegerField(choices=[(1, '1'), (2, '2'), (3, '3'), (4, '4'), (5, '5')])
-
- def save(self, *args, **kwargs):
- super().save(*args, **kwargs)
- if self.icon:
- img = Image.open(self.icon.path)
- if img.height > 40:
- output_size = (int(img.width * 40 / img.height), 40)
- img.thumbnail(output_size)
- img.save(self.icon.path)
-
- def export_data(self):
- rv = {}
- rv['name'] = self.name
- rv['icon_name'] = self.icon.name
- if self.icon.name != '':
- rv['icon_data'] = self.icon.open().read().hex()
- else:
- rv['icon_data'] = None
- rv['url'] = self.url
- rv['left'] = self.left
- rv['sequence_number'] = self.sequence_number
- return rv
-
- def import_data(self, data):
- self.name = data['name']
- self.icon.name = data['icon_name']
- if self.icon.name:
- with self.icon.open('wb') as ih:
- ih.write(bytes.fromhex(data['icon_data']))
- self.url = data['url']
- self.left = data['left']
- self.sequence_number = data['sequence_number']
- self.save()
|