Implement surjectiv URL
This commit is contained in:
parent
6a8f1e1032
commit
a143c40b20
6 changed files with 39 additions and 21 deletions
|
@ -1,4 +1,5 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from .models import Slug
|
from .models import Slug, Destination
|
||||||
|
|
||||||
admin.site.register(Slug)
|
admin.site.register(Slug)
|
||||||
|
admin.site.register(Destination)
|
||||||
|
|
|
@ -11,9 +11,8 @@ from .models import Slug
|
||||||
|
|
||||||
class SlugAddForm(forms.Form):
|
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)
|
slug = forms.CharField(label="Slug", max_length=20,required=False)
|
||||||
bulk = forms.BooleanField(label="Bulk", required=False)
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(SlugAddForm, self).__init__(*args, **kwargs)
|
super(SlugAddForm, self).__init__(*args, **kwargs)
|
||||||
|
|
|
@ -17,11 +17,6 @@ class Slug(models.Model):
|
||||||
blank=True,
|
blank=True,
|
||||||
help_text="Slug of the new URL. Up to 20 caracters including letters, number, underscores and hyphens.", )
|
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(
|
uid = models.CharField(
|
||||||
max_length=100,
|
max_length=100,
|
||||||
null=True,)
|
null=True,)
|
||||||
|
|
|
@ -61,10 +61,10 @@ Code</a>
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-header text-success">
|
<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>
|
||||||
<div class="card-body">
|
<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.
|
for the 0.01% that needed that feature.
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
21
litl/templates/multirect.html
Normal file
21
litl/templates/multirect.html
Normal file
|
@ -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
|
# Add slug if needed
|
||||||
if form.cleaned_data['slug'] == '':
|
if form.cleaned_data['slug'] == '':
|
||||||
rand_slug = ''.join(random.choice(ensemble) for _ in range(RANDOM_SLUG_LEN))
|
rand_slug = ''.join(random.choice(ensemble) for _ in range(RANDOM_SLUG_LEN))
|
||||||
while Slug.object.filter(slug = rand_slug).exists():
|
while Slug.objects.filter(slug = rand_slug).exists():
|
||||||
rand_string = ''.join(random.choice(ensemble) for _ in range(RANDOM_SLUG_LEN))
|
rand_slug = ''.join(random.choice(ensemble) for _ in range(RANDOM_SLUG_LEN))
|
||||||
form.cleaned_data['slug'] = rand_string
|
form.cleaned_data['slug'] = rand_slug
|
||||||
else:
|
else:
|
||||||
# If slug added by the user, check uniqueness
|
# If slug added by the user, check uniqueness
|
||||||
if Slug.objects.filter(slug=form.cleaned_data['slug']).exists():
|
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))
|
rand_uid = ''.join(random.choice(ensemble) for _ in range(UID_LEN))
|
||||||
|
|
||||||
slug = Slug(slug = form.cleaned_data['slug'],
|
slug = Slug(slug = form.cleaned_data['slug'],
|
||||||
uid = rand_uid,
|
uid = rand_uid,)
|
||||||
bulk = form.cleaned_data['bulk'],)
|
|
||||||
slug.save()
|
slug.save()
|
||||||
|
|
||||||
destination = Destination(slug = slug,
|
for dest in form.cleaned_data['destination'].split('\r\n'):
|
||||||
destination = form.cleaned_data['destination'],)
|
destination = Destination(slug = slug,
|
||||||
destination.save()
|
destination = dest,)
|
||||||
|
destination.save()
|
||||||
|
|
||||||
return HttpResponseRedirect('display/{}'.format(slug.slug))
|
return HttpResponseRedirect('display/{}'.format(slug.slug))
|
||||||
else:
|
else:
|
||||||
|
@ -78,5 +78,7 @@ def redirect(request,slug_got):
|
||||||
return render(request,'unknown.html',context)
|
return render(request,'unknown.html',context)
|
||||||
else:
|
else:
|
||||||
destinations = Destination.objects.filter(slug=slug.get()).all()
|
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…
Reference in a new issue