UserProfileForm   A
last analyzed

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 10
1
# coding: utf-8
2
from dj_diabetes.models import UserProfile, Preferences
3
4
from django import forms
5
from django.views.generic.edit import FormMixin
6
7
8
# Function
9
10
11
def pref_filter(filter):
12
    """
13
        get the value we have in the Preferences model for a given key
14
    """
15
    choices = Preferences.objects.filter(key=filter)
16
    data = ()
17
    all_data = ()
18
    for choice in choices:
19
        data = (int(choice.value), choice.title)
20
        all_data = (data,) + all_data
21
    return all_data
22
23
24
class UserProfileForm(forms.ModelForm):
25
26
    name = forms.CharField(widget=forms.TextInput(
27
        {'class': 'form-control'}))
28
    birth_date = forms.DateField(widget=forms.TextInput(
29
        attrs={'class': 'form-control'}))
30
    phone = forms.CharField(widget=forms.TextInput(
31
        {'type': 'tel', 'class': 'form-control'}))
32
    address = forms.CharField(widget=forms.Textarea(
33
        {'class': 'form-control', 'rows': '3'}))
34
    zipcode = forms.CharField(widget=forms.TextInput(
35
        attrs={'class': 'form-control'}))
36
    town = forms.CharField(widget=forms.TextInput(
37
        attrs={'class': 'form-control'}))
38
39
    class Meta:
40
        model = UserProfile
41
        fields = ['name', 'birth_date', 'phone',
42
                  'address', 'zipcode', 'town']
43
44
45
class UserInstanceMixin(FormMixin):
46
47
    def get_form(self, form_class=None):
48
        form = super(UserInstanceMixin, self).get_form(form_class)
49
        form.instance.user = self.request.user
50
        return form
51