Add a shorter URL for pastes.
This commit is contained in:
parent
0eaf461e1e
commit
9fa51ecb7e
|
@ -1,15 +1,18 @@
|
||||||
import string
|
import string
|
||||||
|
import random
|
||||||
import shortuuid
|
import shortuuid
|
||||||
import os
|
import os
|
||||||
from webtools import settings
|
from webtools import settings
|
||||||
from .models import Paste
|
from .models import Paste
|
||||||
|
|
||||||
|
|
||||||
def random_id(model):
|
def random_id(model):
|
||||||
"""Returns a short uuid for the slug of the given model."""
|
"""Returns a short uuid for the slug of the given model."""
|
||||||
uuid = shortuuid.uuid()
|
uuid = random.choice('0123456789') + shortuuid.uuid()
|
||||||
for i in range(3, len(uuid)):
|
for i in range(3, len(uuid)):
|
||||||
if not model.objects.filter(slug=uuid[:i]):
|
potential_uuid = uuid[:i]
|
||||||
return uuid[:i]
|
if not model.objects.filter(slug=potential_uuid):
|
||||||
|
return potential_uuid
|
||||||
return uuid
|
return uuid
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,8 @@ from webtools import settings
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^$', views.index, name='index'),
|
url(r'^$', views.index, name='index'),
|
||||||
url(r'^paste/(?P<slug>[A-z0-9]+)/(?P<renderer>[a-z]+)?$', views.show, name='paste'),
|
|
||||||
url(r'^history$', views.history, name='history'),
|
url(r'^history$', views.history, name='history'),
|
||||||
url(r'^static/(?P<path>.*)', serve, {'document_root': settings.STATIC_ROOT}),
|
url(r'^static/(?P<path>.*)', serve, {'document_root': settings.STATIC_ROOT}),
|
||||||
|
url(r'^paste/(?P<slug>[a-zA-Z0-9]+)/(?P<renderer>[a-z]+)?$', views.show, name='paste'),
|
||||||
|
url(r'^(?P<slug>[0-9][a-zA-Z0-9]+)$', views.show, name='short_paste'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -33,7 +33,7 @@ def index(request):
|
||||||
return render(request, 'paste/index.html', data)
|
return render(request, 'paste/index.html', data)
|
||||||
form.save() # Some logic added to overrided method, see forms.py
|
form.save() # Some logic added to overrided method, see forms.py
|
||||||
location = request.build_absolute_uri(
|
location = request.build_absolute_uri(
|
||||||
reverse('paste', kwargs={'slug': paste.slug}))
|
reverse('short_paste', kwargs={'slug': paste.slug}))
|
||||||
return HttpResponseRedirect(location, content=location + "\n",
|
return HttpResponseRedirect(location, content=location + "\n",
|
||||||
content_type='text/plain')
|
content_type='text/plain')
|
||||||
data['form'] = PasteForm(initial={
|
data['form'] = PasteForm(initial={
|
||||||
|
|
Loading…
Reference in New Issue