Commit 57af3414 authored by David Haynes's avatar David Haynes
Browse files

Comments for forms.py

- luckily I commented up the crispy forms back when I wrote them
parent b3ca497a
Pipeline #491 passed with stage
in 7 minutes and 24 seconds
......@@ -14,11 +14,16 @@ from crispy_forms.bootstrap import StrictButton, PrependedText, Accordion, Accor
from bootstrap3_datetime.widgets import DateTimePicker
from datetime import date, datetime, timedelta
"""
The form that is used in URL creation.
"""
class URLForm(forms.ModelForm):
# Prevent redirect loop links
def clean_target(self):
# get the entered target link
target = self.cleaned_data.get('target')
# if the host (go.gmu.edu) is in the entered target link
if self.host in target:
raise ValidationError("You can't make a Go link to Go silly!")
else:
......@@ -37,27 +42,31 @@ class URLForm(forms.ModelForm):
# Check to make sure the short url has not been used
def unique_short(value):
try:
# if we're able to get a URL with the same short url
URL.objects.get(short__iexact=value)
except URL.DoesNotExist:
return
# then raise a ValidationError
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,
required = False,
label = 'Short URL (Optional)',
widget = forms.TextInput(),
validators = [unique_short],
max_length = 20,
min_length = 3,
)
# define some string date standards
DAY = '1 Day'
WEEK = '1 Week'
MONTH = '1 Month'
CUSTOM = 'Custom Date'
NEVER = 'Never'
# define a tuple of string date standards to be used as our date choices
EXPIRATION_CHOICES = (
(DAY, DAY),
(WEEK, WEEK),
......@@ -68,16 +77,19 @@ class URLForm(forms.ModelForm):
# Add preset expiration choices.
expires = forms.ChoiceField(
required=True,
label='Expiration (Required)',
choices=EXPIRATION_CHOICES,
initial=NEVER,
widget=forms.RadioSelect(),
required = True,
label = 'Expiration (Required)',
choices = EXPIRATION_CHOICES,
initial = NEVER,
widget = forms.RadioSelect(),
)
# Check if the selected date is a valid date
def valid_date(value):
# a valid date is one that is greater than today
if value > timezone.now():
return
# raise a ValidationError if the date is invalid
else:
raise ValidationError('Date must be after today.')
......@@ -85,20 +97,22 @@ class URLForm(forms.ModelForm):
# Add a custom expiration choice.
expires_custom = forms.DateTimeField(
required = False,
label='Custom Date',
input_formats=['%m-%d-%Y'],
validators=[valid_date],
initial=lambda: datetime.now() + timedelta(days=1),
widget=DateTimePicker(
label = 'Custom Date',
input_formats = ['%m-%d-%Y'],
validators = [valid_date],
initial = lambda: datetime.now() + timedelta(days=1),
widget = DateTimePicker(
options={
"format": "MM-DD-YYYY",
"pickTime": False,
},
icon_attrs={
"class": "fa fa-calendar",
})
},
)
)
# on initialization of the form, crispy forms renders this layout
def __init__(self, *args, **kwargs):
# Grab that host info
self.host = kwargs.pop('host', None)
......@@ -159,35 +173,42 @@ class URLForm(forms.ModelForm):
<br />"""),
StrictButton('Shorten', css_class="btn btn-primary btn-md col-md-4", type='submit')))
# metadata about this ModelForm
class Meta:
# what model this form is for
model = URL
fields = ('target',)
exclude = ('owner', 'short', 'date_created', 'clicks', 'expires')
# what attributes are included
fields = ['target',]
"""
The form that is used when a user is signing up to be a RegisteredUser
"""
class SignupForm(forms.ModelForm):
# The full name of the RegisteredUser
full_name = forms.CharField(
required=True,
label='Full Name (Required)',
max_length=100,
widget=forms.TextInput(attrs={
}),
required = True,
label = 'Full Name (Required)',
max_length = 100,
widget = forms.TextInput(),
)
# The RegisteredUser's chosen organization
organization = forms.CharField(
required=True,
label='Organization (Required)',
max_length=100,
widget=forms.TextInput(attrs={
})
required = True,
label = 'Organization (Required)',
max_length = 100,
widget = forms.TextInput(),
)
# The RegisteredUser's reason for signing up to us Go
description = forms.CharField(
required=False,
label='Description (Optional)',
max_length=200,
widget=forms.Textarea(attrs={
}),
required = False,
label = 'Description (Optional)',
max_length = 200,
widget = forms.Textarea(),
)
# A user becomes registered when they agree to the TOS
registered = forms.BooleanField(
required=True,
......@@ -195,6 +216,7 @@ class SignupForm(forms.ModelForm):
label = mark_safe('Do you accept the <a href="http://127.0.0.1:8000/about#terms">Terms of Service</a>?'),
)
# on initialization of the form, crispy forms renders this layout
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
......@@ -218,7 +240,10 @@ class SignupForm(forms.ModelForm):
# Extras at bottom
StrictButton('Submit',css_class='btn btn-primary btn-md col-md-4', type='submit'),
css_class='col-md-6')))
# metadata about this ModelForm
class Meta:
# what model this form is for
model = RegisteredUser
fields = ('full_name', 'organization', 'description', 'registered',)
exclude = ('user',)
# what attributes are included
fields = ['full_name', 'organization', 'description', 'registered',]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment