소스 검색

Import and Export extended

master
Dirk Alders 4 년 전
부모
커밋
c46a60ed88
4개의 변경된 파일199개의 추가작업 그리고 29개의 파일을 삭제
  1. 4
    1
      management/commands/_private.py
  2. 53
    17
      management/commands/export_userdata.py
  3. 125
    11
      management/commands/import_userdata.py
  4. 17
    0
      models.py

+ 4
- 1
management/commands/_private.py 파일 보기

@@ -1,4 +1,7 @@
1
-KEY_USERDATA_VERSION = 'version'
2 1
 KEY_USERDATA_FAVOURITE = 'favourite'
3 2
 KEY_USERDATA_TAGS = 'tag'
3
+KEY_USER_PROFILE = 'userprofile'
4
+KEY_THEME_BOTTOMBAR = 'bottombar'
5
+KEY_THEME_SETTINGS = 'settings'
4 6
 KEY_USERDATA_UPLOAD = 'upload'
7
+KEY_ACCESS_DATA = 'access_data'

+ 53
- 17
management/commands/export_userdata.py 파일 보기

@@ -1,11 +1,17 @@
1 1
 from django.contrib.auth.models import User
2 2
 from django.core.management.base import BaseCommand
3 3
 import json
4
-from pygal.models import Tag
4
+from themes.models import GetSettings
5
+from themes.models import BottomBar
6
+from users.models import UserProfile
7
+from pygal.models import Item, Tag
5 8
 
6
-from ._private import KEY_USERDATA_VERSION
9
+from ._private import KEY_THEME_SETTINGS
10
+from ._private import KEY_THEME_BOTTOMBAR
11
+from ._private import KEY_USER_PROFILE
7 12
 from ._private import KEY_USERDATA_FAVOURITE
8 13
 from ._private import KEY_USERDATA_TAGS
14
+from ._private import KEY_ACCESS_DATA
9 15
 
10 16
 
11 17
 class Command(BaseCommand):
@@ -13,25 +19,55 @@ class Command(BaseCommand):
13 19
 
14 20
     def handle(self, *args, **options):
15 21
         data = {}
16
-        data[KEY_USERDATA_VERSION] = 1
22
+        #
23
+        # Access
24
+        #
25
+        if KEY_ACCESS_DATA not in data:
26
+            data[KEY_ACCESS_DATA] = {}
27
+        # public_access
28
+        data[KEY_ACCESS_DATA][''] = [item.rel_path for item in Item.objects.filter(public_access=True)]
29
+        # user_access
30
+        for u in User.objects.all():
31
+            data[KEY_ACCESS_DATA][u.username] = {}
32
+            data[KEY_ACCESS_DATA][u.username]['read_access'] = [item.rel_path for item in u.read_access.all()]
33
+            data[KEY_ACCESS_DATA][u.username]['modify_access'] = [item.rel_path for item in u.modify_access.all()]
34
+        #
35
+        # Favourites
36
+        #
37
+        if KEY_USERDATA_FAVOURITE not in data:
38
+            data[KEY_USERDATA_FAVOURITE] = {}
17 39
         for user in User.objects.all():
18 40
             favourites = user.favourite_of.all()
19 41
             if len(favourites) > 0:
20
-                if KEY_USERDATA_FAVOURITE not in data:
21
-                    data[KEY_USERDATA_FAVOURITE] = {}
22 42
                 data[KEY_USERDATA_FAVOURITE][user.username] = [i.rel_path for i in favourites]
43
+        #
44
+        # Tags
45
+        #
46
+        if KEY_USERDATA_TAGS not in data:
47
+            data[KEY_USERDATA_TAGS] = {}
23 48
         for tag in Tag.objects.all():
24
-            if KEY_USERDATA_TAGS not in data:
25
-                data[KEY_USERDATA_TAGS] = {}
26
-            if tag.item.rel_path not in data[KEY_USERDATA_TAGS]:
27
-                data[KEY_USERDATA_TAGS][tag.item.rel_path] = []
28
-            tagdata = {}
29
-            tagdata['text'] = tag.text
30
-            if tag.has_valid_coordinates:
31
-                tagdata['topleft_x'] = tag.topleft_x
32
-                tagdata['topleft_y'] = tag.topleft_y
33
-                tagdata['bottomright_x'] = tag.bottomright_x
34
-                tagdata['bottomright_y'] = tag.bottomright_y
35
-            data[KEY_USERDATA_TAGS][tag.item.rel_path].append(tagdata)
49
+            tag_key = tag.export_key()
50
+            if tag_key not in data[KEY_USERDATA_TAGS]:
51
+                data[KEY_USERDATA_TAGS][tag_key] = []
52
+            data[KEY_USERDATA_TAGS][tag.export_key()].append(tag.export_data())
53
+        #
54
+        # Settings
55
+        #
56
+        s = GetSettings()
57
+        data[KEY_THEME_SETTINGS] = s.export_data()
58
+        #
59
+        # Bottombar
60
+        #
61
+        if KEY_THEME_BOTTOMBAR not in data:
62
+            data[KEY_THEME_BOTTOMBAR] = []
63
+        for bb_entry in BottomBar.objects.all():
64
+            data[KEY_THEME_BOTTOMBAR].append(bb_entry.export_data())
65
+        #
66
+        # Userprofile
67
+        #
68
+        if KEY_USER_PROFILE not in data:
69
+            data[KEY_USER_PROFILE] = {}
70
+        for profile in UserProfile.objects.all():
71
+            data[KEY_USER_PROFILE][profile.export_key()] = profile.export_data()
36 72
 
37 73
         self.stdout.write(json.dumps(data, indent=4, sort_keys=True))

+ 125
- 11
management/commands/import_userdata.py 파일 보기

@@ -1,12 +1,17 @@
1 1
 from django.contrib.auth.models import User
2 2
 from django.core.management.base import BaseCommand
3 3
 import json
4
-from pygal.models import Item, Tag
4
+from pygal.models import Item, Tag, TagExist
5
+from themes.models import GetSettings, SettingExist, BottomBar, BottomBarExist
5 6
 import os
6 7
 
7
-from ._private import KEY_USERDATA_VERSION
8 8
 from ._private import KEY_USERDATA_FAVOURITE
9 9
 from ._private import KEY_USERDATA_TAGS
10
+from ._private import KEY_USER_PROFILE
11
+from ._private import KEY_THEME_SETTINGS
12
+from ._private import KEY_THEME_BOTTOMBAR
13
+from users.models import UserProfile, UserprofilerExist
14
+from pygal.management.commands._private import KEY_ACCESS_DATA
10 15
 
11 16
 
12 17
 class Command(BaseCommand):
@@ -25,10 +30,6 @@ class Command(BaseCommand):
25 30
             try:
26 31
                 with open(fn, 'r') as fh:
27 32
                     data = json.load(fh)
28
-                    try:
29
-                        version = data.pop(KEY_USERDATA_VERSION)
30
-                    except KeyError:
31
-                        version = 1
32 33
             except PermissionError:
33 34
                 self.error_skipped_file(fn, 'file permission')
34 35
             except json.decoder.JSONDecodeError:
@@ -42,8 +43,75 @@ class Command(BaseCommand):
42 43
                     cnt_success = 0
43 44
                     cnt_already_exist = 0
44 45
                     cnt_user_missing = 0
46
+                    missing_users = []
45 47
                     cnt_item_missing = 0
