1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
import json
import os
import tempfile
import zipfile
from rest_framework import serializers
from zipfile import ZipFile
from django.apps import apps
from django.conf import settings
from django.core.serializers import deserialize
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Group, Permission
from . import models
from archaeological_operations.models import ActType
from ishtar_common.serializers_utils import generic_get_results, \
archive_serialization, generic_archive_files, SERIALIZATION_VERSION, \
get_model_from_filename
from archaeological_operations.serializers import operation_serialization, \
OPERATION_MODEL_LIST
from archaeological_context_records.serializers import cr_serialization, \
CR_MODEL_LIST
from archaeological_finds.serializers import find_serialization, \
FIND_MODEL_LIST
from archaeological_warehouse.serializers import warehouse_serialization, \
WAREHOUSE_MODEL_LIST
class PublicSerializer(serializers.BaseSerializer):
def to_representation(self, obj):
return obj.public_representation()
TYPE_MODEL_EXCLUDE = ["Area", "OperationTypeOld", "ProfileTypeSummary"]
def get_type_models():
return [ContentType, Permission, Group] + [
model for model in apps.get_models()
if isinstance(model(), models.GeneralType) and (
model.__name__ not in TYPE_MODEL_EXCLUDE)
]
def type_serialization(archive=False, return_empty_types=False,
archive_name=None, info=None):
result = generic_get_results(get_type_models(), "types")
return archive_serialization(result, archive_dir="types", archive=archive,
return_empty_types=return_empty_types,
archive_name=archive_name, info=info)
CONF_MODEL_LIST = [
models.IshtarSiteProfile, models.GlobalVar, models.CustomForm,
models.ExcludedField, models.JsonDataSection, models.JsonDataField,
models.CustomFormJsonField, models.ImporterModel,
models.DocumentTemplate, ActType
]
CONF_SERIALIZATION_INCLUDE = {ActType.__name__: ["associated_template"]}
def conf_serialization(archive=False, return_empty_types=False,
archive_name=None):
media_archive = None
if archive:
media_archive = generic_archive_files(CONF_MODEL_LIST)
result = generic_get_results(
CONF_MODEL_LIST, "common_configuration",
serialization_include=CONF_SERIALIZATION_INCLUDE)
full_archive = archive_serialization(
result, archive_dir="common_configuration", archive=archive,
return_empty_types=return_empty_types, archive_name=archive_name)
if not media_archive:
return full_archive
with ZipFile(full_archive, 'a') as current_zip:
current_zip.write(media_archive, arcname="media.zip")
return full_archive
IMPORT_MODEL_LIST = [
models.Regexp, models.ImporterModel, models.ImporterType,
models.ValueFormater, models.ImporterColumn,
models.FormaterType, models.ImporterDefault, models.ImporterDefaultValues,
models.ImportTarget, models.ImporterDefaultValues,
models.ImporterDuplicateField
]
def importer_serialization(archive=False, return_empty_types=False,
archive_name=None):
result = generic_get_results(IMPORT_MODEL_LIST, "common_imports")
full_archive = archive_serialization(
result, archive_dir="common_imports", archive=archive,
return_empty_types=return_empty_types, archive_name=archive_name)
return full_archive
GEO_MODEL_LIST = [
models.State, models.Department, models.Town, models.Area
]
def geo_serialization(archive=False, return_empty_types=False,
archive_name=None, no_geo=True):
result = generic_get_results(GEO_MODEL_LIST, "common_geo", no_geo=no_geo)
full_archive = archive_serialization(
result, archive_dir="common_geo", archive=archive,
return_empty_types=return_empty_types, archive_name=archive_name)
return full_archive
DIRECTORY_MODEL_LIST = [
models.Organization, models.Person, models.Author
]
def directory_serialization(archive=False, return_empty_types=False,
archive_name=None):
result = generic_get_results(DIRECTORY_MODEL_LIST, "common_directory")
full_archive = archive_serialization(
result, archive_dir="common_directory", archive=archive,
return_empty_types=return_empty_types, archive_name=archive_name)
return full_archive
def document_serialization(archive=False, return_empty_types=False,
archive_name=None, operation_queryset=None,
site_queryset=None, cr_queryset=None,
find_queryset=None, warehouse_queryset=None,
put_locks=False, lock_user=None):
result_queryset = {}
get_queryset_attr = None
if operation_queryset:
get_queryset_attr = {"operation_queryset": operation_queryset,
"get_queryset": True}
elif site_queryset:
get_queryset_attr = {"site_queryset": site_queryset,
"get_queryset": True}
elif cr_queryset:
get_queryset_attr = {"cr_queryset": cr_queryset,
"get_queryset": True}
elif find_queryset:
get_queryset_attr = {"find_queryset": find_queryset,
"get_queryset": True}
elif warehouse_queryset:
get_queryset_attr = {"warehouse_queryset": warehouse_queryset,
"get_queryset": True}
if get_queryset_attr:
queries = operation_serialization(**get_queryset_attr)
queries.update(cr_serialization(**get_queryset_attr))
queries.update(find_serialization(**get_queryset_attr))
queries.update(warehouse_serialization(**get_queryset_attr))
document_ids = set()
for model, attr in (
("Operation", "operations"),
("ArchaeologicalSite", "sites"),
("ContextRecord", "context_records"),
("Find", "finds"),
("Warehouse", "warehouses"),
("Container", "containers")):
values = list(queries[model].values_list("id", flat=True))
document_ids.update(
models.Document.objects.filter(
**{attr + "__id__in": values}).values_list(
"id", flat=True))
result_queryset["Document"] = models.Document.objects.filter(
id__in=document_ids)
result = generic_get_results([models.Document], "documents",
result_queryset=result_queryset)
if put_locks:
q = models.Document.objects
if result_queryset:
q = result_queryset["Document"]
q.update(locked=True, lock_user=lock_user)
media_archive = None
if archive:
media_archive = generic_archive_files([models.Document],
result_queryset=result_queryset)
full_archive = archive_serialization(
result, archive_dir="documents", archive=archive,
return_empty_types=return_empty_types, archive_name=archive_name)
if not media_archive:
return full_archive
has_media = "media.zip" in ZipFile(full_archive, 'r').namelist()
if not has_media:
with ZipFile(full_archive, 'a') as current_zip:
current_zip.write(media_archive, arcname="media.zip")
os.remove(media_archive)
return full_archive
with tempfile.TemporaryDirectory() as tmp_dir_name:
# extract the current archive
current_zip = ZipFile(full_archive, 'r')
name_list = current_zip.namelist()
for name in name_list:
current_zip.extract(name, tmp_dir_name)
current_zip.close()
# extract the media and recreate a media.zip
old_media_archive = ZipFile(
os.path.join(tmp_dir_name, "media.zip"), "r")
with ZipFile(media_archive, "a") as new_zip:
for name in old_media_archive.namelist():
new_zip.writestr(
name, old_media_archive.open(name).read())
# rewrite the archive
with ZipFile(full_archive + "_new", "w") as new_zip:
for name in name_list:
if name == "media.zip":
continue
new_zip.write(os.path.join(tmp_dir_name, name), arcname=name)
new_zip.write(media_archive, arcname="media.zip")
os.remove(media_archive)
os.remove(full_archive)
os.rename(full_archive + "_new", full_archive)
return full_archive
def full_serialization(operation_queryset=None, site_queryset=None,
cr_queryset=None, find_queryset=None,
warehouse_queryset=None, archive=True, no_geo=True,
info=None, export_types=True, export_conf=True,
export_importers=True, export_geo=True, export_dir=True,
export_docs=True, export_items=True, put_locks=False,
lock_user=None):
archive_name = None
if export_types:
# print("type")
archive_name = type_serialization(archive=archive, info=info)
if export_conf:
# print("conf")
archive_name = conf_serialization(archive=archive,
archive_name=archive_name)
if export_importers:
# print("importer")
archive_name = importer_serialization(archive=archive,
archive_name=archive_name)
if export_geo:
# print("geo")
archive_name = geo_serialization(
archive=archive, archive_name=archive_name, no_geo=no_geo)
if export_dir:
# print("directory")
archive_name = directory_serialization(archive=archive,
archive_name=archive_name)
if export_docs:
# print("document")
archive_name = document_serialization(
archive=archive, archive_name=archive_name,
operation_queryset=operation_queryset, site_queryset=site_queryset,
cr_queryset=cr_queryset, find_queryset=find_queryset,
warehouse_queryset=warehouse_queryset,
put_locks=put_locks, lock_user=lock_user
)
if export_items:
# print("operation")
archive_name = operation_serialization(
archive=archive,
archive_name=archive_name, operation_queryset=operation_queryset,
site_queryset=site_queryset, cr_queryset=cr_queryset,
find_queryset=find_queryset, warehouse_queryset=warehouse_queryset,
no_geo=no_geo, put_locks=put_locks, lock_user=lock_user)
# print("cr")
cr_serialization(
archive=archive,
archive_name=archive_name, operation_queryset=operation_queryset,
site_queryset=site_queryset, cr_queryset=cr_queryset,
find_queryset=find_queryset, warehouse_queryset=warehouse_queryset,
no_geo=no_geo, put_locks=put_locks, lock_user=lock_user)
# print("find")
find_serialization(
archive=archive,
archive_name=archive_name, operation_queryset=operation_queryset,
site_queryset=site_queryset, cr_queryset=cr_queryset,
find_queryset=find_queryset, warehouse_queryset=warehouse_queryset,
no_geo=no_geo, put_locks=put_locks, lock_user=lock_user)
# print("warehouse")
warehouse_serialization(
archive=archive,
archive_name=archive_name, operation_queryset=operation_queryset,
site_queryset=site_queryset, cr_queryset=cr_queryset,
find_queryset=find_queryset, warehouse_queryset=warehouse_queryset,
no_geo=no_geo, put_locks=put_locks, lock_user=lock_user)
return archive_name
def restore_serialized(archive_name, user=None, delete_existing=False,
release_locks=False):
with zipfile.ZipFile(archive_name, "r") as zip_file:
# check version
info = json.loads(zip_file.read("info.json").decode("utf-8"))
if info["serialize-version"] != SERIALIZATION_VERSION:
raise ValueError(
"This dump version: {} is not managed by this Ishtar "
"installation".format(info["serialize-version"])
)
DIRS = (
("types", [None]),
("common_imports", IMPORT_MODEL_LIST),
("common_configuration", CONF_MODEL_LIST),
("common_geo", GEO_MODEL_LIST),
("common_directory", DIRECTORY_MODEL_LIST),
("documents", [models.Document]),
("operations", OPERATION_MODEL_LIST),
("context_records", CR_MODEL_LIST),
("warehouse", WAREHOUSE_MODEL_LIST),
("finds", FIND_MODEL_LIST),
)
result = []
namelist = zip_file.namelist()
# restore media
if "media.zip" in namelist:
with tempfile.TemporaryDirectory() as tmp_dir_name:
zip_file.extract("media.zip", tmp_dir_name)
with zipfile.ZipFile(
tmp_dir_name + os.sep + "media.zip", 'r') as media_zip:
media_zip.extractall(settings.MEDIA_ROOT)
for current_dir, model_list in DIRS:
for current_model in model_list:
for json_filename in namelist:
path = json_filename.split(os.sep)
if len(path) != 2 or path[0] != current_dir:
continue
model = get_model_from_filename(path[-1])
if not model or (current_model and current_model != model):
continue
if delete_existing:
model.objects.all().delete()
data = zip_file.read(json_filename).decode("utf-8")
# regenerate labels, add a new version, etc.
historized = hasattr(model, "history_modifier") and (
hasattr(model, "history_creator"))
releasing_locks = hasattr(model, "locked") and (
release_locks)
need_resave = hasattr(model, "CACHED_LABELS") or \
hasattr(model, "cached_label") or \
releasing_locks or (user and historized)
idx = -1
for idx, obj in enumerate(deserialize("json", data)):
extra_attrs = {}
if historized or hasattr(model, "locked"):
keys = obj.object.natural_key()
old_obj = None
try:
old_obj = model.objects.get_by_natural_key(
*keys)
except model.DoesNotExist:
pass
if old_obj:
if historized and (old_obj.history_creator or
old_obj.history_modifier):
extra_attrs = {
"history_modifier_id":
old_obj.history_modifier_id,
"history_creator_id":
old_obj.history_creator_id
}
if hasattr(model, "locked") and old_obj.locked:
extra_attrs.update({
"locked": old_obj.locked,
"lock_user": old_obj.lock_user,
})
obj.save()
if need_resave or extra_attrs:
obj = model.objects.get(id=obj.object.id)
if user:
obj.history_modifier = user
if extra_attrs and \
"history_creator_id" in extra_attrs:
obj.history_creator_id = extra_attrs[
"history_creator_id"]
else:
obj.history_creator = user
if extra_attrs and \
"locked" in extra_attrs:
obj.locked = extra_attrs["locked"]
obj.lock_user = extra_attrs["lock_user"]
elif extra_attrs:
for k in extra_attrs:
setattr(obj, k, extra_attrs[k])
obj.skip_history_when_saving = True
if releasing_locks:
obj.locked = False
obj.lock_user = None
obj._no_move = True
obj.save()
if idx >= 0:
result.append((model.__name__, idx + 1))
return result
|