-
Notifications
You must be signed in to change notification settings - Fork 31
/
tasks.py
257 lines (198 loc) · 8.42 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
from billiard.exceptions import WorkerLostError
from celery.utils.log import get_task_logger
from celery_once import QueueOnce
from django.apps import apps
from django.core.mail import EmailMessage
from django.core.management import call_command
from django.template.loader import render_to_string
from django_elasticsearch_dsl.registries import registry
from core.celery import app
from core.common.constants import CONFIRM_EMAIL_ADDRESS_MAIL_SUBJECT, PASSWORD_RESET_MAIL_SUBJECT
from core.common.utils import write_export_file, web_url
logger = get_task_logger(__name__)
@app.task(base=QueueOnce)
def delete_organization(org_id):
from core.orgs.models import Organization
logger.info('Finding org...')
org = Organization.objects.filter(id=org_id).first()
if not org: # pragma: no cover
logger.info('Not found org %s', org_id)
return
try:
logger.info('Found org %s. Beginning purge...', org.mnemonic)
org.delete()
from core.pins.models import Pin
Pin.objects.filter(resource_type__model='organization', resource_id=org.id).delete()
from core.client_configs.models import ClientConfig
ClientConfig.objects.filter(resource_type__model='organization', resource_id=org.id).delete()
logger.info('Purge complete!')
except Exception as ex:
logger.info('Org delete failed for %s with exception %s', org.mnemonic, ex.args)
@app.task(base=QueueOnce, bind=True)
def export_source(self, version_id):
from core.sources.models import Source
logger.info('Finding source version...')
version = Source.objects.filter(id=version_id).select_related(
'organization', 'user'
).first()
if not version: # pragma: no cover
logger.info('Not found source version %s', version_id)
return
version.add_processing(self.request.id)
try:
logger.info('Found source version %s. Beginning export...', version.version)
write_export_file(version, 'source', 'core.sources.serializers.SourceVersionExportSerializer', logger)
logger.info('Export complete!')
finally:
version.remove_processing(self.request.id)
@app.task(base=QueueOnce, bind=True)
def export_collection(self, version_id):
from core.collections.models import Collection
logger.info('Finding collection version...')
version = Collection.objects.filter(id=version_id).select_related(
'organization', 'user'
).first()
if not version: # pragma: no cover
logger.info('Not found collection version %s', version_id)
return
version.add_processing(self.request.id)
try:
logger.info('Found collection version %s. Beginning export...', version.version)
write_export_file(
version, 'collection', 'core.collections.serializers.CollectionVersionExportSerializer', logger
)
logger.info('Export complete!')
finally:
version.remove_processing(self.request.id)
@app.task(bind=True)
def add_references(
self, user, data, collection, host_url, cascade_mappings=False
): # pylint: disable=too-many-arguments
head = collection.get_head()
head.add_processing(self.request.id)
try:
(added_references, errors) = collection.add_expressions(data, host_url, user, cascade_mappings)
finally:
head.remove_processing(self.request.id)
for ref in added_references:
if ref.concepts:
for concept in ref.concepts:
concept.save()
if ref.mappings:
for mapping in ref.mappings:
mapping.save()
return added_references, errors
def __handle_save(instance):
registry.update(instance)
registry.update_related(instance)
def __handle_pre_delete(instance):
registry.delete_related(instance)
@app.task(
ignore_result=True, autoretry_for=(Exception, WorkerLostError, ), retry_kwargs={'max_retries': 2, 'countdown': 2},
acks_late=True, reject_on_worker_lost=True
)
def handle_save(app_name, model_name, instance_id):
__handle_save(apps.get_model(app_name, model_name).objects.get(id=instance_id))
@app.task(
ignore_result=True, autoretry_for=(Exception, WorkerLostError, ), retry_kwargs={'max_retries': 2, 'countdown': 2},
acks_late=True, reject_on_worker_lost=True
)
def handle_m2m_changed(app_name, model_name, instance_id, action):
instance = apps.get_model(app_name, model_name).objects.get(id=instance_id)
if action in ('post_add', 'post_remove', 'post_clear'):
__handle_save(instance)
elif action in ('pre_remove', 'pre_clear'):
__handle_pre_delete(instance)
@app.task(ignore_result=True)
def handle_pre_delete(app_name, model_name, instance_id):
__handle_pre_delete(apps.get_model(app_name, model_name).objects.get(id=instance_id))
@app.task(base=QueueOnce)
def populate_indexes(app_names=None): # app_names has to be an iterable of strings
__run_search_index_command('--populate', app_names)
@app.task(base=QueueOnce)
def rebuild_indexes(app_names=None): # app_names has to be an iterable of strings
__run_search_index_command('--rebuild', app_names)
def __run_search_index_command(command, app_names=None):
if not command:
return
if app_names:
call_command('search_index', '%s' % command, '-f', '--models', *app_names, '--parallel')
else:
call_command('search_index', command, '-f', '--parallel')
@app.task(base=QueueOnce)
def bulk_import(to_import, username, update_if_exists):
from core.importers.models import BulkImport
return BulkImport(content=to_import, username=username, update_if_exists=update_if_exists).run()
@app.task(base=QueueOnce, bind=True)
def bulk_import_parallel_inline(self, to_import, username, update_if_exists, threads=5):
from core.importers.models import BulkImportParallelRunner
return BulkImportParallelRunner(
content=to_import, username=username, update_if_exists=update_if_exists, parallel=threads,
self_task_id=self.request.id
).run()
@app.task(base=QueueOnce)
def bulk_import_inline(to_import, username, update_if_exists):
from core.importers.models import BulkImportInline
return BulkImportInline(content=to_import, username=username, update_if_exists=update_if_exists).run()
@app.task(bind=True)
def bulk_import_parts_inline(self, input_list, username, update_if_exists):
from core.importers.models import BulkImportInline
return BulkImportInline(
content=None, username=username, update_if_exists=update_if_exists, input_list=input_list,
self_task_id=self.request.id
).run()
@app.task(base=QueueOnce)
def bulk_priority_import(to_import, username, update_if_exists):
from core.importers.models import BulkImport
return BulkImport(content=to_import, username=username, update_if_exists=update_if_exists).run()
@app.task
def send_user_verification_email(user_id):
from core.users.models import UserProfile
user = UserProfile.objects.filter(id=user_id).first()
if not user:
return user
html_body = render_to_string(
'verification.html', {
'user': user,
'url': user.email_verification_url,
}
)
mail = EmailMessage(subject=CONFIRM_EMAIL_ADDRESS_MAIL_SUBJECT, body=html_body, to=[user.email])
mail.content_subtype = "html"
mail.send()
return mail
@app.task
def send_user_reset_password_email(user_id):
from core.users.models import UserProfile
user = UserProfile.objects.filter(id=user_id).first()
if not user:
return user
html_body = render_to_string(
'password_reset.html', {
'user': user,
'url': user.reset_password_url,
'web_url': web_url(),
}
)
mail = EmailMessage(subject=PASSWORD_RESET_MAIL_SUBJECT, body=html_body, to=[user.email])
mail.content_subtype = "html"
mail.send()
return mail
@app.task(bind=True)
def seed_children(self, resource, obj_id):
instance = None
if resource == 'source':
from core.sources.models import Source
instance = Source.objects.filter(id=obj_id).first()
if resource == 'collection':
from core.collections.models import Collection
instance = Collection.objects.filter(id=obj_id).first()
if instance:
task_id = self.request.id
try:
instance.add_processing(task_id)
instance.seed_concepts()
instance.seed_mappings()
instance.seed_references()
finally:
instance.remove_processing(task_id)