diff --git a/litl/settings.py b/litl/settings.py index b09c9c6..70a20f1 100644 --- a/litl/settings.py +++ b/litl/settings.py @@ -124,3 +124,8 @@ USE_TZ = True STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, "static_files") STATICFILES_DIRS = (os.path.join(BASE_DIR, "static/"),) + +# Parameters of the shortener: + +RANDOM_SLUG_LEN = 5 +UID_LEN = 10 diff --git a/litl/templates/display.html b/litl/templates/display.html new file mode 100644 index 0000000..479148b --- /dev/null +++ b/litl/templates/display.html @@ -0,0 +1,7 @@ +{% extends 'base.html' %} + +{% block content %} +Slug : {{slug.slug}} +Destination: {{slug.destination}} +Date: {{slug.date}} +{% endblock %} diff --git a/litl/urls.py b/litl/urls.py index eed7122..d0754d2 100644 --- a/litl/urls.py +++ b/litl/urls.py @@ -16,11 +16,12 @@ Including another URLconf from django.contrib import admin from django.urls import path -from .views import index, AddSlug +from .views import index, AddSlug, display urlpatterns = [ path('admin/', admin.site.urls), #path('', index, name="index"), path('', AddSlug, name="index"), - + #path(r"^display/(?P[a-zA-Z0-9]+)$", display, name="display"), + path('display//', display, name='display'), ] diff --git a/litl/views.py b/litl/views.py index fa80242..335ee88 100644 --- a/litl/views.py +++ b/litl/views.py @@ -6,7 +6,7 @@ from django.shortcuts import render from django.http import HttpResponseRedirect -from .settings import TEMPLATES +from .settings import TEMPLATES, RANDOM_SLUG_LEN, UID_LEN from .models import Slug from .forms import SlugAddForm @@ -25,19 +25,23 @@ def AddSlug(request): """ if request.method == 'POST': form = SlugAddForm(request.POST) - if form.is_valid(): slug = form.save(commit=False) if slug.slug == '': - slug.slug = ''.join(random.choice(ensemble) for _ in range(5)) - - slug.uid = ''.join(random.choice(ensemble) for _ in range(20)) - + slug.slug = ''.join(random.choice(ensemble) for _ in range(RANDOM_SLUG_LEN)) + slug.uid = ''.join(random.choice(ensemble) for _ in range(UID_LEN)) slug.save() - - return HttpResponseRedirect('') + return HttpResponseRedirect('display/{}'.format(slug.slug)) else: form = SlugAddForm() - return render(request,'add_slug.html',{'form':form}) +def display(request, slug): + slug = Slug.objects.filter(slug = slug).all() + if slug.count()!=1: + # TODO Redirect to 'unknown and display a message + return HttpResponsRedirect('display') + else: + context = {'slug':slug.get()} + return render(request,'display.html',context) +