forms.py 2.55 KB
Newer Older
1
from django import forms
2
from go.models import URL, RegisteredUser
3
from django.core.validators import RegexValidator  # MinLengthValidator, MinValueValidator
4
from django.core.exceptions import ValidationError
5
from captcha.fields import CaptchaField
6

7
8

class URLForm(forms.ModelForm):
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

    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(
24
25
26
27
28
        required=True,
        label='Expiration',
        choices=EXPIRATION_CHOICES,
        initial=NEVER,
        widget=forms.RadioSelect(),
29
30
    )

Jean Michel Rouly's avatar
Jean Michel Rouly committed
31
    # Short field must be only letters.
32
33
34
35
    alphanumeric = RegexValidator(
        r'^[a-zA-Z]*$',
        'Only letters are allowed.'
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
36

37
    def unique_short(value):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
38
39
40
41
42
43
        try:
            URL.objects.get(short__iexact=value)
        except URL.DoesNotExist:
            return
        raise ValidationError('Short url already exists.')

Jean Michel Rouly's avatar
Jean Michel Rouly committed
44
    # Custom short-url field with validators.
45
    short = forms.CharField(
46
47
48
49
50
51
        required=False,
        label='Short URL (Optional)',
        widget=forms.TextInput(attrs={}),
        validators=[alphanumeric, unique_short],
        max_length=20,
        min_length=3,
52
53
54
55
    )

    class Meta:
        model = URL
56
57
        fields = ('target',)
        exclude = ('owner', 'short', 'date_created', 'clicks', 'expires')
58
59
60
61
        labels = {
            'target': 'Long URL',
        }
        widgets = {
62
            'target': forms.URLInput(attrs={
63
64
65
                'placeholder': 'http://',
            }),
        }
66
67


68
class SignupForm(forms.ModelForm):
69
70
71
72
73
74
75
76

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

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

    class Meta:
        model = RegisteredUser