Completed
Pull Request — master (#41)
by Paolo
06:52
created

DeleteSubmissionView.get_context_data()   A

Complexity

Conditions 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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