forms.py 1.07 KB
Newer Older
Jamesie Pic's avatar
Jamesie Pic committed
1
2
3
from django import forms

from datetimewidget.widgets import DateWidget
4
from dal.autocomplete import ModelSelect2Multiple
Jamesie Pic's avatar
Jamesie Pic committed
5

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


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

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

Jamesie Pic's avatar
Jamesie Pic committed
22
23
24
        widgets = {
            'datetime': DateWidget(
                usel10n=True,
Nicolas Joyard's avatar
Nicolas Joyard committed
25
                bootstrap_version=3
26
27
28
            ),
            'representatives': ModelSelect2Multiple(
                url='representative-autocomplete',
Nicolas Joyard's avatar
Nicolas Joyard committed
29
            )
Jamesie Pic's avatar
Jamesie Pic committed
30
        }
Nicolas Joyard's avatar
Nicolas Joyard committed
31
32
33
34
35
36
37
38
39
40
41
42

    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