Commit 9cceca85 authored by Jean Michel Rouly's avatar Jean Michel Rouly
Browse files

Customized the short URL input field.

parent d4be5db1
from django import forms
from django.forms import ModelForm, TextInput, RadioSelect, URLInput
from go.models import URL
from django.core.validators import MinLengthValidator, MinValueValidator
from django.core.validators import MinLengthValidator, MinValueValidator, RegexValidator
class URLForm( ModelForm ):
class URLForm( forms.ModelForm ):
DAY = '1 Day'
WEEK = '1 Week'
......@@ -23,21 +22,32 @@ class URLForm( ModelForm ):
label = 'Expiration',
choices = EXPIRATION_CHOICES,
initial = NEVER,
widget = RadioSelect(),
widget = forms.RadioSelect(),
)
# Add a custom short field for validation.
alphanumeric = RegexValidator(
r'^[a-zA-Z]*$',
'Only letters are allowed.'
)
short = forms.CharField(
required = False,
label = 'Short URL (Optional)',
widget = forms.TextInput(attrs={}),
validators = [alphanumeric],
max_length = 20,
min_length = 3,
)
class Meta:
model = URL
fields = ('target', 'short')
exclude = ('owner', 'date_created', 'clicks', 'expires')
fields = ('target',)
exclude = ('owner', 'short', 'date_created', 'clicks', 'expires')
labels = {
'target': 'Long URL',
'short': 'Short URL (Optional)',
}
widgets = {
'target': URLInput(attrs={
'target': forms.URLInput(attrs={
'placeholder': 'http://',
}),
'short': TextInput(attrs={
}),
}
......@@ -22,7 +22,10 @@ def index(request):
url = url_form.save(commit=False)
url.owner = request.user
expires = url_form.cleaned_data['expires']
short = url_form.cleaned_data.get('short')
url.short = short
expires = url_form.cleaned_data.get('expires')
if expires == URLForm.DAY:
url.expires = timezone.now() + timedelta(days=1)
......
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