|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
""" |
|
4
|
|
|
Created on Tue Jul 24 15:49:23 2018 |
|
5
|
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
|
7
|
|
|
""" |
|
8
|
|
|
|
|
9
|
|
|
import logging |
|
10
|
|
|
import ast |
|
11
|
|
|
import re |
|
12
|
|
|
from django.core.exceptions import ObjectDoesNotExist |
|
13
|
|
|
|
|
14
|
|
|
from django.contrib import messages |
|
15
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin |
|
16
|
|
|
from django.db.models import Q |
|
17
|
|
|
from django.http import HttpResponseRedirect |
|
18
|
|
|
from django.views.generic import ( |
|
19
|
|
|
CreateView, DetailView, ListView, UpdateView, DeleteView) |
|
20
|
|
|
from django.views.generic.edit import BaseUpdateView |
|
21
|
|
|
from django.shortcuts import get_object_or_404, redirect |
|
22
|
|
|
from django.urls import reverse_lazy, reverse |
|
23
|
|
|
|
|
24
|
|
|
from common.constants import ( |
|
25
|
|
|
WAITING, ERROR, SUBMITTED, NEED_REVISION, CRYOWEB_TYPE, CRB_ANIM_TYPE, |
|
26
|
|
|
TIME_UNITS, VALIDATION_MESSAGES_ATTRIBUTES, SAMPLE_STORAGE, |
|
27
|
|
|
SAMPLE_STORAGE_PROCESSING, ACCURACIES, UNITS_VALIDATION_MESSAGES, |
|
28
|
|
|
VALUES_VALIDATION_MESSAGES) |
|
29
|
|
|
from common.helpers import get_deleted_objects, uid2biosample |
|
30
|
|
|
from common.views import OwnerMixin |
|
31
|
|
|
from crbanim.tasks import ImportCRBAnimTask |
|
32
|
|
|
from cryoweb.tasks import import_from_cryoweb |
|
33
|
|
|
|
|
34
|
|
|
from image_app.models import Submission, Name, Animal, Sample |
|
35
|
|
|
from excel.tasks import ImportTemplateTask |
|
36
|
|
|
|
|
37
|
|
|
from validation.helpers import construct_validation_message |
|
38
|
|
|
from validation.models import ValidationSummary |
|
39
|
|
|
from animals.tasks import BatchDeleteAnimals, BatchUpdateAnimals |
|
40
|
|
|
from samples.tasks import BatchDeleteSamples, BatchUpdateSamples |
|
41
|
|
|
|
|
42
|
|
|
from .forms import SubmissionForm, ReloadForm |
|
43
|
|
|
from .helpers import is_target_in_message |
|
44
|
|
|
|
|
45
|
|
|
# Get an instance of a logger |
|
46
|
|
|
logger = logging.getLogger(__name__) |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
class CreateSubmissionView(LoginRequiredMixin, CreateView): |
|
50
|
|
|
form_class = SubmissionForm |
|
51
|
|
|
model = Submission |
|
52
|
|
|
|
|
53
|
|
|
# template name is derived from model position and views type. |
|
54
|
|
|
# in this case, ir will be 'image_app/submission_form.html' so |
|
55
|
|
|
# i need to clearly specify it |
|
56
|
|
|
template_name = "submissions/submission_form.html" |
|
57
|
|
|
|
|
58
|
|
|
def form_invalid(self, form): |
|
59
|
|
|
messages.error( |
|
60
|
|
|
self.request, |
|
61
|
|
|
message="Please correct the errors below", |
|
62
|
|
|
extra_tags="alert alert-dismissible alert-danger") |
|
63
|
|
|
|
|
64
|
|
|
return super(CreateSubmissionView, self).form_invalid(form) |
|
65
|
|
|
|
|
66
|
|
|
# add user to this object |
|
67
|
|
|
def form_valid(self, form): |
|
68
|
|
|
self.object = form.save(commit=False) |
|
69
|
|
|
self.object.owner = self.request.user |
|
70
|
|
|
|
|
71
|
|
|
# I will have a different loading function accordingly with data type |
|
72
|
|
|
if self.object.datasource_type == CRYOWEB_TYPE: |
|
73
|
|
|
# update object and force status |
|
74
|
|
|
self.object.message = "waiting for data loading" |
|
75
|
|
|
self.object.status = WAITING |
|
76
|
|
|
self.object.save() |
|
77
|
|
|
|
|
78
|
|
|
# a valid submission start a task |
|
79
|
|
|
res = import_from_cryoweb.delay(self.object.pk) |
|
80
|
|
|
logger.info( |
|
81
|
|
|
"Start cryoweb importing process with task %s" % res.task_id) |
|
82
|
|
|
|
|
83
|
|
|
# I will have a different loading function accordingly with data type |
|
84
|
|
|
elif self.object.datasource_type == CRB_ANIM_TYPE: |
|
85
|
|
|
# update object and force status |
|
86
|
|
|
self.object.message = "waiting for data loading" |
|
87
|
|
|
self.object.status = WAITING |
|
88
|
|
|
self.object.save() |
|
89
|
|
|
|
|
90
|
|
|
# create a task |
|
91
|
|
|
my_task = ImportCRBAnimTask() |
|
92
|
|
|
|
|
93
|
|
|
# a valid submission start a task |
|
94
|
|
|
res = my_task.delay(self.object.pk) |
|
95
|
|
|
logger.info( |
|
96
|
|
|
"Start crbanim importing process with task %s" % res.task_id) |
|
97
|
|
|
|
|
98
|
|
|
else: |
|
99
|
|
|
# update object and force status |
|
100
|
|
|
self.object.message = "waiting for data loading" |
|
101
|
|
|
self.object.status = WAITING |
|
102
|
|
|
self.object.save() |
|
103
|
|
|
|
|
104
|
|
|
# create a task |
|
105
|
|
|
my_task = ImportTemplateTask() |
|
106
|
|
|
|
|
107
|
|
|
# a valid submission start a task |
|
108
|
|
|
res = my_task.delay(self.object.pk) |
|
109
|
|
|
logger.info( |
|
110
|
|
|
"Start template importing process with task %s" % res.task_id) |
|
111
|
|
|
|
|
112
|
|
|
# a redirect to self.object.get_absolute_url() |
|
113
|
|
|
return HttpResponseRedirect(self.get_success_url()) |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
class MessagesSubmissionMixin(object): |
|
117
|
|
|
"""Display messages in SubmissionViews""" |
|
118
|
|
|
|
|
119
|
|
|
# https://stackoverflow.com/a/45696442 |
|
120
|
|
|
def get_context_data(self, **kwargs): |
|
121
|
|
|
data = super().get_context_data(**kwargs) |
|
122
|
|
|
|
|
123
|
|
|
# get the submission message |
|
124
|
|
|
message = self.submission.message |
|
125
|
|
|
|
|
126
|
|
|
# check if data are loaded or not |
|
127
|
|
|
if self.submission.status in [WAITING, SUBMITTED]: |
|
128
|
|
|
messages.warning( |
|
129
|
|
|
request=self.request, |
|
130
|
|
|
message=message, |
|
131
|
|
|
extra_tags="alert alert-dismissible alert-warning") |
|
132
|
|
|
|
|
133
|
|
|
elif self.submission.status in [ERROR, NEED_REVISION]: |
|
134
|
|
|
messages.error( |
|
135
|
|
|
request=self.request, |
|
136
|
|
|
message=message, |
|
137
|
|
|
extra_tags="alert alert-dismissible alert-danger") |
|
138
|
|
|
|
|
139
|
|
|
elif message is not None and message != '': |
|
140
|
|
|
messages.info( |
|
141
|
|
|
request=self.request, |
|
142
|
|
|
message=message, |
|
143
|
|
|
extra_tags="alert alert-dismissible alert-info") |
|
144
|
|
|
|
|
145
|
|
|
return data |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
class DetailSubmissionView(MessagesSubmissionMixin, OwnerMixin, DetailView): |
|
149
|
|
|
model = Submission |
|
150
|
|
|
template_name = "submissions/submission_detail.html" |
|
151
|
|
|
|
|
152
|
|
|
def get_context_data(self, **kwargs): |
|
153
|
|
|
# pass self.object to a new submission attribute in order to call |
|
154
|
|
|
# MessagesSubmissionMixin.get_context_data() |
|
155
|
|
|
self.submission = self.object |
|
156
|
|
|
|
|
157
|
|
|
# Call the base implementation first to get a context |
|
158
|
|
|
context = super(DetailSubmissionView, self).get_context_data(**kwargs) |
|
159
|
|
|
|
|
160
|
|
|
# add submission report to context |
|
161
|
|
|
validation_summary = construct_validation_message(self.submission) |
|
162
|
|
|
|
|
163
|
|
|
# HINT: is this computational intensive? |
|
164
|
|
|
context["validation_summary"] = validation_summary |
|
165
|
|
|
|
|
166
|
|
|
return context |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
class SubmissionValidationSummaryView(OwnerMixin, DetailView): |
|
170
|
|
|
model = Submission |
|
171
|
|
|
template_name = "submissions/submission_validation_summary.html" |
|
172
|
|
|
|
|
173
|
|
|
def get_context_data(self, **kwargs): |
|
174
|
|
|
context = super().get_context_data(**kwargs) |
|
175
|
|
|
summary_type = self.kwargs['type'] |
|
176
|
|
|
try: |
|
177
|
|
|
validation_summary = self.object.validationsummary_set\ |
|
178
|
|
|
.get(type=summary_type) |
|
179
|
|
|
context['validation_summary'] = validation_summary |
|
180
|
|
|
|
|
181
|
|
|
editable = list() |
|
182
|
|
|
|
|
183
|
|
|
for message in validation_summary.messages: |
|
184
|
|
|
message = ast.literal_eval(message) |
|
185
|
|
|
|
|
186
|
|
|
if 'offending_column' not in message: |
|
187
|
|
|
txt = ("Old validation results, please re-run validation" |
|
188
|
|
|
" step!") |
|
189
|
|
|
logger.warning(txt) |
|
190
|
|
|
messages.warning( |
|
191
|
|
|
request=self.request, |
|
192
|
|
|
message=txt, |
|
193
|
|
|
extra_tags="alert alert-dismissible alert-warning") |
|
194
|
|
|
editable.append(False) |
|
195
|
|
|
|
|
196
|
|
|
elif (uid2biosample(message['offending_column']) in |
|
197
|
|
|
[val for sublist in VALIDATION_MESSAGES_ATTRIBUTES for |
|
198
|
|
|
val in sublist]): |
|
199
|
|
|
logger.debug( |
|
200
|
|
|
"%s is editable" % message['offending_column']) |
|
201
|
|
|
editable.append(True) |
|
202
|
|
|
else: |
|
203
|
|
|
logger.debug( |
|
204
|
|
|
"%s is not editable" % message['offending_column']) |
|
205
|
|
|
editable.append(False) |
|
206
|
|
|
|
|
207
|
|
|
context['editable'] = editable |
|
208
|
|
|
|
|
209
|
|
|
except ObjectDoesNotExist: |
|
210
|
|
|
context['validation_summary'] = None |
|
211
|
|
|
|
|
212
|
|
|
context['submission'] = Submission.objects.get(pk=self.kwargs['pk']) |
|
213
|
|
|
|
|
214
|
|
|
return context |
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
class EditSubmissionMixin(): |
|
218
|
|
|
"""A mixin to deal with Updates, expecially when searching ListViews""" |
|
219
|
|
|
|
|
220
|
|
View Code Duplication |
def dispatch(self, request, *args, **kwargs): |
|
|
|
|
|
|
221
|
|
|
handler = super(EditSubmissionMixin, self).dispatch( |
|
222
|
|
|
request, *args, **kwargs) |
|
223
|
|
|
|
|
224
|
|
|
# here I've done get_queryset. Check for submission status |
|
225
|
|
|
if hasattr(self, "submission") and not self.submission.can_edit(): |
|
226
|
|
|
message = "Cannot edit submission: current status is: %s" % ( |
|
227
|
|
|
self.submission.get_status_display()) |
|
228
|
|
|
|
|
229
|
|
|
logger.warning(message) |
|
230
|
|
|
messages.warning( |
|
231
|
|
|
request=self.request, |
|
232
|
|
|
message=message, |
|
233
|
|
|
extra_tags="alert alert-dismissible alert-warning") |
|
234
|
|
|
|
|
235
|
|
|
return redirect(self.submission.get_absolute_url()) |
|
236
|
|
|
|
|
237
|
|
|
return handler |
|
238
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
class SubmissionValidationSummaryFixErrorsView( |
|
241
|
|
|
EditSubmissionMixin, OwnerMixin, ListView): |
|
242
|
|
|
template_name = "submissions/submission_validation_summary_fix_errors.html" |
|
243
|
|
|
|
|
244
|
|
|
def get_queryset(self): |
|
245
|
|
|
"""Define columns that need to change""" |
|
246
|
|
|
|
|
247
|
|
|
self.submission = get_object_or_404( |
|
248
|
|
|
Submission, |
|
249
|
|
|
pk=self.kwargs['pk'], |
|
250
|
|
|
owner=self.request.user) |
|
251
|
|
|
|
|
252
|
|
|
self.summary_type = self.kwargs['type'] |
|
253
|
|
|
self.validation_summary = ValidationSummary.objects.get( |
|
254
|
|
|
submission=self.submission, type=self.summary_type) |
|
255
|
|
|
self.message = ast.literal_eval(self.validation_summary.messages[ |
|
256
|
|
|
int(self.kwargs['message_counter']) |
|
257
|
|
|
]) |
|
258
|
|
|
self.offending_column = uid2biosample( |
|
259
|
|
|
self.message['offending_column']) |
|
260
|
|
|
self.show_units = True |
|
261
|
|
|
if is_target_in_message(self.message['message'], |
|
262
|
|
|
UNITS_VALIDATION_MESSAGES): |
|
263
|
|
|
self.units = [unit.name for unit in TIME_UNITS] |
|
264
|
|
|
if self.offending_column == 'animal_age_at_collection': |
|
265
|
|
|
self.offending_column += "_units" |
|
266
|
|
|
|
|
267
|
|
|
elif is_target_in_message(self.message['message'], |
|
268
|
|
|
VALUES_VALIDATION_MESSAGES): |
|
269
|
|
|
if self.offending_column == 'storage': |
|
270
|
|
|
self.units = [unit.name for unit in SAMPLE_STORAGE] |
|
271
|
|
|
elif self.offending_column == 'storage_processing': |
|
272
|
|
|
self.units = [unit.name for unit in SAMPLE_STORAGE_PROCESSING] |
|
273
|
|
|
elif self.offending_column == 'collection_place_accuracy' or \ |
|
274
|
|
|
self.offending_column == 'birth_location_accuracy': |
|
275
|
|
|
self.units = [unit.name for unit in ACCURACIES] |
|
276
|
|
|
else: |
|
277
|
|
|
self.show_units = False |
|
278
|
|
|
self.units = None |
|
279
|
|
|
if self.summary_type == 'animal': |
|
280
|
|
|
return Animal.objects.filter(id__in=self.message['ids']) |
|
281
|
|
|
elif self.summary_type == 'sample': |
|
282
|
|
|
return Sample.objects.filter(id__in=self.message['ids']) |
|
283
|
|
|
|
|
284
|
|
|
def get_context_data(self, **kwargs): |
|
285
|
|
|
# Call the base implementation first to get a context |
|
286
|
|
|
context = super( |
|
287
|
|
|
SubmissionValidationSummaryFixErrorsView, self |
|
288
|
|
|
).get_context_data(**kwargs) |
|
289
|
|
|
|
|
290
|
|
|
# add submission to context |
|
291
|
|
|
context["message"] = self.message |
|
292
|
|
|
context["type"] = self.summary_type |
|
293
|
|
|
context['attribute_to_edit'] = self.offending_column |
|
294
|
|
|
for attributes in VALIDATION_MESSAGES_ATTRIBUTES: |
|
295
|
|
|
if self.offending_column in attributes: |
|
296
|
|
|
context['attributes_to_show'] = [ |
|
297
|
|
|
attr for attr in attributes if |
|
298
|
|
|
attr != self.offending_column |
|
299
|
|
|
] |
|
300
|
|
|
context['submission'] = self.submission |
|
301
|
|
|
context['show_units'] = self.show_units |
|
302
|
|
|
if self.units: |
|
303
|
|
|
context['units'] = self.units |
|
304
|
|
|
return context |
|
305
|
|
|
|
|
306
|
|
|
|
|
307
|
|
|
# a detail view since I need to operate on a submission object |
|
308
|
|
|
# HINT: rename to a more informative name? |
|
309
|
|
|
class EditSubmissionView( |
|
310
|
|
|
EditSubmissionMixin, MessagesSubmissionMixin, OwnerMixin, ListView): |
|
311
|
|
|
template_name = "submissions/submission_edit.html" |
|
312
|
|
|
paginate_by = 10 |
|
313
|
|
|
|
|
314
|
|
|
def get_queryset(self): |
|
315
|
|
|
"""Subsetting names relying submission id""" |
|
316
|
|
|
|
|
317
|
|
|
self.submission = get_object_or_404( |
|
318
|
|
|
Submission, |
|
319
|
|
|
pk=self.kwargs['pk'], |
|
320
|
|
|
owner=self.request.user) |
|
321
|
|
|
|
|
322
|
|
|
# unknown animals should be removed from a submission. They have no |
|
323
|
|
|
# data in animal table nor sample |
|
324
|
|
|
return Name.objects.select_related( |
|
325
|
|
|
"validationresult", |
|
326
|
|
|
"animal", |
|
327
|
|
|
"sample").filter( |
|
328
|
|
|
Q(submission=self.submission) & ( |
|
329
|
|
|
Q(animal__isnull=False) | Q(sample__isnull=False)) |
|
330
|
|
|
).order_by('id') |
|
331
|
|
|
|
|
332
|
|
|
def get_context_data(self, **kwargs): |
|
333
|
|
|
# Call the base implementation first to get a context |
|
334
|
|
|
context = super(EditSubmissionView, self).get_context_data(**kwargs) |
|
335
|
|
|
|
|
336
|
|
|
# add submission to context |
|
337
|
|
|
context["submission"] = self.submission |
|
338
|
|
|
|
|
339
|
|
|
return context |
|
340
|
|
|
|
|
341
|
|
|
|
|
342
|
|
|
class ListSubmissionsView(OwnerMixin, ListView): |
|
343
|
|
|
model = Submission |
|
344
|
|
|
template_name = "submissions/submission_list.html" |
|
345
|
|
|
ordering = ['-created_at'] |
|
346
|
|
|
paginate_by = 10 |
|
347
|
|
|
|
|
348
|
|
|
|
|
349
|
|
|
class ReloadSubmissionView(OwnerMixin, UpdateView): |
|
350
|
|
|
form_class = ReloadForm |
|
351
|
|
|
model = Submission |
|
352
|
|
|
template_name = 'submissions/submission_reload.html' |
|
353
|
|
|
|
|
354
|
|
|
def form_invalid(self, form): |
|
355
|
|
|
messages.error( |
|
356
|
|
|
self.request, |
|
357
|
|
|
message="Please correct the errors below", |
|
358
|
|
|
extra_tags="alert alert-dismissible alert-danger") |
|
359
|
|
|
|
|
360
|
|
|
return super(ReloadSubmissionView, self).form_invalid(form) |
|
361
|
|
|
|
|
362
|
|
|
def form_valid(self, form): |
|
363
|
|
|
self.object = form.save(commit=False) |
|
364
|
|
|
|
|
365
|
|
|
# update object and force status |
|
366
|
|
|
self.object.message = "waiting for data loading" |
|
367
|
|
|
self.object.status = WAITING |
|
368
|
|
|
self.object.save() |
|
369
|
|
|
|
|
370
|
|
|
# HINT: can I change datasource type? |
|
371
|
|
|
|
|
372
|
|
|
# call the proper method |
|
373
|
|
|
if self.object.datasource_type == CRYOWEB_TYPE: |
|
374
|
|
|
# a valid submission start a task |
|
375
|
|
|
res = import_from_cryoweb.delay(self.object.pk) |
|
376
|
|
|
logger.info( |
|
377
|
|
|
"Start cryoweb reload process with task %s" % res.task_id) |
|
378
|
|
|
|
|
379
|
|
|
elif self.object.datasource_type == CRB_ANIM_TYPE: |
|
380
|
|
|
# a valid submission start a task |
|
381
|
|
|
my_task = ImportCRBAnimTask() |
|
382
|
|
|
|
|
383
|
|
|
# a valid submission start a task |
|
384
|
|
|
res = my_task.delay(self.object.pk) |
|
385
|
|
|
logger.info( |
|
386
|
|
|
"Start crbanim reload process with task %s" % res.task_id) |
|
387
|
|
|
|
|
388
|
|
|
else: |
|
389
|
|
|
# a valid submission start a task |
|
390
|
|
|
my_task = ImportTemplateTask() |
|
391
|
|
|
|
|
392
|
|
|
# a valid submission start a task |
|
393
|
|
|
res = my_task.delay(self.object.pk) |
|
394
|
|
|
logger.info( |
|
395
|
|
|
"Start template reload process with task %s" % res.task_id) |
|
396
|
|
|
|
|
397
|
|
|
# a redirect to self.object.get_absolute_url() |
|
398
|
|
|
return HttpResponseRedirect(self.get_success_url()) |
|
399
|
|
|
|
|
400
|
|
|
|
|
401
|
|
|
class DeleteSubmissionMixin(): |
|
402
|
|
|
"""Prevent a delete relying on statuses""" |
|
403
|
|
|
|
|
404
|
|
View Code Duplication |
def dispatch(self, request, *args, **kwargs): |
|
|
|
|
|
|
405
|
|
|
handler = super(DeleteSubmissionMixin, self).dispatch( |
|
406
|
|
|
request, *args, **kwargs) |
|
407
|
|
|
|
|
408
|
|
|
# here I've done get_queryset. Check for submission status |
|
409
|
|
|
if hasattr(self, "object") and not self.object.can_edit(): |
|
410
|
|
|
message = "Cannot delete %s: submission status is: %s" % ( |
|
411
|
|
|
self.object, self.object.get_status_display()) |
|
412
|
|
|
|
|
413
|
|
|
logger.warning(message) |
|
414
|
|
|
messages.warning( |
|
415
|
|
|
request=self.request, |
|
416
|
|
|
message=message, |
|
417
|
|
|
extra_tags="alert alert-dismissible alert-warning") |
|
418
|
|
|
|
|
419
|
|
|
return redirect(self.object.get_absolute_url()) |
|
420
|
|
|
|
|
421
|
|
|
return handler |
|
422
|
|
|
|
|
423
|
|
|
|
|
424
|
|
|
class BatchDeleteMixin( |
|
425
|
|
|
DeleteSubmissionMixin, OwnerMixin): |
|
426
|
|
|
|
|
427
|
|
|
model = Submission |
|
428
|
|
|
delete_type = None |
|
429
|
|
|
|
|
430
|
|
|
def get_context_data(self, **kwargs): |
|
431
|
|
|
"""Add custom values to template context""" |
|
432
|
|
|
|
|
433
|
|
|
context = super().get_context_data(**kwargs) |
|
434
|
|
|
|
|
435
|
|
|
context['delete_type'] = self.delete_type |
|
436
|
|
|
context['pk'] = self.object.id |
|
437
|
|
|
|
|
438
|
|
|
return context |
|
439
|
|
|
|
|
440
|
|
|
def post(self, request, *args, **kwargs): |
|
441
|
|
|
# get object (Submission) like BaseUpdateView does |
|
442
|
|
|
submission = self.get_object() |
|
443
|
|
|
|
|
444
|
|
|
# get arguments from post object |
|
445
|
|
|
pk = self.kwargs['pk'] |
|
446
|
|
|
keys_to_delete = set() |
|
447
|
|
|
|
|
448
|
|
|
# process all keys in form |
|
449
|
|
|
for key in request.POST['to_delete'].split('\n'): |
|
450
|
|
|
keys_to_delete.add(key.rstrip()) |
|
451
|
|
|
|
|
452
|
|
|
submission.message = 'waiting for batch delete to complete' |
|
453
|
|
|
submission.status = WAITING |
|
454
|
|
|
submission.save() |
|
455
|
|
|
|
|
456
|
|
|
if self.delete_type == 'Animals': |
|
457
|
|
|
# Batch delete task for animals |
|
458
|
|
|
my_task = BatchDeleteAnimals() |
|
459
|
|
|
summary_obj, created = ValidationSummary.objects.get_or_create( |
|
460
|
|
|
submission=submission, type='animal') |
|
461
|
|
|
|
|
462
|
|
|
elif self.delete_type == 'Samples': |
|
463
|
|
|
# Batch delete task for samples |
|
464
|
|
|
my_task = BatchDeleteSamples() |
|
465
|
|
|
summary_obj, created = ValidationSummary.objects.get_or_create( |
|
466
|
|
|
submission=submission, type='sample') |
|
467
|
|
|
|
|
468
|
|
|
# reset validation counters |
|
469
|
|
|
summary_obj.reset() |
|
|
|
|
|
|
470
|
|
|
res = my_task.delay(pk, [item for item in keys_to_delete]) |
|
|
|
|
|
|
471
|
|
|
|
|
472
|
|
|
logger.info( |
|
473
|
|
|
"Start %s batch delete with task %s" % ( |
|
474
|
|
|
self.delete_type, res.task_id)) |
|
475
|
|
|
|
|
476
|
|
|
return HttpResponseRedirect(reverse('submissions:detail', args=(pk,))) |
|
477
|
|
|
|
|
478
|
|
|
|
|
479
|
|
|
class DeleteAnimalsView(BatchDeleteMixin, DetailView): |
|
480
|
|
|
model = Submission |
|
481
|
|
|
template_name = 'submissions/submission_batch_delete.html' |
|
482
|
|
|
delete_type = 'Animals' |
|
483
|
|
|
|
|
484
|
|
|
|
|
485
|
|
|
class DeleteSamplesView(BatchDeleteMixin, DetailView): |
|
486
|
|
|
model = Submission |
|
487
|
|
|
template_name = 'submissions/submission_batch_delete.html' |
|
488
|
|
|
delete_type = 'Samples' |
|
489
|
|
|
|
|
490
|
|
|
|
|
491
|
|
|
class DeleteSubmissionView(DeleteSubmissionMixin, OwnerMixin, DeleteView): |
|
492
|
|
|
model = Submission |
|
493
|
|
|
template_name = "submissions/submission_confirm_delete.html" |
|
494
|
|
|
success_url = reverse_lazy('image_app:dashboard') |
|
495
|
|
|
|
|
496
|
|
|
# https://stackoverflow.com/a/39533619/4385116 |
|
497
|
|
|
def get_context_data(self, **kwargs): |
|
498
|
|
|
# determining related objects |
|
499
|
|
|
# TODO: move this to a custom AJAX call |
|
500
|
|
|
context = super().get_context_data(**kwargs) |
|
501
|
|
|
|
|
502
|
|
|
deletable_objects, model_count, protected = get_deleted_objects( |
|
503
|
|
|
[self.object]) |
|
504
|
|
|
|
|
505
|
|
|
# get only sample and animals from model_count |
|
506
|
|
|
info_deleted = {} |
|
507
|
|
|
|
|
508
|
|
|
items = ['animals', 'samples'] |
|
509
|
|
|
|
|
510
|
|
|
for item in items: |
|
511
|
|
|
if item in model_count: |
|
512
|
|
|
info_deleted[item] = model_count[item] |
|
513
|
|
|
|
|
514
|
|
|
# add info to context |
|
515
|
|
|
context['info_deleted'] = dict(info_deleted).items() |
|
516
|
|
|
|
|
517
|
|
|
return context |
|
518
|
|
|
|
|
519
|
|
|
# https://ccbv.co.uk/projects/Django/1.11/django.views.generic.edit/DeleteView/#delete |
|
520
|
|
|
def delete(self, request, *args, **kwargs): |
|
521
|
|
|
""" |
|
522
|
|
|
Add a message after calling base delete method |
|
523
|
|
|
""" |
|
524
|
|
|
|
|
525
|
|
|
httpresponseredirect = super().delete(request, *args, **kwargs) |
|
526
|
|
|
|
|
527
|
|
|
message = "Submission %s was successfully deleted" % self.object.title |
|
528
|
|
|
logger.info(message) |
|
529
|
|
|
|
|
530
|
|
|
messages.info( |
|
531
|
|
|
request=self.request, |
|
532
|
|
|
message=message, |
|
533
|
|
|
extra_tags="alert alert-dismissible alert-info") |
|
534
|
|
|
|
|
535
|
|
|
return httpresponseredirect |
|
536
|
|
|
|
|
537
|
|
|
|
|
538
|
|
|
class FixValidation(OwnerMixin, BaseUpdateView): |
|
539
|
|
|
model = Submission |
|
540
|
|
|
|
|
541
|
|
|
def post(self, request, **kwargs): |
|
542
|
|
|
# get object (Submission) like BaseUpdateView does |
|
543
|
|
|
submission = self.get_object() |
|
544
|
|
|
|
|
545
|
|
|
# Fetch all required ids from input names and use it as keys |
|
546
|
|
|
keys_to_fix = dict() |
|
547
|
|
|
for key_to_fix in request.POST: |
|
548
|
|
|
if 'to_edit' in key_to_fix: |
|
549
|
|
|
keys_to_fix[ |
|
550
|
|
|
int(re.search('to_edit(.*)', key_to_fix).groups()[0])] \ |
|
|
|
|
|
|
551
|
|
|
= request.POST[key_to_fix] |
|
552
|
|
|
|
|
553
|
|
|
pk = self.kwargs['pk'] |
|
554
|
|
|
record_type = self.kwargs['record_type'] |
|
555
|
|
|
attribute_to_edit = self.kwargs['attribute_to_edit'] |
|
556
|
|
|
|
|
557
|
|
|
submission.message = "waiting for data updating" |
|
558
|
|
|
submission.status = WAITING |
|
559
|
|
|
submission.save() |
|
560
|
|
|
|
|
561
|
|
|
# Update validation summary |
|
562
|
|
|
summary_obj, created = ValidationSummary.objects.get_or_create( |
|
563
|
|
|
submission=submission, type=record_type) |
|
564
|
|
|
summary_obj.submission = submission |
|
565
|
|
|
summary_obj.reset() |
|
566
|
|
|
|
|
567
|
|
|
# create a task |
|
568
|
|
|
if record_type == 'animal': |
|
569
|
|
|
my_task = BatchUpdateAnimals() |
|
570
|
|
|
elif record_type == 'sample': |
|
571
|
|
|
my_task = BatchUpdateSamples() |
|
572
|
|
|
else: |
|
573
|
|
|
return HttpResponseRedirect( |
|
574
|
|
|
reverse('submissions:detail', args=(pk,))) |
|
575
|
|
|
|
|
576
|
|
|
# a valid submission start a task |
|
577
|
|
|
res = my_task.delay(pk, keys_to_fix, attribute_to_edit) |
|
578
|
|
|
logger.info( |
|
579
|
|
|
"Start fix validation process with task %s" % res.task_id) |
|
580
|
|
|
return HttpResponseRedirect(reverse('submissions:detail', args=(pk,))) |
|
581
|
|
|
|