Completed
Pull Request — master (#41)
by
unknown
06:35
created

submissions.views.FixValidation.post()   B

Complexity

Conditions 5

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 38
rs 8.7413
c 0
b 0
f 0
cc 5
nop 3
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 import View
19
from django.views.generic import (
20
    CreateView, DetailView, ListView, UpdateView, DeleteView)
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 BatchUpdateAnimals
40
from samples.tasks import 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
            editable = list()
181
            for message in validation_summary.messages:
182
                message = ast.literal_eval(message)
183
                if uid2biosample(message['offending_column']) in \
184
                        [val for sublist in VALIDATION_MESSAGES_ATTRIBUTES for
185
                         val in sublist]:
186
                    editable.append(True)
187
                else:
188
                    editable.append(False)
189
            context['editable'] = editable
190
        except ObjectDoesNotExist:
191
            context['validation_summary'] = None
192
        context['submission'] = Submission.objects.get(pk=self.kwargs['pk'])
193
        return context
194
195
196
class SubmissionValidationSummaryFixErrorsView(OwnerMixin, ListView):
197
    template_name = "submissions/submission_validation_summary_fix_errors.html"
198
199
    def get_queryset(self):
200
        self.summary_type = self.kwargs['type']
201
        self.submission = Submission.objects.get(pk=self.kwargs['pk'])
202
        self.validation_summary = ValidationSummary.objects.get(
203
            submission=self.submission, type=self.summary_type)
204
        self.message = ast.literal_eval(self.validation_summary.messages[
205
                                            int(self.kwargs['message_counter'])
206
                                        ])
207
        self.offending_column = uid2biosample(
208
            self.message['offending_column'])
209
        self.show_units = True
210
        if is_target_in_message(self.message['message'],
211
                                UNITS_VALIDATION_MESSAGES):
212
            self.units = [unit.name for unit in TIME_UNITS]
213
            if self.offending_column == 'animal_age_at_collection':
214
                self.offending_column += "_units"
215
216
        elif is_target_in_message(self.message['message'],
217
                                  VALUES_VALIDATION_MESSAGES):
218
            if self.offending_column == 'storage':
219
                self.units = [unit.name for unit in SAMPLE_STORAGE]
220
            elif self.offending_column == 'storage_processing':
221
                self.units = [unit.name for unit in SAMPLE_STORAGE_PROCESSING]
222
            elif self.offending_column == 'collection_place_accuracy' or \
223
                    self.offending_column == 'birth_location_accuracy':
224
                self.units = [unit.name for unit in ACCURACIES]
225
        else:
226
            self.show_units = False
227
            self.units = None
228
        if self.summary_type == 'animal':
229
            return Animal.objects.filter(id__in=self.message['ids'])
230
        elif self.summary_type == 'sample':
231
            return Sample.objects.filter(id__in=self.message['ids'])
232
233
    def get_context_data(self, **kwargs):
234
        # Call the base implementation first to get a context
235
        context = super(
236
            SubmissionValidationSummaryFixErrorsView, self
237
        ).get_context_data(**kwargs)
238
239
        # add submission to context
240
        context["message"] = self.message
241
        context["type"] = self.summary_type
242
        context['attribute_to_edit'] = self.offending_column
243
        for attributes in VALIDATION_MESSAGES_ATTRIBUTES:
244
            if self.offending_column in attributes:
245
                context['attributes_to_show'] = [
246
                    attr for attr in attributes if attr != self.offending_column
247
                ]
248
        context['submission'] = self.submission
249
        context['error_type'] = 'coordinate_check'
250
        context['show_units'] = self.show_units
251
        if self.units:
252
            context['units'] = self.units
253
        return context
254
255
256
# a detail view since I need to operate on a submission object
257
# HINT: rename to a more informative name?
258
class EditSubmissionView(MessagesSubmissionMixin, OwnerMixin, ListView):
259
    template_name = "submissions/submission_edit.html"
260
    paginate_by = 10
261
262
    def get_queryset(self):
263
        """Subsetting names relying submission id"""
264
        self.submission = get_object_or_404(
265
            Submission,
266
            pk=self.kwargs['pk'],
267
            owner=self.request.user)
268
269
        # unknown animals should be removed from a submission. They have no
270
        # data in animal table nor sample
271
        return Name.objects.select_related(
272
                "validationresult",
273
                "animal",
274
                "sample").filter(
275
            Q(submission=self.submission) & (
276
                Q(animal__isnull=False) | Q(sample__isnull=False))
277
            ).order_by('id')
278
279 View Code Duplication
    def dispatch(self, request, *args, **kwargs):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
280
        handler = super(EditSubmissionView, self).dispatch(
281
                request, *args, **kwargs)
282
283
        # here I've done get_queryset. Check for submission status
284
        if hasattr(self, "submission") and not self.submission.can_edit():
285
            message = "Cannot edit submission: current status is: %s" % (
286
                    self.submission.get_status_display())
287
288
            logger.warning(message)
289
            messages.warning(
290
                request=self.request,
291
                message=message,
292
                extra_tags="alert alert-dismissible alert-warning")
293
294
            return redirect(self.submission.get_absolute_url())
295
296
        return handler
297
298
    def get_context_data(self, **kwargs):
299
        # Call the base implementation first to get a context
300
        context = super(EditSubmissionView, self).get_context_data(**kwargs)
301
302
        # add submission to context
303
        context["submission"] = self.submission
304
305
        return context
306
307
308
class ListSubmissionsView(OwnerMixin, ListView):
309
    model = Submission
310
    template_name = "submissions/submission_list.html"
311
    ordering = ['-created_at']
312
    paginate_by = 10
313
314
315
class ReloadSubmissionView(OwnerMixin, UpdateView):
316
    form_class = ReloadForm
317
    model = Submission
318
    template_name = 'submissions/submission_reload.html'
319
320
    def form_invalid(self, form):
321
        messages.error(
322
            self.request,
323
            message="Please correct the errors below",
324
            extra_tags="alert alert-dismissible alert-danger")
325
326
        return super(ReloadSubmissionView, self).form_invalid(form)
327
328
    def form_valid(self, form):
329
        self.object = form.save(commit=False)
330
331
        # update object and force status
332
        self.object.message = "waiting for data loading"
333
        self.object.status = WAITING
334
        self.object.save()
335
336
        # HINT: can I change datasource type?
337
338
        # call the proper method
339
        if self.object.datasource_type == CRYOWEB_TYPE:
340
            # a valid submission start a task
341
            res = import_from_cryoweb.delay(self.object.pk)
342
            logger.info(
343
                "Start cryoweb reload process with task %s" % res.task_id)
344
345
        elif self.object.datasource_type == CRB_ANIM_TYPE:
346
            # a valid submission start a task
347
            my_task = ImportCRBAnimTask()
348
349
            # a valid submission start a task
350
            res = my_task.delay(self.object.pk)
351
            logger.info(
352
                "Start crbanim reload process with task %s" % res.task_id)
353
354
        else:
355
            # a valid submission start a task
356
            my_task = ImportTemplateTask()
357
358
            # a valid submission start a task
359
            res = my_task.delay(self.object.pk)
360
            logger.info(
361
                "Start template reload process with task %s" % res.task_id)
362
363
        # a redirect to self.object.get_absolute_url()
364
        return HttpResponseRedirect(self.get_success_url())
365
366
367
class DeleteSubmissionView(OwnerMixin, DeleteView):
368
    model = Submission
369
    template_name = "submissions/submission_confirm_delete.html"
370
    success_url = reverse_lazy('image_app:dashboard')
371
372 View Code Duplication
    def dispatch(self, request, *args, **kwargs):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
373
        handler = super(DeleteSubmissionView, self).dispatch(
374
                request, *args, **kwargs)
375
376
        # here I've done get_queryset. Check for submission status
377
        if hasattr(self, "object") and not self.object.can_edit():
378
            message = "Cannot delete %s: submission status is: %s" % (
379
                    self.object, self.object.get_status_display())
380
381
            logger.warning(message)
382
            messages.warning(
383
                request=self.request,
384
                message=message,
385
                extra_tags="alert alert-dismissible alert-warning")
386
387
            return redirect(self.object.get_absolute_url())
388
389
        return handler
390
391
    # https://stackoverflow.com/a/39533619/4385116
392
    def get_context_data(self, **kwargs):
393
        # determining related objects
394
        # TODO: move this to a custom AJAX call
395
        context = super().get_context_data(**kwargs)
396
397
        deletable_objects, model_count, protected = get_deleted_objects(
398
            [self.object])
399
400
        # get only sample and animals from model_count
401
        info_deleted = {}
402
403
        items = ['animals', 'samples']
404
405
        for item in items:
406
            if item in model_count:
407
                info_deleted[item] = model_count[item]
408
409
        # add info to context
410
        context['info_deleted'] = dict(info_deleted).items()
411
412
        return context
413
414
    # https://ccbv.co.uk/projects/Django/1.11/django.views.generic.edit/DeleteView/#delete
415
    def delete(self, request, *args, **kwargs):
416
        """
417
        Add a message after calling base delete method
418
        """
419
420
        httpresponseredirect = super().delete(request, *args, **kwargs)
421
422
        message = "Submission %s was successfully deleted" % self.object.title
423
        logger.info(message)
424
425
        messages.info(
426
            request=self.request,
427
            message=message,
428
            extra_tags="alert alert-dismissible alert-info")
429
430
        return httpresponseredirect
431
432
433
class FixValidation(View, OwnerMixin):
434
    def post(self, request, **kwargs):
435
        # Fetch all required ids from input names and use it as keys
436
        keys_to_fix = dict()
437
        for key_to_fix in request.POST:
438
            if 'to_edit' in key_to_fix:
439
                keys_to_fix[
440
                    int(re.search('to_edit(.*)', key_to_fix).groups()[0])] \
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable int does not seem to be defined.
Loading history...
441
                    = request.POST[key_to_fix]
442
443
        pk = self.kwargs['pk']
444
        record_type = self.kwargs['record_type']
445
        attribute_to_edit = self.kwargs['attribute_to_edit']
446
447
        submission = Submission.objects.get(pk=pk)
448
        submission.message = "waiting for data updating"
449
        submission.status = WAITING
450
        submission.save()
451
452
        # Update validation summary
453
        summary_obj, created = ValidationSummary.objects.get_or_create(
454
            submission=submission, type=record_type)
455
        summary_obj.submission = submission
456
        summary_obj.reset()
457
458
        # create a task
459
        if record_type == 'animal':
460
            my_task = BatchUpdateAnimals()
461
        elif record_type == 'sample':
462
            my_task = BatchUpdateSamples()
463
        else:
464
            return HttpResponseRedirect(
465
                reverse('submissions:detail', args=(pk,)))
466
467
        # a valid submission start a task
468
        res = my_task.delay(pk, keys_to_fix, attribute_to_edit)
469
        logger.info(
470
            "Start fix validation process with task %s" % res.task_id)
471
        return HttpResponseRedirect(reverse('submissions:detail', args=(pk,)))
472