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