views.py 2.61 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 PermissionDenied
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
    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

25
            short = url_form.cleaned_data.get('short').strip()
26 27 28
            url.short = short

            expires = url_form.cleaned_data.get('expires')
29 30 31 32 33 34 35 36 37 38 39 40

            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()
41
            return redirect('success')
42 43 44

    return render(request, 'index.html', {
        'form': url_form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
45 46 47
    },
    )

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
# 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,
    },
    )

63 64
# My-Links page.
@login_required
65
def my_links(request):
66 67 68 69 70 71 72 73
    links = URL.objects.filter( owner = request.user )

    return render(request, 'my_links.html', {
        'links' : links,
    },
    )

# Delete link page.
74
@login_required
75
def delete(request, short):
76
    url = get_object_or_404(URL, short = short )
77 78
    if url.owner == request.user:
        url.delete()
79 80
        return redirect('my_links')
    else:
81
        raise PermissionDenied()
82

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

    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
89 90 91 92 93 94 95

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

    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
96 97 98 99

# Redirection view.
def redirection(request, short):
    try:
Jean Michel Rouly's avatar
Jean Michel Rouly committed
100 101
        # case insensitive matching
        url = URL.objects.get( short__iexact = short )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
102 103 104 105 106 107 108
    except URL.DoesNotExist:
        raise Http404("Target URL not found.")

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