/
part.py
255 lines (210 loc) · 8.65 KB
/
part.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Hive Appier Framework
# Copyright (c) 2008-2019 Hive Solutions Lda.
#
# This file is part of Hive Appier Framework.
#
# Hive Appier Framework is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by the Apache
# Foundation, either version 2.0 of the License, or (at your option) any
# later version.
#
# Hive Appier Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Apache License for more details.
#
# You should have received a copy of the Apache License along with
# Hive Appier Framework. If not, see <http://www.apache.org/licenses/>.
__author__ = "João Magalhães <joamag@hive.pt>"
""" The author(s) of the module """
__version__ = "1.0.0"
""" The version of the module """
__revision__ = "$LastChangedRevision$"
""" The revision number of the module """
__date__ = "$LastChangedDate$"
""" The last change date of the module """
__copyright__ = "Copyright (c) 2008-2019 Hive Solutions Lda."
""" The copyright for the module """
__license__ = "Apache License, Version 2.0"
""" The license for the module """
import json
import types
import zipfile
import tempfile
import appier
from appier_extras import base
class ExportPart(appier.Part):
"""
Modular part class that provides an infra-structure for the individual
export of models using a JSON exporting format.
"""
def __init__(self, *args, **kwargs):
appier.Part.__init__(self, *args, **kwargs)
def version(self):
return base.VERSION
def load(self):
appier.Part.load(self)
self._monkey_patch()
def routes(self):
return [
(("GET",), "/json/<str:model>.zip", self.zip_model_json, None, True),
(("GET",), "/json/<str:model>/<str:_id>.zip", self.zip_entity_json, None, True)
]
@classmethod
@appier.link(name = "JSON Global")
def json_collection(cls):
return appier.get_app().url_for(
"admin.show_model_json",
model = cls._under()
)
@classmethod
@appier.link(name = "JSON Zip Global")
def json_collection_zip(cls):
return appier.get_app().url_for(
"export.zip_model_json",
model = cls._under()
)
@classmethod
@appier.operation(
name = "Import JSON",
description = "Imports a JSON representing this entity.",
parameters = (
("Contents", "contents", "text"),
("Empty source", "empty", bool, False)
)
)
def import_json(cls, contents, empty):
appier.verify(not contents == None, "Contents must be defined.")
if empty: cls.delete_c()
if appier.legacy.is_bytes(contents):
contents = contents.decode("utf-8")
models = json.loads(contents)
if not isinstance(models, list): models = [models]
for model_d in models:
del model_d["_id"]
model = cls(model_d)
model.save()
@classmethod
@appier.operation(
name = "Import JSON File",
parameters = (
("JSON File", "file", "file"),
("Empty source", "empty", bool, False)
)
)
def import_json_file(cls, file, empty):
def callback(model_d):
model = cls(model_d)
model.save()
if empty: cls.delete_c()
cls._json_import(file, callback)
@classmethod
@appier.operation(
name = "Import JSON Zip File",
parameters = (
("JSON Zip File", "file", "file"),
("Empty source", "empty", bool, False)
)
)
def import_json_zip(cls, file, empty):
if empty: cls.delete_c()
_file_name, _mime_type, data = file
with zipfile.ZipFile(appier.legacy.BytesIO(data), "r") as open_zip:
for name in open_zip.namelist():
content = open_zip.read(name)
cls.import_json(content, False)
@appier.ensure(token = "admin", context = "admin")
def zip_model_json(self, model):
model_json = self.admin_part.show_model_json(model)
_zip_handle, zip_path = tempfile.mkstemp()
zip_file = zipfile.ZipFile(zip_path, mode = "w", allowZip64 = True)
prefix = "%s_%s" % (self.info_dict()["name"], model)
try:
zip_file.writestr("%s.json" % prefix, json.dumps(model_json))
finally:
zip_file.close()
return self.send_path(
zip_path,
name = "%s_json.zip" % prefix
)
@appier.ensure(token = "admin", context = "admin")
def zip_entity_json(self, model, _id):
model_json = self.admin_part.show_entity_json(model, _id)
_zip_handle, zip_path = tempfile.mkstemp()
zip_file = zipfile.ZipFile(zip_path, mode = "w", allowZip64 = True)
prefix = "%s_%s_%s" % (self.info_dict()["name"], model, _id)
try:
zip_file.writestr("%s.json" % prefix, json.dumps(model_json))
finally: zip_file.close()
return self.send_path(
zip_path,
name = "%s_json.zip" % prefix
)
@appier.link(name = "JSON")
def json(self):
return appier.get_app().url_for(
"admin.show_entity_json",
model = self.__class__.__name__.lower(),
_id = self._id
)
@appier.link(name = "JSON Zip")
def json_zip(self):
return appier.get_app().url_for(
"export.zip_entity_json",
model = self.__class__.__name__.lower(),
_id = self._id
)
def _monkey_patch(
self,
models = None,
base_cls = None,
class_methods = None,
instance_methods = None
):
# verifies that a set of model classes have been provided to the method
# and if that's not the case assumes that the complete set of model classes
# registers in the current application should be patched
models = models or list(self.owner.models_l)
base_cls = base_cls or appier.Model
class_methods = class_methods or (
ExportPart.json_collection,
ExportPart.json_collection_zip,
ExportPart.import_json,
ExportPart.import_json_file,
ExportPart.import_json_zip
)
instance_methods = instance_methods or (
ExportPart.json,
ExportPart.json_zip
)
# adds both the class and the instance method names to the sequence of ordered
# values to be computed by the base class (new methods added)
base_cls._ordered += [class_method.__name__ for class_method in class_methods]
base_cls._ordered += [instance_method.__name__ for instance_method in instance_methods]
# iterates over the "special" instance methods to be added and changes
# them to comply with the new instance intrinsics
for instance_method in instance_methods:
setattr(base_cls, instance_method.__name__, instance_method)
# iterates over the complete set of model classes that are going to
# be patched by adding the class and instance methods
for model in models:
# in case the model is not a sub class of the base class for
# the current operation (nothing to be done) continues the loop
if not issubclass(model, base_cls): continue
# iterates over the complete set of class methods to add to the current
# model class, then instantiates them and set the as attributes
for class_method in class_methods:
bound_method = types.MethodType(class_method.__func__, model)
setattr(model, class_method.__name__, bound_method)
# iterates over the "special" instance methods to be added and schedules
# them to be bounded once the model instance is created, this is done
# by adding them to the extra methods sequence of the model class
for instance_method in instance_methods:
model._extra_methods.append(
(instance_method.__name__, instance_method.__func__)
)
# invalidates the methods cache in the model class, so that the
# next retrieval already includes the newly added methods
if "_methods" in model.__dict__: del model.__dict__["_methods"]