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

CreateSubmissionView.form_invalid()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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