Completed
Push — master ( 742d10...04a923 )
by Fox
02:38
created

MealsForm.__init__()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 3
Ratio 100 %

Importance

Changes 0
Metric Value
dl 3
loc 3
rs 10
c 0
b 0
f 0
cc 1
1
# coding: utf-8
2
from dj_diabetes.models import UserProfile, Preferences
3
from dj_diabetes.models.appointments import Appointments
4
from dj_diabetes.models.exams import Examinations, ExaminationDetails
5
from dj_diabetes.models.glucoses import Glucoses
6
from dj_diabetes.models.issues import Issues
7
from dj_diabetes.models.meals import Meals
8
from dj_diabetes.models.sports import Exercises
9
from dj_diabetes.models.weights import Weights
10
11
from django import forms
12
from django.conf import settings
13
from django.forms.models import inlineformset_factory
14
from django.views.generic.edit import FormMixin
15
16
17
# Function
18
19
20
def pref_filter(filter):
21
    """
22
        get the value we have in the Preferences model for a given key
23
    """
24
    choices = Preferences.objects.filter(key=filter)
25
    data = ()
26
    all_data = ()
27
    for choice in choices:
28
        data = (int(choice.value), choice.title)
29
        all_data = (data,) + all_data
30
    return all_data
31
32
33
class UserProfileForm(forms.ModelForm):
34
35
    name = forms.CharField(widget=forms.TextInput(
36
        {'class': 'form-control'}))
37
    birth_date = forms.DateField(widget=forms.TextInput(
38
        attrs={'class': 'form-control'}))
39
    phone = forms.CharField(widget=forms.TextInput(
40
        {'type': 'tel', 'class': 'form-control'}))
41
    address = forms.CharField(widget=forms.Textarea(
42
        {'class': 'form-control', 'rows': '3'}))
43
    zipcode = forms.CharField(widget=forms.TextInput(
44
        attrs={'class': 'form-control'}))
45
    town = forms.CharField(widget=forms.TextInput(
46
        attrs={'class': 'form-control'}))
47
48
    class Meta:
49
        model = UserProfile
50
        fields = ['name', 'birth_date', 'phone',
51
                  'address', 'zipcode', 'town']
52
53
54
class GlucosesForm(forms.ModelForm):
55
    """
56
        glucoses Form
57
    """
58
    glucose = forms.IntegerField(widget=forms.TextInput(
59
        attrs={'class': 'form-control', 'type': 'number'}))
60
    # get the list of pref to get the value in the dropdown
61
    insulin = forms.IntegerField(widget=forms.TextInput(
62
        attrs={'class': 'form-control', 'type': 'number'}))
63
    moment = forms.ChoiceField(widget=forms.Select(
64
        attrs={'class': 'form-control'}))
65
    # to " suit " the HTML textearea
66
    comment = forms.CharField(widget=forms.Textarea(
67
        {'class': 'form-control', 'rows': '3'}))
68
    date_glucoses = forms.DateField(widget=forms.TextInput(
69
        {'class': 'form-control'}))
70
    hour_glucoses = forms.TimeField(widget=forms.TextInput(
71
        {'class': 'form-control'}))
72
73
    class Meta:
74
        model = Glucoses
75
        # Do the user uses insulin ?
76
        if settings.DJ_DIABETES['insulin'] is True:
77
            fields = ['moment', 'comment', 'glucose', 'insulin',
78
                      'date_glucoses', 'hour_glucoses']
79
        else:
80
            fields = ['moment', 'comment', 'glucose',
81
                      'date_glucoses', 'hour_glucoses']
82
83
    def __init__(self, *args, **kwargs):
84
        super(GlucosesForm, self).__init__(*args, **kwargs)
85
        self.fields['moment'].choices = pref_filter("moment")
86
87
88
class AppointmentsForm(forms.ModelForm):
89
    """
90
        Appointments Form
91
    """
92
    # to " suit " the HTML textearea
93
    title = forms.CharField(widget=forms.TextInput(
94
        attrs={'class': 'form-control'}))
95
    body = forms.CharField(widget=forms.Textarea(
96
        attrs={'class': 'form-control', 'rows': '3'}))
97
    recall_one_duration = forms.IntegerField(widget=forms.TextInput(
98
        attrs={'class': 'form-control', 'type': 'number'}))
99
    recall_two_duration = forms.IntegerField(widget=forms.TextInput(
100
        attrs={'class': 'form-control', 'type': 'number'}))
101
    recall_one_unit = forms.IntegerField(widget=forms.TextInput(
102
        attrs={'class': 'form-control', 'type': 'number'}))
103
    recall_two_unit = forms.IntegerField(widget=forms.TextInput(
104
        attrs={'class': 'form-control', 'type': 'number'}))
105
    date_appointments = forms.DateField(widget=forms.TextInput(
106
        {'class': 'form-control'}))
107
    hour_appointments = forms.TimeField(widget=forms.TextInput(
108
        {'class': 'form-control'}))
109
110
    class Meta:
111
        model = Appointments
112
        fields = ['appointment_types', 'title', 'body',
113
                  'date_appointments', 'hour_appointments',
114
                  'recall_one_duration', 'recall_two_duration',
115
                  'recall_one_unit', 'recall_two_unit']
116
117
    def __init__(self, *args, **kwargs):
118
        super(AppointmentsForm, self).__init__(*args, **kwargs)
