views.py 2.54 KB
Newer Older
Jean Michel Rouly's avatar
Jean Michel Rouly committed
1
from go.models import URL
2 3
from go.forms import URLForm
from datetime import timedelta
Jean Michel Rouly's avatar
Jean Michel Rouly committed
4
from django.http import Http404
5
from django.utils import timezone
Jean Michel Rouly's avatar
Jean Michel Rouly committed
6
from django.contrib.auth.models import User
7
from django.core.exceptions import ValidationError
8
from django.contrib.auth.decorators import login_required
Jean Michel Rouly's avatar
Jean Michel Rouly committed
9
from django.shortcuts import render, get_object_or_404, redirect
10

Jean Michel Rouly's avatar
Jean Michel Rouly committed
11
# Homepage view.
12
@login_required
Jean Michel Rouly's avatar
Jean Michel Rouly committed
13 14
def index(request):

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    url_form = URLForm() # unbound form
    errors = []

    if request.method == 'POST':
        url_form = URLForm( request.POST ) # bind dat form
        if url_form.is_valid():

            url = url_form.save(commit=False)
            url.owner = request.user

            expires = url_form.cleaned_data['expires']

            if expires == URLForm.DAY:
                url.expires = timezone.now() + timedelta(days=1)
            elif expires == URLForm.WEEK:
                url.expires = timezone.now() + timedelta(weeks=1)
            elif expires == URLForm.MONTH:
                url.expires = timezone.now() + timedelta(weeks=3)
            else:
                pass # leave the field NULL

            url.full_clean()
            url.save()
38
            return redirect('success')
39 40 41

    return render(request, 'index.html', {
        'form': url_form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
42 43 44
    },
    )

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
# Sucessful url registration page.
@login_required
def success(request):

    url = None
    try:
        url = URL.objects.filter(owner=request.user).latest('date_created')
    except URL.DoesNotExist:
        pass

    return render(request, 'success.html', {
        'url': url,
    },
    )

60 61
# My-Links page.
@login_required
62
def my_links(request, permission = True):
63 64 65 66
    links = URL.objects.filter( owner = request.user )

    return render(request, 'my_links.html', {
        'links' : links,
67
        'permission' : permission,
68 69 70 71
    },
    )

# Delete link page.
72
@login_required
73
def delete(request, short):
74 75 76
    url = URL.objects.get( short = short )
    if url.owner == request.user:
        url.delete()
77 78 79
        return redirect('my_links')
    else:
        return my_links(request, permission = False)
80

Jean Michel Rouly's avatar
Jean Michel Rouly committed
81 82 83 84 85 86
# About page, static.
def about(request):
    return render(request, 'about.html', {

    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
87 88 89 90 91 92 93

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

    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
94 95 96 97 98 99 100 101 102 103 104 105

# 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 )