check existing slug and uid, just in case

This commit is contained in:
grisel-davy 2020-12-31 10:44:29 +01:00
parent a3c55fd975
commit 6b2545c0fc
4 changed files with 18 additions and 6 deletions

View file

@ -25,6 +25,6 @@ class Slug(models.Model):
auto_now=True,)
def __str__(self):
return '{} -> {:.10}'.format(self.slug,self.destination)
return '{} -> {:.15}...'.format(self.slug,self.destination)

View file

@ -6,6 +6,7 @@
<html>
<head>
<title>Litl.</title>
<link rel="stylesheet" type="text/css" href="{% static 'css/base.css' %}"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
@ -13,6 +14,8 @@
<nav class="navbar navbar-dark bg-dark">
<a class="navbar-brand mb-0 h1" href="/">Litl.</a>
<a class="navbar-right text-white" href="https://gitea.auro.re/Aurore/litl"><i class="fa fa-code-fork" aria-hidden="true"></i>
Code</a>
</nav>
<div class="container">

View file

@ -8,7 +8,7 @@
<h2><p class="fa fa-arrow-down" style="color:grey;"></p></h2>
<div class="container">
<h3><p id="shorten">litl.auro.re/{{slug.slug}}</p></h3>
<h3><p id="shorten">{{request.META.HTTP_HOST}}/{{slug.slug}}</p></h3>
</div>
<button type="button" onclick="copy()" class="btn btn-dark">COPY <i class="fa fa-copy"></i></button>
</div>

View file

@ -27,9 +27,17 @@ def AddSlug(request):
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(RANDOM_SLUG_LEN))
slug.uid = ''.join(random.choice(ensemble) for _ in range(UID_LEN))
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:
@ -41,13 +49,14 @@ def display(request, slug_got):
if slug.count()!=1:
return render(request,'unknown.html',{'slug':slug_got})
else:
context = {'slug':slug.get()}
context = {'slug':slug.get(), 'request': request}
return render(request,'display.html',context)
def redirect(request,slug_got):
slug = Slug.objects.filter(slug = slug_got).all()
if slug.count()!=1:
return render(request,'unknown.html',{'slug':slug_got})
context = {'slug':slug_got,'request': request}
return render(request,'unknown.html',context)
else:
return HttpResponseRedirect(slug.get().destination)