Add uniqueness of slug and uid and messages
This commit is contained in:
parent
6b2545c0fc
commit
e16a8fe738
3 changed files with 40 additions and 20 deletions
|
@ -4,6 +4,7 @@
|
|||
# Forms for litl. The main form is for creating the link.
|
||||
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
from crispy_forms.helper import FormHelper
|
||||
from crispy_forms.layout import Submit
|
||||
from .models import Slug
|
||||
|
|
|
@ -23,6 +23,17 @@ Code</a>
|
|||
<div class="col-sm-1">
|
||||
</div>
|
||||
<div class="col-sm-10">
|
||||
{% if messages %}
|
||||
<div class="mt-5 mb-5">
|
||||
{% for message in messages %}
|
||||
{% if message.tags == "error" %}
|
||||
<div class="alert alert-danger">{{ message }}</div>
|
||||
{% else %}
|
||||
<div class="alert alert-info">{{ message }}</div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="pt-5">
|
||||
{% block content %}
|
||||
{% endblock %}
|
||||
|
|
|
@ -4,7 +4,9 @@
|
|||
# Views for litl.
|
||||
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponseRedirect
|
||||
from django import forms
|
||||
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
|
||||
from django.contrib import messages
|
||||
|
||||
from .settings import TEMPLATES, RANDOM_SLUG_LEN, UID_LEN
|
||||
from .models import Slug
|
||||
|
@ -23,25 +25,31 @@ def index(request):
|
|||
def AddSlug(request):
|
||||
"""Display the form for creating a new slug.
|
||||
"""
|
||||
if request.method == 'POST':
|
||||
form = SlugAddForm(request.POST)
|
||||
if form.is_valid():
|
||||
slug = form.save(commit=False)
|
||||
form = SlugAddForm(request.POST or None)
|
||||
if request.method == 'POST' and form.is_valid():
|
||||
# Add slug if needed
|
||||
if form.cleaned_data['slug'] == '':
|
||||
rand_slug = ''.join(random.choice(ensemble) for _ in range(RANDOM_SLUG_LEN))
|
||||
while Slug.object.filter(slug = rand_slug).exists():
|
||||
rand_string = ''.join(random.choice(ensemble) for _ in range(RANDOM_SLUG_LEN))
|
||||
form.cleaned_data['slug'] = rand_string
|
||||
else:
|
||||
# If slug added by the user, check uniqueness
|
||||
if Slug.objects.filter(slug=form.cleaned_data['slug']).exists():
|
||||
messages.error(request,"This slug is already in use.")
|
||||
return render(request,'add_slug.html',{'form':form})
|
||||
|
||||
if slug.slug == '':
|
||||
while slug.slug=='':
|
||||
rand_string = ''.join(random.choice(ensemble) for _ in range(RANDOM_SLUG_LEN))
|
||||
if not Slug.objects.filter(slug=rand_string).exists():
|
||||
slug.slug = rand_string
|
||||
while slug.uid == None:
|
||||
rand_uid = ''.join(random.choice(ensemble) for _ in range(UID_LEN))
|
||||
if not Slug.objects.filter(uid = rand_uid).exists():
|
||||
slug.uid = rand_uid
|
||||
|
||||
slug.save()
|
||||
return HttpResponseRedirect('display/{}'.format(slug.slug))
|
||||
else:
|
||||
form = SlugAddForm()
|
||||
# Generate uid
|
||||
rand_uid = ''.join(random.choice(ensemble) for _ in range(UID_LEN))
|
||||
while Slug.objects.filter(uid = rand_uid).exists():
|
||||
rand_uid = ''.join(random.choice(ensemble) for _ in range(UID_LEN))
|
||||
|
||||
slug = form.save(commit=False)
|
||||
slug.uid = rand_uid
|
||||
slug.save()
|
||||
return HttpResponseRedirect('display/{}'.format(slug.slug))
|
||||
|
||||
# First call, return empty form
|
||||
return render(request,'add_slug.html',{'form':form})
|
||||
|
||||
def display(request, slug_got):
|
||||
|
@ -58,5 +66,5 @@ def redirect(request,slug_got):
|
|||
context = {'slug':slug_got,'request': request}
|
||||
return render(request,'unknown.html',context)
|
||||
else:
|
||||
return HttpResponseRedirect(slug.get().destination)
|
||||
return HttpResponsePermanentRedirect(slug.get().destination)
|
||||
|
||||
|
|
Loading…
Reference in a new issue