min.ie/links/views.py

30 lines
985 B
Python
Raw Normal View History

2015-01-18 00:24:13 +00:00
from urlparse import urlparse
2015-01-17 23:54:26 +00:00
from django.shortcuts import render, redirect
from django.http import Http404
2015-01-18 00:24:13 +00:00
from django.contrib import messages
2015-01-17 23:54:26 +00:00
from .models import Link, LinkForm
def catchall(request, id):
try:
link = Link.objects.get(id=id)
return redirect(link.url)
except:
parsed = urlparse(id)
if parsed.netloc:
link = Link(url=id)
link.save();
context = {'form': LinkForm}
2015-01-18 00:24:13 +00:00
request.session['short_url'] = "http://" + str(request.get_host()) + "/" + str(link.id)
return redirect('/')
2015-01-17 23:54:26 +00:00
raise Http404("Link does not exist")
def home(request):
context = {'form': LinkForm}
2015-01-18 00:24:13 +00:00
if 'short_url' in request.session:
context['short_url'] = request.session['short_url']
2015-01-17 23:59:29 +00:00
if 'url' in request.POST:
link = Link(url=request.POST['url'])
2015-01-17 23:54:26 +00:00
link.save();
2015-01-18 00:24:13 +00:00
request.session['short_url'] = "http://" + request.get_host() + "/" + link.id
return redirect('/')
2015-01-17 23:54:26 +00:00
return render(request, 'index.html', context)