views.py 2.85 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
53
54
# Preview a link.
def view(request, short):
    url = get_object_or_404(URL, short__iexact = short)
    return render(request, 'view.html', {
55
56
57
58
        'url': url,
    },
    )

59
60
# My-Links page.
@login_required
61
def my_links(request):
62
    urls = URL.objects.filter( owner = request.user )
63
    return render(request, 'my_links.html', {
64
        'urls' : urls,
65
66
67
68
    },
    )

# Delete link page.
69
@login_required
70
def delete(request, short):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
71
    url = get_object_or_404(URL, short__iexact = short )
72
73
    if url.owner == request.user:
        url.delete()
74
75
        return redirect('my_links')
    else:
76
        raise PermissionDenied()
77

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

    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
84
85
86
87
88
89
90

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

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

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

    url.clicks = url.clicks + 1
    url.save()
102
103
104
105
106
107
108
109

    from piwikapi.tracking import PiwikTracker
    from django.conf import settings
    piwiktracker = PiwikTracker(settings.PIWIK_SITE_ID, request)
    piwiktracker.set_api_url(settings.PIWIK_URL)
    piwiktracker.do_track_page_view('Redirect to %s' % url.target)

    return redirect( url.target )