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
Christopher M Reffett
go
Commits
0476774e
Commit
0476774e
authored
Feb 01, 2014
by
Jean Michel Rouly
Browse files
Reverted
parent
ee7c67c9
Changes
1
Hide whitespace changes
Inline
Side-by-side
go/go/forms.py
View file @
0476774e
...
...
@@ -32,49 +32,23 @@ class URLForm( forms.ModelForm ):
'Only letters are allowed.'
)
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
.
CharField
(
required
=
False
,
label
=
'Short URL (Optional)'
,
widget
=
forms
.
TextInput
(
attrs
=
{}),
validators
=
[
alphanumeric
],
validators
=
[
alphanumeric
,
unique_short
],
max_length
=
20
,
min_length
=
3
,
)
def
clean
(
self
):
"""
Override the default clean method to check if the entered short
exists, or if none is entered, to generate a new one.
"""
cleaned_data
=
super
(
URLForm
,
self
).
clean
()
short
=
cleaned_data
.
get
(
'short'
).
strip
()
# If the user has entered a value for short, then verify that it's
# unique.
if
len
(
short
)
>
0
:
try
:
URL
.
objects
.
get
(
short__iexact
=
short
)
except
URL
.
DoesNotExist
:
return
cleaned_data
raise
ValidationError
(
'Short URL already exists.'
)
# If the user did not enter a value for short, then attempt to
# generate a ranomd url. If a random URL cannot be generated in 100
# attempts, raise an error.
else
:
short
=
URL
.
generate_valid_short
()
if
short
is
None
:
raise
ValidationError
(
'Unable to generate identifier. Try again.'
)
else
:
# Set the new, randomly generated short value
cleaned_data
[
'short'
]
=
short
return
cleaned_data
# This should never happen.
raise
ValidationError
(
'Server error. Try again.'
)
class
Meta
:
model
=
URL
fields
=
(
'target'
,)
...
...
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