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
24c9403a
Commit
24c9403a
authored
Dec 20, 2013
by
Jean Michel Rouly
Browse files
Added method to generate a new, random short url.
parent
bdcf3a2c
Changes
1
Hide whitespace changes
Inline
Side-by-side
go/go/models.py
View file @
24c9403a
...
...
@@ -2,6 +2,8 @@ from django.db import models
from
django.contrib.auth.models
import
User
from
django.utils
import
timezone
import
random
,
string
# Create your models here.
class
URL
(
models
.
Model
):
owner
=
models
.
ForeignKey
(
User
)
...
...
@@ -17,3 +19,18 @@ class URL( models.Model ):
class
Meta
:
ordering
=
[
'short'
]
@
staticmethod
def
generate_valid_short
():
selection
=
string
.
ascii_lowercase
+
string
.
digits
tries
=
0
while
True
:
short
=
''
.
join
(
random
.
choice
(
selection
)
for
i
in
range
(
5
))
try
:
urls
=
URL
.
objects
.
get
(
short__iexact
=
short
)
tries
+=
1
except
URL
.
DoesNotExist
:
return
short
if
tries
>
100
:
return
None
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