|
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
|
|
|
|
|
11
|
|
|
from django.db import transaction |
|
12
|
|
|
|
|
13
|
|
|
from common.constants import ERROR, NEED_REVISION |
|
14
|
|
|
from common.tasks import BatchUpdateMixin, BatchFailurelMixin |
|
15
|
|
|
from image.celery import app as celery_app, MyTask |
|
16
|
|
|
from image_app.models import Submission, Sample, Name |
|
17
|
|
|
from submissions.helpers import send_message |
|
18
|
|
|
from validation.helpers import construct_validation_message |
|
19
|
|
|
from validation.models import ValidationSummary |
|
20
|
|
|
|
|
21
|
|
|
# Get an instance of a logger |
|
22
|
|
|
logger = get_task_logger(__name__) |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class BatchDeleteSamples(BatchFailurelMixin, MyTask): |
|
26
|
|
|
name = "Batch delete samples" |
|
27
|
|
|
description = """Batch remove samples""" |
|
28
|
|
|
batch_type = "delete" |
|
29
|
|
|
model_type = "sample" |
|
30
|
|
|
submission_cls = Submission |
|
31
|
|
|
|
|
32
|
|
|
def run(self, submission_id, sample_ids): |
|
33
|
|
|
"""Function for batch update attribute in animals |
|
34
|
|
|
Args: |
|
35
|
|
|
submission_id (int): id of submission |
|
36
|
|
|
sample_ids (list): set with ids to delete |
|
37
|
|
|
""" |
|
38
|
|
|
|
|
39
|
|
|
# get a submisision object |
|
40
|
|
|
submission_obj = Submission.objects.get(pk=submission_id) |
|
41
|
|
|
|
|
42
|
|
|
logger.info("Start batch delete for samples") |
|
43
|
|
|
success_ids = list() |
|
44
|
|
|
failed_ids = list() |
|
45
|
|
|
|
|
46
|
|
|
for sample_id in sample_ids: |
|
47
|
|
|
try: |
|
48
|
|
|
name = Name.objects.get( |
|
49
|
|
|
name=sample_id, submission=submission_obj) |
|
50
|
|
|
|
|
51
|
|
|
sample_obj = Sample.objects.get(name=name) |
|
52
|
|
|
|
|
53
|
|
|
with transaction.atomic(): |
|
54
|
|
|
sample_obj.delete() |
|
55
|
|
|
name.delete() |
|
56
|
|
|
success_ids.append(sample_id) |
|
57
|
|
|
|
|
58
|
|
|
except Name.DoesNotExist: |
|
59
|
|
|
failed_ids.append(sample_id) |
|
60
|
|
|
|
|
61
|
|
|
except Sample.DoesNotExist: |
|
62
|
|
|
failed_ids.append(sample_id) |
|
63
|
|
|
|
|
64
|
|
|
# Update submission |
|
65
|
|
|
submission_obj.refresh_from_db() |
|
66
|
|
|
submission_obj.status = NEED_REVISION |
|
67
|
|
|
|
|
68
|
|
|
if len(failed_ids) != 0: |
|
69
|
|
|
submission_obj.message = f"You've removed {len(success_ids)} " \ |
|
70
|
|
|
f"samples. It wasn't possible to find records with these " \ |
|
71
|
|
|
f"ids: {', '.join(failed_ids)}. Rerun validation please!" |
|
72
|
|
|
else: |
|
73
|
|
|
submission_obj.message = f"You've removed {len(success_ids)} " \ |
|
74
|
|
|
f"samples. Rerun validation please!" |
|
75
|
|
|
|
|
76
|
|
|
submission_obj.save() |
|
77
|
|
|
|
|
78
|
|
|
summary_obj, created = ValidationSummary.objects.get_or_create( |
|
79
|
|
|
submission=submission_obj, type='sample') |
|
80
|
|
|
summary_obj.reset_all_count() |
|
81
|
|
|
|
|
82
|
|
|
send_message( |
|
83
|
|
|
submission_obj, construct_validation_message(submission_obj) |
|
84
|
|
|
) |
|
85
|
|
|
|
|
86
|
|
|
logger.info("batch delete for samples completed") |
|
87
|
|
|
|
|
88
|
|
|
return 'success' |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
class BatchUpdateSamples(BatchFailurelMixin, BatchUpdateMixin, MyTask): |
|
92
|
|
|
name = "Batch update samples" |
|
93
|
|
|
description = """Batch update of field in samples""" |
|
94
|
|
|
batch_type = "update" |
|
95
|
|
|
model_type = "sample" |
|
96
|
|
|
|
|
97
|
|
|
item_cls = Sample |
|
98
|
|
|
submission_cls = Submission |
|
99
|
|
|
|
|
100
|
|
|
def run(self, submission_id, sample_ids, attribute): |
|
101
|
|
|
"""Function for batch update attribute in samples |
|
102
|
|
|
Args: |
|
103
|
|
|
submission_id (int): id of submission |
|
104
|
|
|
sample_ids (dict): dict with id and values to update |
|
105
|
|
|
attribute (str): attribute to update |
|
106
|
|
|
""" |
|
107
|
|
|
|
|
108
|
|
|
logger.info("Start batch update for samples") |
|
109
|
|
|
self.batch_update(submission_id, sample_ids, attribute) |
|
110
|
|
|
|
|
111
|
|
|
return 'success' |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
# register explicitly tasks |
|
115
|
|
|
# https://github.com/celery/celery/issues/3744#issuecomment-271366923 |
|
116
|
|
|
celery_app.tasks.register(BatchDeleteSamples) |
|
117
|
|
|
celery_app.tasks.register(BatchUpdateSamples) |
|
118
|
|
|
|