forms.py 1.06 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from django import forms
from django.forms import ModelForm, TextInput, RadioSelect, URLInput
from go.models import URL
from django.core.validators import MinLengthValidator, MinValueValidator

class URLForm( ModelForm ):

    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(
        required = True,
        label = 'Expiration',
        choices = EXPIRATION_CHOICES,
        initial = NEVER,
        widget = RadioSelect(),
    )

    class Meta:
        model = URL
        fields = ('target', 'short')
        exclude = ('owner', 'date_created', 'clicks', 'expires')
        labels = {
            'target': 'Long URL',
            'short': 'Short URL (Optional)',
        }
        widgets = {
            'target': URLInput(attrs={
                'placeholder': 'http://',
            }),
            'short': TextInput(attrs={
            }),
        }