Commit 36078ef0 authored by David Haynes's avatar David Haynes 🙆
Browse files

Clean up forms.py a bit, organize a bit better

- pull target out of Meta (why was it in there, I don't see any benefit)
- organize form fields to be in order of appearence
- I like 'A University Branded URL Shortener' better than just 'A URL Shorterner' tbh fam
parent 69f41c37
from django import forms
from go.models import URL, RegisteredUser
from django.core.validators import RegexValidator # MinLengthValidator, MinValueValidator
from django.core.exceptions import ValidationError
from captcha.fields import CaptchaField
class URLForm(forms.ModelForm):
target = forms.URLField(
required=True,
label='Long URL',
max_length=1000,
widget=forms.URLInput(attrs={
'placeholder': 'http://'
})
)
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'
......@@ -28,36 +53,9 @@ class URLForm(forms.ModelForm):
widget=forms.RadioSelect(),
)
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(attrs={}),
validators=[unique_short],
max_length=20,
min_length=3,
)
class Meta:
model = URL
fields = ('target',)
exclude = ('owner', 'short', 'date_created', 'clicks', 'expires')
labels = {
'target': 'Long URL',
}
widgets = {
'target': forms.URLInput(attrs={
'placeholder': 'http://',
}),
}
fields = '__all__'
class SignupForm(forms.ModelForm):
......
......@@ -2,7 +2,7 @@
{% block title %}
Go - A URL Shortener
Go - A University Branded URL Shortener
{% endblock %}
......
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