46
-                    if section == KEY_USERDATA_FAVOURITE:
48
+                    #
49
+                    # Access
50
+                    #
51
+                    if section == KEY_ACCESS_DATA:
52
+                        for username in data[section]:
53
+                            if username == '':
54
+                                # public_access
55
+                                for rel_path in data[section][username]:
56
+                                    cnt_all_datasets += 1
57
+                                    try:
58
+                                        item = Item.objects.get(rel_path=rel_path)
59
+                                    except User.DoesNotExist:
60
+                                        cnt_item_missing += 1
61
+                                    else:
62
+                                        if item.public_access:
63
+                                            cnt_already_exist += 1
64
+                                        else:
65
+                                            item.public_access = True
66
+                                            item.save()
67
+                                            cnt_success += 1
68
+                            else:
69
+                                try:
70
+                                    user = User.objects.get(username=username)
71
+                                except User.DoesNotExist:
72
+                                    user = None
73
+                                # read_access
74
+                                for rel_path in data[section][username]['read_access']:
75
+                                    cnt_all_datasets += 1
76
+                                    if user is None:
77
+                                        cnt_user_missing += 1
78
+                                        if username not in missing_users:
79
+                                            missing_users.append(username)
80
+                                    else:
81
+                                        try:
82
+                                            item = Item.objects.get(rel_path=rel_path)
83
+                                        except User.DoesNotExist:
84
+                                            cnt_item_missing += 1
85
+                                        else:
86
+                                            if user in item.read_access.all():
87
+                                                cnt_already_exist += 1
88
+                                            else:
89
+                                                item.read_access.add(user)
90
+                                                item.save()
91
+                                                cnt_success += 1
92
+                                # modify_access
93
+                                for rel_path in data[section][username]['modify_access']:
94
+                                    cnt_all_datasets += 1
95
+                                    if user is None:
96
+                                        cnt_user_missing += 1
97
+                                        if username not in missing_users:
98
+                                            missing_users.append(username)
99
+                                    else:
100
+                                        try:
101
+                                            item = Item.objects.get(rel_path=rel_path)
102
+                                        except User.DoesNotExist:
103
+                                            cnt_item_missing += 1
104
+                                        else:
105
+                                            if user in item.modify_access.all():
106
+                                                cnt_already_exist += 1
107
+                                            else:
108
+                                                item.modify_access.add(user)
109
+                                                item.save()
110
+                                                cnt_success += 1
111
+                    #
112
+                    # Favourites
113
+                    #
114
+                    elif section == KEY_USERDATA_FAVOURITE:
47 115
                         for username in data[section]:
48 116
                             try:
49 117
                                 user = User.objects.get(username=username)
@@ -53,6 +121,8 @@ class Command(BaseCommand):
53 121
                                 cnt_all_datasets += 1
54 122
                                 if user is None:
55 123
                                     cnt_user_missing += 1
124
+                                    if username not in missing_users:
125
+                                        missing_users.append(username)
56 126
                                 try:
57 127
                                     i = Item.objects.get(rel_path=rel_path)
58 128
                                 except Item.DoesNotExist:
@@ -60,9 +130,12 @@ class Command(BaseCommand):
60 130
                                 else:
61 131
                                     if user in i.favourite_of.all():
62 132
                                         cnt_already_exist += 1
63
-                                    else:
133
+                                    elif user is not None:
64 134
                                         cnt_success += 1
65 135
                                         i.favourite_of.add(user)
136
+                    #
137
+                    # Tags
138
+                    #
66 139
                     elif section == KEY_USERDATA_TAGS:
67 140
                         for rel_path in data[section]:
68 141
                             cnt_all_datasets += len(data[section][rel_path])
@@ -72,12 +145,53 @@ class Command(BaseCommand):
72 145
                                 cnt_item_missing += len(data[section][rel_path])
73 146
                             else:
74 147
                                 for tag_data in data[section][rel_path]:
75
-                                    if len(Tag.objects.filter(item=i, text=tag_data['text'])) > 0:
148
+                                    if TagExist(i, tag_data):
76 149
                                         cnt_already_exist += 1
77 150
                                     else:
78 151
                                         tag_data['item'] = i
79 152
                                         Tag(**tag_data).save()
80 153
                                         cnt_success += 1
