Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SRCT
go
Commits
9cceca85
Commit
9cceca85
authored
Dec 20, 2013
by
Jean Michel Rouly
Browse files
Customized the short URL input field.
parent
d4be5db1
Changes
2
Show whitespace changes
Inline
Side-by-side
go/go/forms.py
View file @
9cceca85
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
=
{
}),
}
go/go/views.py
View file @
9cceca85
...
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment