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
|
from copy import deepcopy
import datetime
import json
import importlib
import os
import tempfile
import zipfile
from rest_framework import serializers
from zipfile import ZipFile
from django.apps import apps
from django.contrib.sites.models import Site
from django.core.serializers import deserialize, serialize
from version import get_version
from . import models
class PublicSerializer(serializers.BaseSerializer):
def to_representation(self, obj):
return obj.public_representation()
SERIALIZATION_VERSION = "1.0"
def get_model_from_filename(filename):
filename = filename.split(".")[0] # remove extension
module_name, model_name = filename.split("__")
module = importlib.import_module(module_name + ".models")
return getattr(module, model_name)
def serialization_info():
site = Site.objects.get_current()
return {
"serialize-version": SERIALIZATION_VERSION,
"ishtar-version": get_version(),
"domain": site.domain,
"name": site.name,
"date": datetime.datetime.now().isoformat()
}
def archive_serialization(result, archive_dir=None, archive=False,
return_empty_types=False, archive_name=None):
"""
Serialize all types models to JSON
Used for import and export scripts
:param result: serialization results
:param archive_dir: directory inside the archive (default None)
:param return_empty_types: if True instead of serialization return empty
types (default False)
:param archive: if True return a zip file containing all the file serialized
(default False)
:return: string containing the json serialization of types unless
return_empty_types or archive is set to True
"""
if archive and return_empty_types:
raise ValueError("archive and return_empty_types are incompatible")
if return_empty_types:
return [k for k in result if not result[k]]
if not archive:
return result
archive_created = False
if not archive_name:
archive_created = True
tmpdir = tempfile.mkdtemp(prefix="ishtarexport-") + os.sep
archive_name = tmpdir + "ishtar-{}.zip".format(
datetime.date.today().strftime("%Y-%m-%d")
)
if not archive_name.endswith(".zip"):
archive_name += ".zip"
with tempfile.TemporaryDirectory() as tmpdirname:
if archive_dir:
os.mkdir(tmpdirname + os.sep + archive_dir)
with ZipFile(archive_name, 'w') as current_zip:
if archive_created:
base_filename = "info.json"
filename = tmpdirname + os.sep + base_filename
with open(filename, "w") as json_file:
json_file.write(
json.dumps(serialization_info(), indent=2)
)
current_zip.write(filename, arcname=base_filename)
for model_name in result:
base_filename = model_name + ".json"
filename = tmpdirname + os.sep + base_filename
with open(filename, "w") as json_file:
json_file.write(result[model_name])
current_zip.write(filename,
arcname="types" + os.sep + base_filename)
return archive_name
def type_serialization(archive=False, return_empty_types=False,
archive_name=None):
result = {}
for model in apps.get_models():
if not isinstance(model(), models.GeneralType):
continue
model_name = model.__name__
model_name = str(model.__module__).split(".")[0] + "__" + model_name
base_q = model.objects
q = base_q
recursion = None
if hasattr(model, "parent"):
recursion = "parent"
elif hasattr(model, "inverse_relation"):
recursion = "inverse_relation"
if recursion:
q = q.filter(**{recursion + "__isnull": True})
result[model_name] = serialize(
"json", q.all(),
indent=2,
use_natural_foreign_keys=True, use_natural_primary_keys=True
)
if recursion:
serialized = [item["id"] for item in q.values("id").all()]
q = base_q.filter(**{recursion + "_id__in": serialized}
).exclude(id__in=serialized)
while q.count():
v = serialize(
"json", q.all(), indent=2, use_natural_foreign_keys=True,
use_natural_primary_keys=True)
new_result = json.loads(result[model_name])
new_result += json.loads(v)
result[model_name] = json.dumps(new_result, indent=2)
serialized += [item["id"] for item in q.values("id").all()]
q = base_q.filter(**{recursion + "_id__in": serialized}
).exclude(id__in=serialized)
# managed circular
q = base_q.exclude(id__in=serialized)
if q.count():
v = serialize(
"json", q.all(), indent=2, use_natural_foreign_keys=True,
use_natural_primary_keys=True)
result_to_add = json.loads(v)
result_cleaned = deepcopy(result_to_add)
for res in result_cleaned: # first add with no recursion
res["fields"][recursion] = None
new_result = json.loads(result[model_name])
new_result += result_cleaned
new_result += result_to_add
result[model_name] = json.dumps(new_result, indent=2)
return archive_serialization(result, archive_dir="types", archive=archive,
return_empty_types=return_empty_types,
archive_name=archive_name)
def restore_serialized(archive_name, delete_existing=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"])
)
# restore types
for json_filename in zip_file.namelist():
path = json_filename.split(os.sep)
if len(path) != 2 or path[0] != "types":
continue
if delete_existing:
model = get_model_from_filename(path[-1])
model.objects.all().delete()
data = zip_file.read(json_filename).decode("utf-8")
for obj in deserialize("json", data):
obj.save()
|