forms.py 1.93 KB
Newer Older
christophe siraut's avatar
christophe siraut committed
1
from django import forms
Christophe Siraut's avatar
Christophe Siraut committed
2
from .models import UserProfile, Poll, Choice, Bulletin, Vote
Christophe Siraut's avatar
Christophe Siraut committed
3
from django.utils.translation import ugettext_lazy as _
4
from datetime import datetime, timedelta
Christophe Siraut's avatar
Christophe Siraut committed
5
from datetimewidget.widgets import DateTimeWidget
Christophe Siraut's avatar
Christophe Siraut committed
6 7 8

dateTimeOptions = {
    'weekStart': '1',
Christophe Siraut's avatar
Christophe Siraut committed
9
    'minView': '1',
10
    'minuteStep': '15',
Christophe Siraut's avatar
Christophe Siraut committed
11
    'startDate': datetime.today().date(),
Christophe Siraut's avatar
Christophe Siraut committed
12
    'todayHighlight': 'true',
Christophe Siraut's avatar
Christophe Siraut committed
13 14 15 16 17 18 19 20 21
}


class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('email_notifications',)

class DateTimeWidgetFixed(DateTimeWidget):
Christophe Siraut's avatar
Christophe Siraut committed
22 23 24
    '''a hack for 1. returning a string and 2. returning a
    fixed hour value (xx:00:00)'''

Christophe Siraut's avatar
Christophe Siraut committed
25 26 27 28
    def __init__(self, **kwargs):
        super(DateTimeWidgetFixed, self).__init__(**kwargs)

    def value_from_datadict(self, data, files, name):
Christophe Siraut's avatar
Christophe Siraut committed
29
        a = str(super(
Christophe Siraut's avatar
Christophe Siraut committed
30
            DateTimeWidgetFixed, self).value_from_datadict(data, files, name))
Christophe Siraut's avatar
Christophe Siraut committed
31 32
        a = ''.join(a.split(':')[0]) + ':00:00'
        return a
Christophe Siraut's avatar
Christophe Siraut committed
33

christophe siraut's avatar
christophe siraut committed
34 35

class PollForm(forms.ModelForm):
Christophe Siraut's avatar
Christophe Siraut committed
36 37 38 39 40
    title = forms.CharField(label=_('Title'))
    description = forms.CharField(
        widget=forms.Textarea,
        label=_('Description'))

christophe siraut's avatar
christophe siraut committed
41 42
    class Meta:
        model = Poll
Christophe Siraut's avatar
Christophe Siraut committed
43 44
        fields = ('title', 'description')

christophe siraut's avatar
christophe siraut committed
45 46

class ChoiceForm(forms.ModelForm):
Christophe Siraut's avatar
Cleanup  
Christophe Siraut committed
47
    choice = forms.DateTimeField(
Christophe Siraut's avatar
Christophe Siraut committed
48 49 50 51
        widget=DateTimeWidgetFixed(
            options=dateTimeOptions)
    )

christophe siraut's avatar
christophe siraut committed
52 53
    class Meta:
        model = Choice
Christophe Siraut's avatar
Christophe Siraut committed
54 55 56 57 58
        #widgets = {
        #  'choice': DateTimeWidget(
        #       attrs={'id': '666'},
        #       options=dateTimeOptions)}
        exclude = ('poll', 'votecount')
christophe siraut's avatar
christophe siraut committed
59 60 61 62 63 64

class BulletinForm(forms.ModelForm):
    class Meta:
        model = Bulletin

class VoteForm(forms.ModelForm):
Christophe Siraut's avatar
Cleanup  
Christophe Siraut committed
65 66 67 68 69
    comment = forms.CharField(
        required=False,
        max_length=80,
        widget=forms.TextInput(attrs={'class':'comment'})
        )
christophe siraut's avatar
christophe siraut committed
70 71 72 73 74
    choice = forms.CharField()
    class Meta:
        model = Vote
        fields = ('choice','voice','comment')