|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
""" |
|
4
|
|
|
Created on Tue Jul 9 16:10:06 2019 |
|
5
|
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
|
7
|
|
|
""" |
|
8
|
|
|
|
|
9
|
|
|
import logging |
|
10
|
|
|
|
|
11
|
|
|
from common.constants import ERROR |
|
12
|
|
|
|
|
13
|
|
|
from .helpers import send_message |
|
14
|
|
|
|
|
15
|
|
|
# Get an instance of a logger |
|
16
|
|
|
logger = logging.getLogger(__name__) |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class ImportGenericTaskMixin(): |
|
20
|
|
|
name = None |
|
21
|
|
|
description = None |
|
22
|
|
|
submission_model = None |
|
23
|
|
|
datasource_type = None |
|
24
|
|
|
|
|
25
|
|
|
# Ovverride default on failure method |
|
26
|
|
|
# This is not a failed validation for a wrong value, this is an |
|
27
|
|
|
# error in task that mean an error in coding |
|
28
|
|
|
def on_failure(self, exc, task_id, args, kwargs, einfo): |
|
29
|
|
|
logger.error('{0!r} failed: {1!r}'.format(task_id, exc)) |
|
30
|
|
|
|
|
31
|
|
|
# get submission object |
|
32
|
|
|
submission_obj = self.submission_model.objects.get(pk=args[0]) |
|
33
|
|
|
|
|
34
|
|
|
# mark submission with ERROR |
|
35
|
|
|
submission_obj.status = ERROR |
|
36
|
|
|
submission_obj.message = ( |
|
37
|
|
|
"Error in %s loading: %s" % (self.datasource_type, 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 %s loading: %s" % ( |
|
46
|
|
|
self.datasource_type, args[0]), |
|
47
|
|
|
("Something goes wrong with %s loading. Please report " |
|
48
|
|
|
"this to InjectTool team\n\n %s" % ( |
|
49
|
|
|
self.datasource_type, |
|
50
|
|
|
str(einfo))), |
|
51
|
|
|
) |
|
52
|
|
|
|
|
53
|
|
|
# TODO: submit mail to admin |
|
54
|
|
|
|
|
55
|
|
|
def run(self, submission_id): |
|
56
|
|
|
"""a function to upload data into UID""" |
|
57
|
|
|
|
|
58
|
|
|
logger.info( |
|
59
|
|
|
"Start import from %s for submission: %s" % ( |
|
60
|
|
|
self.datasource_type, submission_id)) |
|
61
|
|
|
|
|
62
|
|
|
# get a submission object |
|
63
|
|
|
submission = self.submission_model.objects.get(pk=submission_id) |
|
64
|
|
|
|
|
65
|
|
|
# upload data into UID with the proper method (defined in child class) |
|
66
|
|
|
status = self.import_data_from_file(submission) |
|
67
|
|
|
|
|
68
|
|
|
# if something went wrong, uploaded_cryoweb has token the exception |
|
69
|
|
|
# ad update submission.message field |
|
70
|
|
|
if status is False: |
|
71
|
|
|
message = "Error in uploading %s data" % (self.datasource_type) |
|
72
|
|
|
logger.error(message) |
|
73
|
|
|
|
|
74
|
|
|
# this a failure in my import, not the task itself |
|
75
|
|
|
return message |
|
76
|
|
|
|
|
77
|
|
|
else: |
|
78
|
|
|
message = "%s import completed for submission: %s" % ( |
|
79
|
|
|
self.datasource_type, submission_id) |
|
80
|
|
|
|
|
81
|
|
|
# debug |
|
82
|
|
|
logger.info(message) |
|
83
|
|
|
|
|
84
|
|
|
# always return something |
|
85
|
|
|
return "success" |
|
86
|
|
|
|