Explorar el Código

Import and Export for Models added

master
Dirk Alders hace 4 años
padre
commit
d3b38d9e7a
Se han modificado 2 ficheros con 75 adiciones y 5 borrados
  1. 2
    5
      __init__.py
  2. 73
    0
      models.py

+ 2
- 5
__init__.py Ver fichero

@@ -16,11 +16,8 @@ default_theme = 'default'
16 16
 
17 17
 class ThemeSettings(object):
18 18
     def __init__(self):
19
-        from .models import Setting
20
-        s = Setting.objects.filter(id=1).first()
21
-        if s is None:
22
-            s = Setting(id=1)
23
-            s.save()
19
+        from .models import GetSettings
20
+        s = GetSettings()
24 21
         for attr_name in model_to_dict(s):
25 22
             attr = getattr(s, attr_name)
26 23
             if attr_name == 'page_theme' and attr == 'default':

+ 73
- 0
models.py Ver fichero

@@ -3,6 +3,25 @@ from . import available_themes, default_theme
3 3
 from PIL import Image
4 4
 
5 5
 
6
+def GetSettings():
7
+    s = Setting.objects.filter(id=1).first()
8
+    if s is None:
9
+        s = Setting(id=1)
10
+        s.save()
11
+    return s
12
+
13
+
14
+def SettingExist(data):
15
+    s = GetSettings()
16
+    if data['page_theme'] == s.page_theme and data['page_title'] == s.page_title and data['page_image'] == s.page_image.name:
17
+        if s.page_image.name:
18
+            return data['page_image_data'] == s.page_image.open().read().hex()
19
+        else:
20
+            return True
21
+    else:
22
+        return False
23
+
24
+
6 25
 class Setting(models.Model):
7 26
     page_image = models.ImageField(
8 27
         upload_to='theme',
@@ -24,6 +43,36 @@ class Setting(models.Model):
24 43
             img.thumbnail(output_size)
25 44
             img.save(self.page_image.path)
26 45
 
46
+    def export_data(self):
47
+        rv = {}
48
+        rv['page_image'] = self.page_image.name
49
+        rv['page_image_data'] = self.page_image.open().read().hex()
50
+        rv['page_theme'] = self.page_theme
51
+        rv['page_title'] = self.page_title
52
+        return rv
53
+
54
+    def import_data(self, data):
55
+        self.page_title = data['page_title']
56
+        self.page_theme = data['page_theme']
57
+        self.page_image.name = data['page_image']
58
+        if self.page_image.name:
59
+            with self.page_image.open('wb') as pi:
60
+                pi.write(bytes.fromhex(data['page_image_data']))
61
+        self.save()
62
+
63
+
64
+def BottomBarExist(data):
65
+    name = data.get('name', '')
66
+    icon_name = data.get('icon_name', '')
67
+    icon_data = data.get('icon_data')
68
+    url = data.get('url', '')
69
+    left = data.get('left')
70
+    sequence_number = data.get('sequence_number')
71
+    for bb_entry in BottomBar.objects.filter(name=name, icon=icon_name, url=url, left=left, sequence_number=sequence_number):
72
+        if icon_name == '' or icon_data == bb_entry.icon.open().read().hex():
73
+            return True
74
+    return False
75
+
27 76
 
28 77
 class BottomBar(models.Model):
29 78
     name = models.CharField(max_length=64, blank=True)
@@ -40,3 +89,27 @@ class BottomBar(models.Model):
40 89
                 output_size = (int(img.width * 40 / img.height), 40)
41 90
                 img.thumbnail(output_size)
42 91
                 img.save(self.icon.path)
92
+
93
+    def export_data(self):
94
+        rv = {}
95
+        rv['name'] = self.name
96
+        rv['icon_name'] = self.icon.name
97
+        if self.icon.name != '':
98
+            rv['icon_data'] = self.icon.open().read().hex()
99
+        else:
100
+            rv['icon_data'] = None
101
+        rv['url'] = self.url
102
+        rv['left'] = self.left
103
+        rv['sequence_number'] = self.sequence_number
104
+        return rv
105
+
106
+    def import_data(self, data):
107
+        self.name = data['name']
108
+        self.icon.name = data['icon_name']
109
+        if self.icon.name:
110
+            with self.icon.open('wb') as ih:
111
+                ih.write(bytes.fromhex(data['icon_data']))
112
+        self.url = data['url']
113
+        self.left = data['left']
114
+        self.sequence_number = data['sequence_number']
115
+        self.save()

Loading…
Cancelar
Guardar