views.py 2.62 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
29
            if len(short) > 0:
                url.short = short
            else:
                url.short = URL.generate_valid_short()
30
31

            expires = url_form.cleaned_data.get('expires')
32
33
34
35
36
37
38
39
40
41
42
43

            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()
Jean Michel Rouly's avatar
Jean Michel Rouly committed
44
            return redirect('view', url.short)
45
46
47

    return render(request, 'index.html', {
        'form': url_form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
48
49
50
    },
    )

Jean Michel Rouly's avatar
Jean Michel Rouly committed
51
52
# Preview a link.
def view(request, short):
53

Jean Michel Rouly's avatar
Jean Michel Rouly committed
54
    url = get_object_or_404(URL, short__iexact = short)
55

Jean Michel Rouly's avatar
Jean Michel Rouly committed
56
    return render(request, 'view.html', {
57
58
59
60
        'url': url,
    },
    )

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

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

# Delete link page.
72
@login_required
73
def delete(request, short):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
74
    url = get_object_or_404(URL, short__iexact = short )
75
76
    if url.owner == request.user:
        url.delete()
77
78
        return redirect('my_links')
    else:
79
        raise PermissionDenied()
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

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

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