diff --git a/paste/tools.py b/paste/tools.py index 069b817..dc04d82 100644 --- a/paste/tools.py +++ b/paste/tools.py @@ -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 diff --git a/paste/urls.py b/paste/urls.py index 04de19d..e20302a 100644 --- a/paste/urls.py +++ b/paste/urls.py @@ -6,7 +6,8 @@ from webtools import settings urlpatterns = [ url(r'^$', views.index, name='index'), - url(r'^paste/(?P[A-z0-9]+)/(?P[a-z]+)?$', views.show, name='paste'), url(r'^history$', views.history, name='history'), url(r'^static/(?P.*)', serve, {'document_root': settings.STATIC_ROOT}), + url(r'^paste/(?P[a-zA-Z0-9]+)/(?P[a-z]+)?$', views.show, name='paste'), + url(r'^(?P[0-9][a-zA-Z0-9]+)$', views.show, name='short_paste'), ] diff --git a/paste/views.py b/paste/views.py index a2dcec7..825c1f1 100644 --- a/paste/views.py +++ b/paste/views.py @@ -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={