forms.py 929 Bytes
Newer Older
Jamesie Pic's avatar
Jamesie Pic committed
1
2
3
4
from django import forms

from datetimewidget.widgets import DateWidget

Nicolas Joyard's avatar
Nicolas Joyard committed
5
from memopol_themes.models import Theme
Jamesie Pic's avatar
Jamesie Pic committed
6
7
8
9
from .models import Position


class PositionForm(forms.ModelForm):
Nicolas Joyard's avatar
Nicolas Joyard committed
10
11
12
13
14
15
    themes = forms.models.ModelMultipleChoiceField(
        queryset=Theme.objects.all(),
        required=False,
        widget=forms.CheckboxSelectMultiple
    )

Jamesie Pic's avatar
Jamesie Pic committed
16
17
    class Meta:
        model = Position
18
19
        fields = ['representative', 'link', 'datetime', 'themes', 'title',
                  'kind', 'text']
Nicolas Joyard's avatar
Nicolas Joyard committed
20

Jamesie Pic's avatar
Jamesie Pic committed
21
22
23
        widgets = {
            'datetime': DateWidget(
                usel10n=True,
Nicolas Joyard's avatar
Nicolas Joyard committed
24
25
                bootstrap_version=3
            )
Jamesie Pic's avatar
Jamesie Pic committed
26
        }
Nicolas Joyard's avatar
Nicolas Joyard committed
27
28
29
30
31
32
33
34
35
36
37
38

    def save(self, commit=True):
        position = super(PositionForm, self).save(commit=False)

        if commit:
            position.save()

        if position.pk:
            position.themes = self.cleaned_data.get('themes')
            self.save_m2m()

        return position