1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Thu Jun 27 11:52:37 2019 |
5
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
import asyncio |
10
|
|
|
import re |
11
|
|
|
|
12
|
|
|
from import_export import resources |
13
|
|
|
|
14
|
|
|
from common.constants import STATUSES |
15
|
|
|
from common.helpers import send_message_to_websocket |
16
|
|
|
from uid.models import Animal, Sample |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
def send_message(submission_obj, validation_message=None): |
20
|
|
|
""" |
21
|
|
|
Update submission.status and submission message using django |
22
|
|
|
channels |
23
|
|
|
|
24
|
|
|
Args: |
25
|
|
|
submission_obj (uid.models.Submission): an UID submission |
26
|
|
|
object |
27
|
|
|
validation_message (dict): set validation message |
28
|
|
|
""" |
29
|
|
|
|
30
|
|
|
# define a message to send |
31
|
|
|
message = { |
32
|
|
|
'message': STATUSES.get_value_display(submission_obj.status), |
33
|
|
|
'notification_message': submission_obj.message, |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
# if validation message is needed, add to the final message |
37
|
|
|
if validation_message: |
38
|
|
|
message['validation_message'] = validation_message |
39
|
|
|
|
40
|
|
|
# now send the message to its submission |
41
|
|
|
asyncio.get_event_loop().run_until_complete( |
42
|
|
|
send_message_to_websocket( |
43
|
|
|
message, |
44
|
|
|
submission_obj.pk |
45
|
|
|
) |
46
|
|
|
) |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
def is_target_in_message(target, messages): |
50
|
|
|
""" |
51
|
|
|
This function will return true if target in message |
52
|
|
|
|
53
|
|
|
Args: |
54
|
|
|
target (str): target to search |
55
|
|
|
""" |
56
|
|
|
for message in messages: |
57
|
|
|
if re.search(message, target): |
58
|
|
|
return True |
59
|
|
|
return False |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
# to export data with django-import-export library |
63
|
|
|
class AnimalResource(resources.ModelResource): |
64
|
|
|
class Meta: |
65
|
|
|
model = Animal |
66
|
|
|
fields = ( |
67
|
|
|
'id', |
68
|
|
|
'name', |
69
|
|
|
'biosample_id', |
70
|
|
|
'material', |
71
|
|
|
'status', |
72
|
|
|
'last_changed', |
73
|
|
|
'last_submitted' |
74
|
|
|
) |
75
|
|
|
|
76
|
|
|
export_order = fields |
77
|
|
|
|
78
|
|
|
def dehydrate_status(self, animal): |
79
|
|
|
"""Convert a numeric status field into the displayed column""" |
80
|
|
|
|
81
|
|
|
return STATUSES.get_value_display(animal.status) |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
class SampleResource(resources.ModelResource): |
85
|
|
|
class Meta: |
86
|
|
|
model = Sample |
87
|
|
|
fields = ( |
88
|
|
|
'id', |
89
|
|
|
'name', |
90
|
|
|
'biosample_id', |
91
|
|
|
'material', |
92
|
|
|
'status', |
93
|
|
|
'last_changed', |
94
|
|
|
'last_submitted' |
95
|
|
|
) |
96
|
|
|
|
97
|
|
|
export_order = fields |
98
|
|
|
|
99
|
|
|
def dehydrate_status(self, sample): |
100
|
|
|
"""Convert a numeric status field into the displayed column""" |
101
|
|
|
|
102
|
|
|
return STATUSES.get_value_display(sample.status) |
103
|
|
|
|