forms.py 7.19 KB
Newer Older
1
from django import forms
2
from go.models import URL, RegisteredUser
3
from django.core.exceptions import ValidationError
4
from django.utils.safestring import mark_safe
David Haynes's avatar
David Haynes committed
5
from crispy_forms.helper import FormHelper
6 7
from crispy_forms.layout import Layout, Fieldset, Submit, HTML, Div, Field
from crispy_forms.bootstrap import StrictButton, PrependedText, Accordion, AccordionGroup
8 9

class URLForm(forms.ModelForm):
10

11 12 13
    # Custom target URL field
    target = forms.URLField(
        required=True,
14
        label='Long URL (Required)',
15 16
        max_length=1000,
        widget=forms.URLInput(attrs={
17
            'placeholder': 'https://yoursite.com/'
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 44 45 46 47 48 49 50 51 52 53
        })
    )

    # Check to make sure the short url has not been used
    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,
    )

    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,
54
        label='Expiration (Required)',
55 56 57 58 59 60
        choices=EXPIRATION_CHOICES,
        initial=NEVER,
        widget=forms.RadioSelect(),
    )

    def __init__(self, *args, **kwargs):
61
        super(URLForm, self).__init__(*args, **kwargs)
62
        self.helper = FormHelper()
63
        self.helper.form_method = 'POST'
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-md-1'
        self.helper.field_class = 'col-md-6'


        self.helper.layout = Layout(
            Fieldset(
                '',
                Accordion(
                    AccordionGroup('Step 1: Long URL',
                        Div(
                            HTML("""
                                <h4>Paste the URL you would like to shorten:</h4>
                                <br />
                            """),
80
                            'target',
81 82 83 84
                            style="background: rgb(#F6F6F6);",
                            title="target_url",
                            css_class="first_group",
                        ),
85 86 87
                        css_id='firstCollapse',
                        active=True,
                        template='crispy/accordian-group.html',
88 89 90 91 92
                    ),
                    AccordionGroup('Step 2: Short URL',
                        Div(
                            HTML("""
                                <h4>Create a custom Go address:</h4>
93
                                <br />
94 95
                            """),
                            PrependedText('short',
96
                            'https://go.gmu.edu/',
97 98 99 100
                            ),
                            style="background: rgb(#F6F6F6);",
                            title="short_url",
                            css_class="second_group",
101 102
                        ),
                        css_id='secondCollapse',
103
                        active=True,
104
                        template='crispy/accordian-group.html',
105 106 107 108 109
                    ),
                    AccordionGroup('Step 3: URL Expiration',
                        Div(
                            HTML("""
                                <h4>Set when you would like your Go address to expire:</h4>
110
                                <br />
111 112 113 114 115
                            """),
                            'expires',
                            style="background: rgb(#F6F6F6);",
                            title="expires_url",
                            css_class="third_group",
116 117
                        ),
                        css_id='thirdCollapse',
118
                        active=True,
119 120 121 122
                        template='crispy/accordian-group.html',
                    ),
                    css_id='accordian',
                    template='crispy/accordian.html'
123 124 125 126
                ),
            HTML("""
                <br />
            """),
David Haynes's avatar
David Haynes committed
127
            StrictButton('Shorten', css_class="btn btn-primary btn-md col-md-4", type='submit'),
128 129 130
        )
    )

131

132 133 134 135 136 137
    class Meta:
        model = URL
        fields = ('target',)
        exclude = ('owner', 'short', 'date_created', 'clicks', 'expires')


138
class SignupForm(forms.ModelForm):
139 140 141 142 143 144 145 146

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

147
    username = forms.CharField(
148
        required=True,
149
        label='Mason NetID (Required)',
150
        max_length=30,
151
        validators=[validate_username],
152
        widget=forms.TextInput(attrs={
153 154 155
        }),
    )
    full_name = forms.CharField(
156
        required=True,
157
        label='Full Name (Required)',
158 159
        max_length=100,
        widget=forms.TextInput(attrs={
160 161
        }),
    )
162 163 164 165 166 167 168
    organization = forms.CharField(
        required=True,
        label='Organization (Required)',
        max_length=100,
        widget=forms.TextInput(attrs={
        })
    )
169
    description = forms.CharField(
170 171 172 173
        required=False,
        label='Description (Optional)',
        max_length=200,
        widget=forms.Textarea(attrs={
174 175
        }),
    )
176 177
    tos_box = forms.BooleanField(
        required=True,
178
        # Need to add a Terms of Service Page and replace the href below
179 180 181 182 183 184 185 186 187 188 189 190
        label = mark_safe('Do you accept the <a href="#" target="_blank">Terms of Service</a>?'),
    )

    def clean_username(self):
        # Prevent hax: (non-staff) Users cannot signup for other users
        cleaned_data = super(SignupForm, self).clean()
        data_username = cleaned_data.get("username")

        if not self.request.user.is_staff:
            if self.request.user.username not in data_username:
                self.add_error('username', "This is not your NetID!")
        return data_username
191

192 193 194 195 196

    def __init__(self, request, *args, **kwargs):
        # Necessary to call request in forms.py, is otherwise restricted to views.py and models.py
        self.request = request
        super(SignupForm, self).__init__(*args, **kwargs)
David Haynes's avatar
David Haynes committed
197
        self.helper = FormHelper()
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-md-4'
        self.helper.field_class = 'col-md-6'

        self.helper.layout = Layout(
            Fieldset(
                '',
                Div(
                    Div(
                        'username',
                        'full_name',
                        'organization',
                        'description',
                        'tos_box',
                        css_class='well',
                    ),
                    StrictButton('Submit',css_class='btn btn-primary btn-md col-md-4', type='submit'),
                    css_class='col-md-6',
                ),
            )
        )
219 220
    class Meta:
        model = RegisteredUser
221
        fields = '__all__'