Add uniqueness of slug and uid and messages

This commit is contained in:
grisel-davy 2021-01-01 14:53:57 +01:00
parent 6b2545c0fc
commit e16a8fe738
3 changed files with 40 additions and 20 deletions

View file

@ -4,6 +4,7 @@
# Forms for litl. The main form is for creating the link. # Forms for litl. The main form is for creating the link.
from django import forms from django import forms
from django.core.exceptions import ValidationError
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit from crispy_forms.layout import Submit
from .models import Slug from .models import Slug

View file

@ -23,6 +23,17 @@ Code</a>
<div class="col-sm-1"> <div class="col-sm-1">
</div> </div>
<div class="col-sm-10"> <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"> <div class="pt-5">
{% block content %} {% block content %}
{% endblock %} {% endblock %}

View file

@ -4,7 +4,9 @@
# Views for litl. # Views for litl.
from django.shortcuts import render 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 .settings import TEMPLATES, RANDOM_SLUG_LEN, UID_LEN
from .models import Slug from .models import Slug
@ -23,25 +25,31 @@ def index(request):
def AddSlug(request): def AddSlug(request):
"""Display the form for creating a new slug. """Display the form for creating a new slug.
""" """
if request.method == 'POST': form = SlugAddForm(request.POST or None)
form = SlugAddForm(request.POST) if request.method == 'POST' and form.is_valid():
if form.is_valid(): # Add slug if needed
slug = form.save(commit=False) if form.cleaned_data['slug'] == '':
rand_slug = ''.join(random.choice(ensemble) for _ in range(RANDOM_SLUG_LEN))
if slug.slug == '': while Slug.object.filter(slug = rand_slug).exists():
while slug.slug=='':
rand_string = ''.join(random.choice(ensemble) for _ in range(RANDOM_SLUG_LEN)) rand_string = ''.join(random.choice(ensemble) for _ in range(RANDOM_SLUG_LEN))
if not Slug.objects.filter(slug=rand_string).exists(): form.cleaned_data['slug'] = rand_string
slug.slug = rand_string else:
while slug.uid == None: # If slug added by the user, check uniqueness
rand_uid = ''.join(random.choice(ensemble) for _ in range(UID_LEN)) if Slug.objects.filter(slug=form.cleaned_data['slug']).exists():
if not Slug.objects.filter(uid = rand_uid).exists(): messages.error(request,"This slug is already in use.")
slug.uid = rand_uid return render(request,'add_slug.html',{'form':form})
# 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() slug.save()
return HttpResponseRedirect('display/{}'.format(slug.slug)) return HttpResponseRedirect('display/{}'.format(slug.slug))
else:
form = SlugAddForm() # First call, return empty form
return render(request,'add_slug.html',{'form':form}) return render(request,'add_slug.html',{'form':form})
def display(request, slug_got): def display(request, slug_got):
@ -58,5 +66,5 @@ def redirect(request,slug_got):
context = {'slug':slug_got,'request': request} context = {'slug':slug_got,'request': request}
return render(request,'unknown.html',context) return render(request,'unknown.html',context)
else: else:
return HttpResponseRedirect(slug.get().destination) return HttpResponsePermanentRedirect(slug.get().destination)