119
        self.fields['appointment_types'].widget.attrs['class'] = 'form-control'
120
121
122
class IssuesForm(forms.ModelForm):
123
    """
124
        Issues Form
125
    """
126
    question_to = forms.CharField(widget=forms.TextInput(
127
        {'class': 'form-control'}))
128
    # to " suit " the HTML textearea
129
    question = forms.CharField(widget=forms.Textarea(
130
        {'class': 'form-control', 'rows': '3'}))
131
    # to " suit " the HTML textearea
132
    answer = forms.CharField(widget=forms.Textarea(
133
        {'class': 'form-control', 'rows': '3'}))
134
    date_answer = forms.DateField(widget=forms.TextInput(
135
        attrs={'class': 'form-control'}))
136
137
    class Meta:
138
        model = Issues
139
        fields = ['question', 'question_to', 'answer', 'date_answer']
140
141
142
class WeightsForm(forms.ModelForm):
143
    """
144
        Weights Form
145
    """
146
    weight = forms.IntegerField(widget=forms.TextInput(
147
        {'class': 'form-control', 'type': 'number'}))
148
    date_weights = forms.DateField(widget=forms.TextInput(
149
        attrs={'class': 'form-control'}))
150
151
    class Meta:
152
        model = Weights
153
        fields = ['weight', 'date_weights']
154
155
156 View Code Duplication
class MealsForm(forms.ModelForm):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
157
    """
158
        Meals Form
159
    """
160
    # get the list of pref to get the value in the dropdown
161
    breakfast_lunch_diner = forms.ChoiceField(widget=forms.Select(
162
        attrs={'class': 'form-control'}))
163
    food = forms.CharField(widget=forms.Textarea(
164
        {'class': 'form-control', 'rows': '3'}))
165
    date_meals = forms.DateField(widget=forms.TextInput(
166
        {'class': 'form-control'}))
167
    hour_meals = forms.TimeField(widget=forms.TextInput(
168
        {'class': 'form-control'}))
169
170
    class Meta:
171
        model = Meals
172
        fields = ['food', 'breakfast_lunch_diner', 'date_meals', 'hour_meals']
173
174
    def __init__(self, *args, **kwargs):
175
        super(MealsForm, self).__init__(*args, **kwargs)
176
        self.fields['breakfast_lunch_diner'].choices = pref_filter("meal")
177
178
179 View Code Duplication
class ExercisesForm(forms.ModelForm):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
180
    """
181
        Exercises Form
182
    """
183
    comment = forms.CharField(widget=forms.Textarea(
184
        {'class': 'form-control', 'rows': '3'}))
185
    duration = forms.IntegerField(widget=forms.TextInput(
186
        {'class': 'form-control', 'type': 'number'}))
187
    date_exercises = forms.DateField(widget=forms.TextInput(
188
        attrs={'class': 'form-control'}))
189
    hour_exercises = forms.TimeField(widget=forms.TextInput(
190
        attrs={'class': 'form-control'}))
191
192
    class Meta:
193
        model = Exercises
194
        fields = ['sports', 'comment', 'duration',
195
                  'date_exercises', 'hour_exercises']
196
197
    def __init__(self, *args, **kwargs):
198
        super(ExercisesForm, self).__init__(*args, **kwargs)
199
        self.fields['sports'].widget.attrs['class'] = 'form-control'
200
201
202
class ExamsForm(forms.ModelForm):
203
    """
204
        Exams Form
205
    """
206
    # to " suit " the HTML textearea
207
    comments = forms.CharField(widget=forms.Textarea(
208
        {'class': 'form-control', 'rows': '3'}))
209
    date_examinations = forms.DateField(widget=forms.TextInput(
210
        attrs={'class': 'form-control'}))
211
    hour_examinations = forms.TimeField(widget=forms.TextInput(
212
        attrs={'class': 'form-control'}))
213
214
    def save(self, user=None):
215
        self.myobject = super(ExamsForm, self).save(commit=False)
216
        self.myobject.user = user
217
        self.myobject.save()
218
        return self.myobject
219
220
    class Meta:
221
        model = Examinations
222
        fields = ['examination_types', 'comments',
223
                  'date_examinations', 'hour_examinations']
224
        exclude = ('user',)
225
226
    def __init__(self, *args, **kwargs):
227
        super(ExamsForm, self).__init__(*args, **kwargs)
228
        self.fields['examination_types'].widget.attrs['class'] = 'form-control'
229
230
231
class ExamDetailsForm(forms.ModelForm):
232
    """
233
        Details of Exams Form
234
    """
235
    title = forms.CharField(widget=forms.TextInput(
236
        attrs={'class': 'form-control'}))
237
    value = forms.DecimalField(widget=forms.TextInput(
238
        attrs={'class': 'form-control', 'type': 'number'}))
239
240
    class Meta:
241
        model = ExaminationDetails
242
        fields = ['title', 'value']
243
244
# a formset based on the model of the Mother and Child + 2 new empty lines
245
my_fields = ('examination', 'title', 'value')
246
ExamDetailsFormSet = inlineformset_factory(Examinations,
247
                                           ExaminationDetails,
248
                                           fields=my_fields, extra=2)
249
250
251
class UserInstanceMixin(FormMixin):
252
253
    def get_form(self, form_class=None):
254
        form = super(UserInstanceMixin, self).get_form(form_class)
255
        form.instance.user = self.request.user
256
        return form
257