Django Library Themes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

models.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from django.db import models
  2. from . import available_themes, default_theme
  3. from PIL import Image
  4. def GetSettings():
  5. s = Setting.objects.filter(id=1).first()
  6. if s is None:
  7. s = Setting(id=1)
  8. s.save()
  9. return s
  10. def SettingExist(data):
  11. s = GetSettings()
  12. if data['page_theme'] == s.page_theme and data['page_title'] == s.page_title and data['page_image'] == s.page_image.name:
  13. if s.page_image.name:
  14. return data['page_image_data'] == s.page_image.open().read().hex()
  15. else:
  16. return True
  17. else:
  18. return False
  19. class Setting(models.Model):
  20. page_image = models.ImageField(
  21. upload_to='theme',
  22. default='theme/logo.png'
  23. )
  24. page_theme = models.CharField(
  25. max_length=64,
  26. choices=available_themes,
  27. default=default_theme
  28. )
  29. page_title = models.CharField(max_length=32, default='MyApp', blank=True)
  30. def save(self, *args, **kwargs):
  31. super().save(*args, **kwargs)
  32. img = Image.open(self.page_image.path)
  33. if img.height > 50:
  34. output_size = (int(img.width * 50 / img.height), 50)
  35. img.thumbnail(output_size)
  36. img.save(self.page_image.path)
  37. def export_data(self):
  38. rv = {}
  39. rv['page_image'] = self.page_image.name
  40. rv['page_image_data'] = self.page_image.open().read().hex()
  41. rv['page_theme'] = self.page_theme
  42. rv['page_title'] = self.page_title
  43. return rv
  44. def import_data(self, data):
  45. self.page_title = data['page_title']
  46. self.page_theme = data['page_theme']
  47. self.page_image.name = data['page_image']
  48. if self.page_image.name:
  49. with self.page_image.open('wb') as pi:
  50. pi.write(bytes.fromhex(data['page_image_data']))
  51. self.save()
  52. def BottomBarExist(data):
  53. name = data.get('name', '')
  54. icon_name = data.get('icon_name', '')
  55. icon_data = data.get('icon_data')
  56. url = data.get('url', '')
  57. left = data.get('left')
  58. sequence_number = data.get('sequence_number')
  59. for bb_entry in BottomBar.objects.filter(name=name, icon=icon_name, url=url, left=left, sequence_number=sequence_number):
  60. if icon_name == '' or icon_data == bb_entry.icon.open().read().hex():
  61. return True
  62. return False
  63. class BottomBar(models.Model):
  64. name = models.CharField(max_length=64, blank=True)
  65. icon = models.ImageField(upload_to='theme', blank=True)
  66. url = models.URLField()
  67. left = models.BooleanField()
  68. sequence_number = models.SmallIntegerField(choices=[(1, '1'), (2, '2'), (3, '3'), (4, '4'), (5, '5')])
  69. def save(self, *args, **kwargs):
  70. super().save(*args, **kwargs)
  71. if self.icon:
  72. img = Image.open(self.icon.path)
  73. if img.height > 40:
  74. output_size = (int(img.width * 40 / img.height), 40)
  75. img.thumbnail(output_size)
  76. img.save(self.icon.path)
  77. def export_data(self):
  78. rv = {}
  79. rv['name'] = self.name
  80. rv['icon_name'] = self.icon.name
  81. if self.icon.name != '':
  82. rv['icon_data'] = self.icon.open().read().hex()
  83. else:
  84. rv['icon_data'] = None
  85. rv['url'] = self.url
  86. rv['left'] = self.left
  87. rv['sequence_number'] = self.sequence_number
  88. return rv
  89. def import_data(self, data):
  90. self.name = data['name']
  91. self.icon.name = data['icon_name']
  92. if self.icon.name:
  93. with self.icon.open('wb') as ih:
  94. ih.write(bytes.fromhex(data['icon_data']))
  95. self.url = data['url']
  96. self.left = data['left']
  97. self.sequence_number = data['sequence_number']
  98. self.save()