154
+                    #
155
+                    # Settings
156
+                    #
157
+                    elif section == KEY_THEME_SETTINGS:
158
+                        cnt_all_datasets = 1
159
+                        if SettingExist(data[section]):
160
+                            cnt_already_exist += 1
161
+                        else:
162
+                            s = GetSettings()
163
+                            s.import_data(data[section])
164
+                            cnt_success += 1
165
+                    #
166
+                    # Bottombar
167
+                    #
168
+                    elif section == KEY_THEME_BOTTOMBAR:
169
+                        cnt_all_datasets = len(data[section])
170
+                        for bb_entry_import in data[section]:
171
+                            if BottomBarExist(bb_entry_import):
172
+                                cnt_already_exist += 1
173
+                            else:
174
+                                bb_entry = BottomBar()
175
+                                bb_entry.import_data(bb_entry_import)
176
+                                cnt_success += 1
177
+                    #
178
+                    # Userprofile
179
+                    #
180
+                    elif section == KEY_USER_PROFILE:
181
+                        cnt_all_datasets = len(data[section])
182
+                        for username in data[section]:
183
+                            if UserprofilerExist(username, data[section][username]):
184
+                                cnt_already_exist += 1
185
+                            else:
186
+                                try:
187
+                                    user = User.objects.get(username=username)
188
+                                except User.DoesNotExist:
189
+                                    cnt_user_missing += 1
190
+                                    missing_users.append(username)
191
+                                else:
192
+                                    profile = UserProfile(user=user)
193
+                                    profile.import_data(data[section][username])
194
+                                    cnt_success += 1
81 195
                     else:
82 196
                         self.stdout.write(self.style.ERROR('    Section unknown!'))
83 197
                     #
@@ -93,6 +207,6 @@ class Command(BaseCommand):
93 207
                         self.stdout.write(style('    %5d %s(s) successfully added.' % (cnt_success, section)))
94 208
                         self.stdout.write(style('    %5d %s(s) already set correctly.' % (cnt_already_exist, section)))
95 209
                         if cnt_user_missing > 0:
96
-                            self.stdout.write(self.style.ERROR('    %5d %s(s) not added, because the target user does not exist.' % (cnt_user_missing, section)))
210
+                            self.stdout.write(self.style.ERROR('    %5d %s(s) not added, because the target user(s) %s do(es) not exist.' % (cnt_user_missing, section, repr(missing_users))))
97 211
                         if cnt_item_missing > 0:
98
-                            self.stdout.write(self.style.ERROR('    %5d %s(s) not added, because the target item does not exist.' % (cnt_item_missing, section)))
212
+                            self.stdout.write(self.style.ERROR('    %5d %s(s) not added, because the target item does not exist. Try python manage.py rebuild_cache to create items, if you import to a scratch database.' % (cnt_item_missing, section)))

+ 17
- 0
models.py 파일 보기

@@ -447,6 +447,10 @@ class Item(models.Model):
447 447
         return 'Item: %s' % self.rel_path
448 448
 
449 449
 
450
+def TagExist(item, data):
451
+    return len(Tag.objects.filter(item=item, text=data['text'])) > 0
452
+
453
+
450 454
 class Tag(models.Model):
451 455
     item = models.ForeignKey(Item, on_delete=models.CASCADE)
452 456
     text = models.CharField(max_length=100)
@@ -468,3 +472,16 @@ class Tag(models.Model):
468 472
         """the url to the Django admin interface for the model instance"""
469 473
         info = (self._meta.app_label, self._meta.model_name)
470 474
         return reverse('admin:%s_%s_change' % info, args=(self.pk,))
475
+
476
+    def export_key(self):
477
+        return self.item.rel_path
478
+
479
+    def export_data(self):
480
+        rv = {}
481
+        rv['text'] = self.text
482
+        if self.has_valid_coordinates:
483
+            rv['topleft_x'] = self.topleft_x
484
+            rv['topleft_y'] = self.topleft_y
485
+            rv['bottomright_x'] = self.bottomright_x
486
+            rv['bottomright_y'] = self.bottomright_y
487
+        return rv

Loading…
취소
저장