Implement surjectiv URL

master
grisel-davy 3 years ago
parent 6a8f1e1032
commit a143c40b20

@ -1,4 +1,5 @@
from django.contrib import admin
from .models import Slug
from .models import Slug, Destination
admin.site.register(Slug)
admin.site.register(Destination)

@ -11,9 +11,8 @@ from .models import Slug
class SlugAddForm(forms.Form):
destination = forms.CharField(label="Destination", max_length=300,required=True)
destination = forms.CharField(label="Destination(s) ", max_length=300,required=True,widget=forms.Textarea(attrs={"rows":3}))
slug = forms.CharField(label="Slug", max_length=20,required=False)
bulk = forms.BooleanField(label="Bulk", required=False)
def __init__(self, *args, **kwargs):
super(SlugAddForm, self).__init__(*args, **kwargs)

@ -16,11 +16,6 @@ class Slug(models.Model):
null=False,
blank=True,
help_text="Slug of the new URL. Up to 20 caracters including letters, number, underscores and hyphens.", )
bulk = models.BooleanField(
default=False,
help_text="Generate a short URL leading to multiple destinations",
)
uid = models.CharField(
max_length=100,

@ -61,10 +61,10 @@ Code</a>
<div class="col">
<div class="card">
<div class="card-header text-success">
<i class="fa fa-list-ol"></i> Bulk URLs
<i class="fa fa-list-ol"></i> Surjectiv URLs
</div>
<div class="card-body">
You can bulk multiple destinations under the same shorten URL. Just
You can pack multiple destinations under the same shorten URL. Just
for the 0.01% that needed that feature.
</div>
</div>

@ -0,0 +1,21 @@
{% extends 'base.html' %}
{% block content %}
<div class="container text-center">
<h2>Redirecting...</h2>
If nothing happens, verify that Javascript is enabled in your browser and authorise it to open links and popups.
</div>
<script type="text/javascript">
(function(){
{% for destination in destinations %}
window.open("{{destination.destination}}",'');
{% endfor %}
window.open('','_parent','');
window.close();
})()
</script>
{% endblock %}

@ -31,9 +31,9 @@ def AddSlug(request):
# 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
while Slug.objects.filter(slug = rand_slug).exists():
rand_slug = ''.join(random.choice(ensemble) for _ in range(RANDOM_SLUG_LEN))
form.cleaned_data['slug'] = rand_slug
else:
# If slug added by the user, check uniqueness
if Slug.objects.filter(slug=form.cleaned_data['slug']).exists():
@ -46,13 +46,13 @@ def AddSlug(request):
rand_uid = ''.join(random.choice(ensemble) for _ in range(UID_LEN))
slug = Slug(slug = form.cleaned_data['slug'],
uid = rand_uid,
bulk = form.cleaned_data['bulk'],)
uid = rand_uid,)
slug.save()
destination = Destination(slug = slug,
destination = form.cleaned_data['destination'],)
destination.save()
for dest in form.cleaned_data['destination'].split('\r\n'):
destination = Destination(slug = slug,
destination = dest,)
destination.save()
return HttpResponseRedirect('display/{}'.format(slug.slug))
else:
@ -78,5 +78,7 @@ def redirect(request,slug_got):
return render(request,'unknown.html',context)
else:
destinations = Destination.objects.filter(slug=slug.get()).all()
return HttpResponsePermanentRedirect(destinations.first().destination)
if destinations.count() == 1:
return HttpResponsePermanentRedirect(destinations.get().destination)
elif destinations.count()>1:
return render(request,'multirect.html',{'destinations':destinations})

Loading…
Cancel
Save