forked from dmm-com/pagoda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
300 lines (228 loc) · 10.9 KB
/
tasks.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
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
import json
import custom_view
from airone.celery import app
from airone.lib.job import may_schedule_until_job_is_ready
from airone.lib.types import AttrTypeValue
from entity.api_v2.serializers import EntityCreateSerializer, EntityUpdateSerializer
from entity.models import Entity, EntityAttr
from job.models import Job
from user.models import History, User
@app.task(bind=True)
def create_entity(self, job_id):
job = Job.objects.get(id=job_id)
if job.proceed_if_ready():
# At the first time, update job status to prevent executing this job duplicately
job.update(Job.STATUS["PROCESSING"])
user = User.objects.filter(id=job.user.id).first()
entity = Entity.objects.filter(id=job.target.id, is_active=True).first()
# for history record
entity._history_user = user
if not entity or not user:
# Abort when specified entity doesn't exist
job.update(Job.STATUS["CANCELED"])
return
recv_data = json.loads(job.params)
# register history to modify Entity
history = user.seth_entity_add(entity)
adding_attrs = []
for attr in recv_data["attrs"]:
attr_base = EntityAttr.objects.create(
name=attr["name"],
type=int(attr["type"]),
is_mandatory=attr["is_mandatory"],
is_delete_in_chain=attr["is_delete_in_chain"],
created_user=user,
parent_entity=entity,
index=int(attr["row_index"]),
)
if int(attr["type"]) & AttrTypeValue["object"]:
[attr_base.referral.add(Entity.objects.get(id=x)) for x in attr["ref_ids"]]
# This is neccesary to summarize adding attribute history to one time
adding_attrs.append(attr_base)
# register history to modify Entity
history.add_attr(attr_base)
if adding_attrs:
# save history for adding attributes if it's necessary
entity.attrs.add(*adding_attrs)
# clear flag to specify this entity has been completed to create
entity.del_status(Entity.STATUS_CREATING)
# update job status and save it
job.update(Job.STATUS["DONE"])
@app.task(bind=True)
def edit_entity(self, job_id):
job = Job.objects.get(id=job_id)
if job.proceed_if_ready():
# At the first time, update job status to prevent executing this job duplicately
job.update(Job.STATUS["PROCESSING"])
user = User.objects.filter(id=job.user.id).first()
entity = Entity.objects.filter(id=job.target.id, is_active=True).first()
# for history record
entity._history_user = user
if not entity or not user:
# Abort when specified entity doesn't exist
job.update(Job.STATUS["CANCELED"])
return
recv_data = json.loads(job.params)
# register history to modify Entity
history = user.seth_entity_mod(entity)
if entity.name != recv_data["name"]:
history.mod_entity(entity, 'old name: "%s"' % (entity.name))
if entity.name != recv_data["name"]:
entity.name = recv_data["name"]
entity.save(update_fields=["name"])
if entity.note != recv_data["note"]:
entity.note = recv_data["note"]
entity.save(update_fields=["note"])
# This describes job pamraeters of Job.update_es_docuemnt()
jp_update_es_document = {
"is_updated": True,
}
# update processing for each attrs
deleted_attr_ids = []
adding_attrs = []
for attr in recv_data["attrs"]:
if "deleted" in attr:
# In case of deleting attribute which has been already existed
attr_obj = EntityAttr.objects.get(id=attr["id"])
attr_obj.delete()
# Save deleted EntityAttr id to update es_document of Entries
# that are refered by associated AttributeValues.
deleted_attr_ids.append(attr_obj.id)
# register History to register deleting EntityAttr
history.del_attr(attr_obj)
elif "id" in attr and EntityAttr.objects.filter(id=attr["id"]).exists():
# In case of updating attribute which has been already existed
attr_obj = EntityAttr.objects.get(id=attr["id"])
# register operaion history if the parameters are changed
if attr_obj.name != attr["name"]:
history.mod_attr(attr_obj, 'old name: "%s"' % (attr_obj.name))
if attr_obj.is_mandatory != attr["is_mandatory"]:
if attr["is_mandatory"]:
history.mod_attr(attr_obj, "set mandatory flag")
else:
history.mod_attr(attr_obj, "unset mandatory flag")
# EntityAttr.is_referral_updated() is separated from EntityAttr.is_updated()
# to reduce unnecessary creation of HistoricalRecord.
params = {
"name": attr["name"],
"index": attr["row_index"],
"is_mandatory": attr["is_mandatory"],
"is_delete_in_chain": attr["is_delete_in_chain"],
}
if attr_obj.is_updated(**params):
attr_obj.name = attr["name"]
attr_obj.is_mandatory = attr["is_mandatory"]
attr_obj.is_delete_in_chain = attr["is_delete_in_chain"]
attr_obj.index = int(attr["row_index"])
attr_obj.save()
if (attr_obj.type & AttrTypeValue["object"]) and (
attr_obj.is_referral_updated([int(x) for x in attr["ref_ids"]])
):
# the case of an attribute that has referral entry
attr_obj.referral_clear()
attr_obj.referral.add(*[Entity.objects.get(id=x) for x in attr["ref_ids"]])
else:
# In case of creating new attribute
attr_obj = EntityAttr.objects.create(
name=attr["name"],
type=int(attr["type"]),
is_mandatory=attr["is_mandatory"],
is_delete_in_chain=attr["is_delete_in_chain"],
index=int(attr["row_index"]),
created_user=user,
parent_entity=entity,
)
# append referral objects
if int(attr["type"]) & AttrTypeValue["object"]:
[attr_obj.referral.add(Entity.objects.get(id=x)) for x in attr["ref_ids"]]
# add a new attribute on the existed Entries
adding_attrs.append(attr_obj)
# register History to register adding EntityAttr
history.add_attr(attr_obj)
if adding_attrs:
entity.attrs.add(*adding_attrs)
Job.new_update_documents(entity, "", jp_update_es_document).run()
# This job updates elasticsearch not only Entries that are belonged to
# the edited Entity, but also Entrie that are refered by Entry, which is
# belonged to this Entity.
associated_entity_ids = set()
for attr in entity.attrs.filter(is_active=True):
associated_entity_ids |= set([x.id for x in attr.referral.filter(is_active=True)])
# This also update es-documents of Entries that are referred by AttributeValues
# that are associated with deleted EntityAttrs.
for attr_id in deleted_attr_ids:
attr = EntityAttr.objects.filter(id=attr_id).last()
if attr:
associated_entity_ids |= set([x.id for x in attr.referral.filter(is_active=True)])
# create job to update es-document that is related with edited Entity
for related_entity_id in associated_entity_ids:
related_entity = Entity.objects.get(id=related_entity_id)
Job.new_update_documents(related_entity, "", jp_update_es_document).run()
# clear flag to specify this entity has been completed to edit
entity.del_status(Entity.STATUS_EDITING)
# update job status and save it
job.update(Job.STATUS["DONE"])
@app.task(bind=True)
def delete_entity(self, job_id):
job = Job.objects.get(id=job_id)
if job.proceed_if_ready():
# At the first time, update job status to prevent executing this job duplicately
job.update(Job.STATUS["PROCESSING"])
user = User.objects.filter(id=job.user.id).first()
entity = Entity.objects.filter(id=job.target.id, is_active=False).first()
# for history record
entity._history_user = user
if not entity or not user:
# Abort when specified entity doesn't exist
job.update(Job.STATUS["CANCELED"])
return
entity.delete()
history = user.seth_entity_del(entity)
# Delete all attributes which target Entity have
for attr in entity.attrs.all():
attr.delete()
history.del_attr(attr)
# update job status and save it
job.update(Job.STATUS["DONE"])
@app.task(bind=True)
@may_schedule_until_job_is_ready
def create_entity_v2(self, job: Job):
serializer = EntityCreateSerializer(data=json.loads(job.params), context={"_user": job.user})
if not serializer.is_valid():
return Job.STATUS["ERROR"]
serializer.create(serializer.validated_data)
# update job status and save it
return Job.STATUS["DONE"]
@app.task(bind=True)
@may_schedule_until_job_is_ready
def edit_entity_v2(self, job: Job):
entity: Entity | None = Entity.objects.filter(id=job.target.id, is_active=True).first()
if not entity:
job.update(Job.STATUS["ERROR"])
return
serializer = EntityUpdateSerializer(
instance=entity, data=json.loads(job.params), context={"_user": job.user}
)
if not serializer.is_valid():
return Job.STATUS["ERROR"]
serializer.update(entity, serializer.validated_data)
return Job.STATUS["DONE"]
@app.task(bind=True)
@may_schedule_until_job_is_ready
def delete_entity_v2(self, job: Job):
entity: Entity | None = Entity.objects.filter(id=job.target.id, is_active=True).first()
if not entity:
return Job.STATUS["ERROR"]
if custom_view.is_custom("before_delete_entity_v2"):
custom_view.call_custom("before_delete_entity_v2", None, job.user, entity)
# register operation History for deleting entity
history: History = job.user.seth_entity_del(entity)
entity.delete()
# Delete all attributes which target Entity have
entity_attr: EntityAttr
for entity_attr in entity.attrs.filter(is_active=True):
history.del_attr(entity_attr)
entity_attr.delete()
if custom_view.is_custom("after_delete_entity_v2"):
custom_view.call_custom("after_delete_entity_v2", None, job.user, entity)
return Job.STATUS["DONE"]