views.py 1.3 KB
Newer Older
Jean Michel Rouly's avatar
Jean Michel Rouly committed
1
from go.models import URL
Jean Michel Rouly's avatar
Jean Michel Rouly committed
2
from django.http import Http404
Jean Michel Rouly's avatar
Jean Michel Rouly committed
3
from django.contrib.auth.models import User
4
from django.contrib.auth.decorators import login_required
Jean Michel Rouly's avatar
Jean Michel Rouly committed
5
from django.shortcuts import render, get_object_or_404, redirect
6

Jean Michel Rouly's avatar
Jean Michel Rouly committed
7
# Homepage view.
8
@login_required
Jean Michel Rouly's avatar
Jean Michel Rouly committed
9 10 11 12 13 14
def index(request):
    return render(request, 'index.html', {

    },
    )

15 16
# My-Links page.
@login_required
17
def my_links(request, permission = True):
18 19 20 21
    links = URL.objects.filter( owner = request.user )

    return render(request, 'my_links.html', {
        'links' : links,
22
        'permission' : permission,
23 24 25 26
    },
    )

# Delete link page.
27
@login_required
28
def delete(request, short):
29 30 31
    url = URL.objects.get( short = short )
    if url.owner == request.user:
        url.delete()
32 33 34
        return redirect('my_links')
    else:
        return my_links(request, permission = False)
35

Jean Michel Rouly's avatar
Jean Michel Rouly committed
36 37 38 39 40 41
# About page, static.
def about(request):
    return render(request, 'about.html', {

    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
42 43 44 45 46 47 48

# Signup page.
def signup(request):
    return render(request, 'signup.html', {

    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
49 50 51 52 53 54 55 56 57 58 59 60

# Redirection view.
def redirection(request, short):
    try:
        url = URL.objects.get( short = short )
    except URL.DoesNotExist:
        raise Http404("Target URL not found.")

    target = url.target
    url.clicks = url.clicks + 1
    url.save()
    return redirect( target )