|
@@ -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)))
|