|
1
|
|
|
# coding: utf-8 |
|
2
|
|
|
from __future__ import unicode_literals |
|
3
|
|
|
|
|
4
|
|
|
import logging |
|
5
|
|
|
|
|
6
|
|
|
from django.urls import reverse |
|
7
|
|
|
from django.http import HttpResponseRedirect |
|
8
|
|
|
from django.views.generic import CreateView, UpdateView, DeleteView |
|
9
|
|
|
# dj_diabetes |
|
10
|
|
|
from dj_diabetes.models import InitMixin, SuccessMixin, PaginateMixin |
|
11
|
|
|
from dj_diabetes.views import LoginRequiredMixin |
|
12
|
|
|
from dj_diabetes.models.exams import Examinations |
|
13
|
|
|
from dj_diabetes.forms.exams import ExamsForm, ExamDetailsFormSet |
|
14
|
|
|
|
|
15
|
|
|
# Get an instance of a logger |
|
16
|
|
|
logger = logging.getLogger(__name__) |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class ExamsMixin(SuccessMixin): |
|
20
|
|
|
""" |
|
21
|
|
|
mixin for form_class and model settings |
|
22
|
|
|
""" |
|
23
|
|
|
form_class = ExamsForm |
|
24
|
|
|
model = Examinations |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class ExamsFormSetValid(object): |
|
28
|
|
|
""" |
|
29
|
|
|
mixin for formset validation |
|
30
|
|
|
""" |
|
31
|
|
|
def form_valid(self, form): |
|
32
|
|
|
|
|
33
|
|
|
formset = ExamDetailsFormSet((self.request.POST or None), |
|
34
|
|
|
instance=self.object) |
|
35
|
|
|
if formset.is_valid(): |
|
36
|
|
|
self.object = form.save(user=self.request.user) |
|
37
|
|
|
formset.instance = self.object |
|
38
|
|
|
formset.save() |
|
39
|
|
|
|
|
40
|
|
|
return HttpResponseRedirect(reverse('exams')) |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
class ExamsContextDataMixin(PaginateMixin): |
|
44
|
|
|
""" |
|
45
|
|
|
mixin for context data |
|
46
|
|
|
""" |
|
47
|
|
|
def get_context_data(self, **kw): |
|
48
|
|
|
context = super(ExamsContextDataMixin, self).get_context_data(**kw) |
|
49
|
|
|
if self.request.POST: |
|
50
|
|
|
context['examsdetails_form'] = ExamDetailsFormSet( |
|
51
|
|
|
self.request.POST) |
|
52
|
|
|
else: |
|
53
|
|
|
context['examsdetails_form'] = ExamDetailsFormSet( |
|
54
|
|
|
instance=self.object) |
|
55
|
|
|
return context |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
class ExamsCreateView(InitMixin, ExamsMixin, LoginRequiredMixin, |
|
59
|
|
|
ExamsContextDataMixin, ExamsFormSetValid, CreateView): |
|
60
|
|
|
""" |
|
61
|
|
|
to Create Exams |
|
62
|
|
|
""" |
|
63
|
|
|
template_name = "dj_diabetes/exams_form.html" |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
class ExamsUpdateView(ExamsMixin, LoginRequiredMixin, |
|
67
|
|
|
ExamsContextDataMixin, ExamsFormSetValid, UpdateView): |
|
68
|
|
|
""" |
|
69
|
|
|
to Edit Exams |
|
70
|
|
|
""" |
|
71
|
|
|
template_name = "dj_diabetes/exams_form.html" |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
class ExamsDeleteView(ExamsMixin, DeleteView): |
|
75
|
|
|
""" |
|
76
|
|
|
to Delete Examination Details |
|
77
|
|
|
""" |
|
78
|
|
|
template_name = 'dj_diabetes/confirm_delete.html' |
|
79
|
|
|
|