forms.py 3.99 KB
Newer Older
1
from django import forms
2
from go.models import URL, RegisteredUser
3
from django.core.exceptions import ValidationError
4
from captcha.fields import CaptchaField
David Haynes's avatar
David Haynes committed
5
6
7
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit
from crispy_forms.bootstrap import StrictButton
8
9

class URLForm(forms.ModelForm):
10

11
    # Custom target URL field
12
13
14
15
16
    target = forms.URLField(
        required=True,
        label='Long URL',
        max_length=1000,
        widget=forms.URLInput(attrs={
17
            'placeholder': 'https://'
18
19
20
        })
    )

21
    # Check to make sure the short url has not been used
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    def unique_short(value):
        try:
            URL.objects.get(short__iexact=value)
        except URL.DoesNotExist:
            return
        raise ValidationError('Short url already exists.')

    # Custom short-url field with validators.
    short = forms.SlugField(
        required=False,
        label='Short URL (Optional)',
        widget=forms.TextInput(),
        validators=[unique_short],
        max_length=20,
        min_length=3,
    )

39
40
41
42
43
44
45
46
47
48
49
50
51
52
    DAY = '1 Day'
    WEEK = '1 Week'
    MONTH = '1 Month'
    NEVER = 'Never'

    EXPIRATION_CHOICES = (
        (DAY, DAY),
        (WEEK, WEEK),
        (MONTH, MONTH),
        (NEVER, NEVER),
    )

    # Add a custom expiration choice field.
    expires = forms.ChoiceField(
53
54
55
56
57
        required=True,
        label='Expiration',
        choices=EXPIRATION_CHOICES,
        initial=NEVER,
        widget=forms.RadioSelect(),
58
59
    )

60
61
    class Meta:
        model = URL
62
63
        fields = ('target',)
        exclude = ('owner', 'short', 'date_created', 'clicks', 'expires')
64

65
class SignupForm(forms.ModelForm):
66
67
68
69
70
71
72
73

    def validate_username(username):
        try:
            registered = RegisteredUser.objects.get(username=username)
            raise ValidationError('Username "%s" is already in use.' % username)
        except RegisteredUser.DoesNotExist:
            return

74
    username = forms.CharField(
75
76
77
        required=True,
        label='Mason NetID',
        max_length=30,
78
        validators=[validate_username],
79
        widget=forms.TextInput(attrs={
80
81
82
        }),
    )
    full_name = forms.CharField(
83
84
85
86
        required=True,
        label='Full Name',
        max_length=100,
        widget=forms.TextInput(attrs={
87
88
89
        }),
    )
    description = forms.CharField(
90
91
92
93
        required=False,
        label='Description (Optional)',
        max_length=200,
        widget=forms.Textarea(attrs={
94
95
        }),
    )
96
    captcha = CaptchaField()
97
98
99

    class Meta:
        model = RegisteredUser
100
        fields = '__all__'
David Haynes's avatar
David Haynes committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

class ExampleForm(forms.Form):
    like_website = forms.TypedChoiceField(
        label = "Do you like this website?",
        choices = ((1, "Yes"), (0, "No")),
        coerce = lambda x: bool(int(x)),
        widget = forms.RadioSelect,
        initial = '1',
        required = True,
    )

    favorite_food = forms.CharField(
        label = "What is your favorite food?",
        max_length = 80,
        required = True,
    )

    favorite_color = forms.CharField(
        label = "What is your favorite color?",
        max_length = 80,
        required = True,
    )

    favorite_number = forms.IntegerField(
        label = "Favorite number",
        required = False,
    )

    notes = forms.CharField(
        label = "Additional notes or feedback",
        required = False,
    )
    def __init__(self, *args, **kwargs):
        super(ExampleForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_id = 'id-exampleForm'
        self.helper.form_method = 'post'
        self.helper.form_action = 'submit_survey'

        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-md-4'
        self.helper.field_class = 'col-md-4'
        self.helper.layout = Layout(
            'like_website',
            'favorite_food',
            'favorite_color',
            'favorite_number',
            'notes',
            StrictButton('Sign in', css_class='btn-default'),
        )