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.
|
# 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
|
||||||
|
|
|
@ -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 %}
|
||||||
|
|
|
@ -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))
|
||||||
|
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 == '':
|
# Generate uid
|
||||||
while slug.slug=='':
|
rand_uid = ''.join(random.choice(ensemble) for _ in range(UID_LEN))
|
||||||
rand_string = ''.join(random.choice(ensemble) for _ in range(RANDOM_SLUG_LEN))
|
while Slug.objects.filter(uid = rand_uid).exists():
|
||||||
if not Slug.objects.filter(slug=rand_string).exists():
|
rand_uid = ''.join(random.choice(ensemble) for _ in range(UID_LEN))
|
||||||
slug.slug = rand_string
|
|
||||||
while slug.uid == None:
|
slug = form.save(commit=False)
|
||||||
rand_uid = ''.join(random.choice(ensemble) for _ in range(UID_LEN))
|
slug.uid = rand_uid
|
||||||
if not Slug.objects.filter(uid = rand_uid).exists():
|
slug.save()
|
||||||
slug.uid = rand_uid
|
return HttpResponseRedirect('display/{}'.format(slug.slug))
|
||||||
|
|
||||||
slug.save()
|
# First call, return empty form
|
||||||
return HttpResponseRedirect('display/{}'.format(slug.slug))
|
|
||||||
else:
|
|
||||||
form = SlugAddForm()
|
|
||||||
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)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue