Add a shorter URL for pastes.

This commit is contained in:
Julien Palard 2018-05-12 23:23:57 +02:00
parent 0eaf461e1e
commit 9fa51ecb7e
3 changed files with 9 additions and 5 deletions

View File

@ -1,15 +1,18 @@
import string
import random
import shortuuid
import os
from webtools import settings
from .models import Paste
def random_id(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)):
if not model.objects.filter(slug=uuid[:i]):
return uuid[:i]
potential_uuid = uuid[:i]
if not model.objects.filter(slug=potential_uuid):
return potential_uuid
return uuid

View File

@ -6,7 +6,8 @@ from webtools import settings
urlpatterns = [
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'^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'),
]

View File

@ -33,7 +33,7 @@ def index(request):
return render(request, 'paste/index.html', data)
form.save() # Some logic added to overrided method, see forms.py
location = request.build_absolute_uri(
reverse('paste', kwargs={'slug': paste.slug}))
reverse('short_paste', kwargs={'slug': paste.slug}))
return HttpResponseRedirect(location, content=location + "\n",
content_type='text/plain')
data['form'] = PasteForm(initial={