ExamsForm   A
last analyzed

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 3 1
A save() 0 5 1
1
# coding: utf-8
2
from dj_diabetes.models.exams import Examinations, ExaminationDetails
3
4
from django import forms
5
from django.forms.models import inlineformset_factory
6
7
8
class ExamsForm(forms.ModelForm):
9
    """
10
        Exams Form
11
    """
12
    # to " suit " the HTML textearea
13
    comments = forms.CharField(widget=forms.Textarea(
14
        {'class': 'form-control', 'rows': '3'}))
15
    date_examinations = forms.DateField(widget=forms.TextInput(
16
        attrs={'class': 'form-control'}))
17
    hour_examinations = forms.TimeField(widget=forms.TextInput(
18
        attrs={'class': 'form-control'}))
19
20
    def save(self, user=None):
21
        self.myobject = super(ExamsForm, self).save(commit=False)
22
        self.myobject.user = user
23
        self.myobject.save()
24
        return self.myobject
25
26
    class Meta:
27
        model = Examinations
28
        fields = ['examination_types', 'comments',
29
                  'date_examinations', 'hour_examinations']
30
        exclude = ('user',)
31
32
    def __init__(self, *args, **kwargs):
33
        super(ExamsForm, self).__init__(*args, **kwargs)
34
        self.fields['examination_types'].widget.attrs['class'] = 'form-control'
35
36
37
class ExamDetailsForm(forms.ModelForm):
38
    """
39
        Details of Exams Form
40
    """
41
    title = forms.CharField(widget=forms.TextInput(
42
        attrs={'class': 'form-control'}))
43
    value = forms.DecimalField(widget=forms.TextInput(
44
        attrs={'class': 'form-control', 'type': 'number'}))
45
46
    class Meta:
47
        model = ExaminationDetails
48
        fields = ['title', 'value']
49
50
51
# a formset based on the model of the Mother and Child + 2 new empty lines
52
my_fields = ('examination', 'title', 'value')
53
ExamDetailsFormSet = inlineformset_factory(Examinations,
54
                                           ExaminationDetails,
55
                                           fields=my_fields, extra=2)
56