Completed
Pull Request — master (#41)
by
unknown
05:48
created

submissions.views.fix_validation()   B

Complexity

Conditions 5

Size

Total Lines 42
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

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