|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
""" |
|
4
|
|
|
Created on Wed Feb 27 16:38:37 2019 |
|
5
|
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
|
7
|
|
|
""" |
|
8
|
|
|
|
|
9
|
|
|
from celery.utils.log import get_task_logger |
|
10
|
|
|
import asyncio |
|
11
|
|
|
|
|
12
|
|
|
from common.constants import ERROR, LOADED, STATUSES |
|
13
|
|
|
from common.helpers import send_message_to_websocket |
|
14
|
|
|
from image.celery import app as celery_app, MyTask |
|
15
|
|
|
from image_app.models import Animal, Submission |
|
16
|
|
|
from submissions.helpers import send_message |
|
17
|
|
|
from validation.helpers import construct_validation_message |
|
18
|
|
|
|
|
19
|
|
|
# Get an instance of a logger |
|
20
|
|
|
logger = get_task_logger(__name__) |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
View Code Duplication |
class BatchUpdateAnimals(MyTask): |
|
|
|
|
|
|
24
|
|
|
name = "Batch update" |
|
25
|
|
|
description = """Batch update of field in animals""" |
|
26
|
|
|
|
|
27
|
|
|
# Ovverride default on failure method |
|
28
|
|
|
# This is not a failed validation for a wrong value, this is an |
|
29
|
|
|
# error in task that mean an error in coding |
|
30
|
|
|
def on_failure(self, exc, task_id, args, kwargs, einfo): |
|
31
|
|
|
logger.error('{0!r} failed: {1!r}'.format(task_id, exc)) |
|
32
|
|
|
|
|
33
|
|
|
# get submissio object |
|
34
|
|
|
submission_obj = Submission.objects.get(pk=args[0]) |
|
35
|
|
|
|
|
36
|
|
|
# mark submission with ERROR |
|
37
|
|
|
submission_obj.status = ERROR |
|
38
|
|
|
submission_obj.message = ("Error in batch update for animals: %s" % (str(exc))) |
|
39
|
|
|
submission_obj.save() |
|
40
|
|
|
|
|
41
|
|
|
asyncio.get_event_loop().run_until_complete( |
|
42
|
|
|
send_message_to_websocket( |
|
43
|
|
|
{ |
|
44
|
|
|
'message': STATUSES.get_value_display(ERROR), |
|
45
|
|
|
'notification_message': submission_obj.message |
|
46
|
|
|
}, |
|
47
|
|
|
args[0] |
|
48
|
|
|
) |
|
49
|
|
|
) |
|
50
|
|
|
|
|
51
|
|
|
# send a mail to the user with the stacktrace (einfo) |
|
52
|
|
|
submission_obj.owner.email_user( |
|
53
|
|
|
"Error in batch update for animals: %s" % (args[0]), |
|
54
|
|
|
("Something goes wrong in batch update for animals. Please report " |
|
55
|
|
|
"this to InjectTool team\n\n %s" % str(einfo)), |
|
56
|
|
|
) |
|
57
|
|
|
|
|
58
|
|
|
# TODO: submit mail to admin |
|
59
|
|
|
|
|
60
|
|
|
def run(self, submission_id, animal_ids): |
|
61
|
|
|
"""a function to upload data into UID""" |
|
62
|
|
|
|
|
63
|
|
|
logger.info("Start batch update for animals") |
|
64
|
|
|
|
|
65
|
|
|
for animal_id, value in animal_ids.items(): |
|
66
|
|
|
animal = Animal.objects.get(pk=animal_id) |
|
67
|
|
|
animal.birth_location = value |
|
68
|
|
|
animal.save() |
|
69
|
|
|
|
|
70
|
|
|
# Update submission |
|
71
|
|
|
submission_obj = Submission.objects.get(pk=submission_id) |
|
72
|
|
|
submission_obj.status = LOADED |
|
73
|
|
|
submission_obj.message = "Data updated, try to rerun validation" |
|
74
|
|
|
submission_obj.save() |
|
75
|
|
|
|
|
76
|
|
|
send_message( |
|
77
|
|
|
submission_obj, construct_validation_message(submission_obj) |
|
78
|
|
|
) |
|
79
|
|
|
return 'success' |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
# register explicitly tasks |
|
83
|
|
|
# https://github.com/celery/celery/issues/3744#issuecomment-271366923 |
|
84
|
|
|
celery_app.tasks.register(BatchUpdateAnimals) |
|
85
|
|
|
|