You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
3.0 KiB
Python
83 lines
3.0 KiB
Python
# coding: utf-8
|
|
# Author: Grizzly
|
|
#
|
|
# Views for litl.
|
|
|
|
from django.shortcuts import render
|
|
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 .models import Slug, Destination
|
|
from .forms import SlugAddForm
|
|
|
|
import random, string
|
|
ensemble = string.ascii_letters + string.digits
|
|
|
|
def index(request):
|
|
"""Display the home page of the site.
|
|
TODO: Search and display the urls shortened by the user in the past.
|
|
"""
|
|
context = {}
|
|
return render(request,'base.html',context)
|
|
|
|
def AddSlug(request):
|
|
"""Display the form for creating a new slug.
|
|
"""
|
|
if request.method == 'POST':
|
|
form = SlugAddForm(request.POST)
|
|
if form.is_valid():
|
|
# 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
|
|
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})
|
|
|
|
# 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 = Slug(slug = form.cleaned_data['slug'],
|
|
uid = rand_uid,
|
|
bulk = form.cleaned_data['bulk'],)
|
|
slug.save()
|
|
|
|
destination = Destination(slug = slug,
|
|
destination = form.cleaned_data['destination'],)
|
|
destination.save()
|
|
|
|
return HttpResponseRedirect('display/{}'.format(slug.slug))
|
|
else:
|
|
form = SlugAddForm()
|
|
|
|
# First call, return empty form
|
|
return render(request,'add_slug.html',{'form':form})
|
|
|
|
def display(request, slug_got):
|
|
slug = Slug.objects.filter(slug = slug_got).all()
|
|
|
|
if slug.count()!=1:
|
|
return render(request,'unknown.html',{'slug':slug_got})
|
|
else:
|
|
destinations = Destination.objects.filter(slug=slug.get()).all()
|
|
context = {'slug':slug.get(), 'destinations':destinations, '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:
|
|
context = {'slug':slug_got,'request': request}
|
|
return render(request,'unknown.html',context)
|
|
else:
|
|
destinations = Destination.objects.filter(slug=slug.get()).all()
|
|
return HttpResponsePermanentRedirect(destinations.first().destination)
|
|
|