views.py 3.24 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

11
12
13
14
# Check if user is registered.
def is_registered(user):
    return user.groups.filter(name="Registered").exists()

Jean Michel Rouly's avatar
Jean Michel Rouly committed
15
# Homepage view.
16
@login_required
Jean Michel Rouly's avatar
Jean Michel Rouly committed
17
18
def index(request):

19
20
21
    if not is_registered(request.user):
        return render(request, 'not_registered.html')

22
23
24
25
26
27
28
29
30
31
    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

32
            short = url_form.cleaned_data.get('short').strip()
33
34
35
36
            if len(short) > 0:
                url.short = short
            else:
                url.short = URL.generate_valid_short()
37
38

            expires = url_form.cleaned_data.get('expires')
39
40
41
42
43
44
45
46
47
48
49
50

            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
51
            return redirect('view', url.short)
52
53
54

    return render(request, 'index.html', {
        'form': url_form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
55
56
57
    },
    )

Jean Michel Rouly's avatar
Jean Michel Rouly committed
58
59
60
61
# Preview a link.
def view(request, short):
    url = get_object_or_404(URL, short__iexact = short)
    return render(request, 'view.html', {
62
63
64
65
        'url': url,
    },
    )

66
67
# My-Links page.
@login_required
68
def my_links(request):
69
70
    if not is_registered(request.user):
        return render(request, 'not_registered.html')
71
    urls = URL.objects.filter( owner = request.user )
72
    return render(request, 'my_links.html', {
73
        'urls' : urls,
74
75
76
77
    },
    )

# Delete link page.
78
@login_required
79
def delete(request, short):
80
81
    if not is_registered(request.user):
        return render(request, 'not_registered.html')
Jean Michel Rouly's avatar
Jean Michel Rouly committed
82
    url = get_object_or_404(URL, short__iexact = short )
83
84
    if url.owner == request.user:
        url.delete()
85
86
        return redirect('my_links')
    else:
87
        raise PermissionDenied()
88

Jean Michel Rouly's avatar
Jean Michel Rouly committed
89
90
91
92
93
94
# About page, static.
def about(request):
    return render(request, 'about.html', {

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

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

    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
102
103
104
105

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

    url.clicks = url.clicks + 1
    url.save()
113
114
115
116
117
118
119
120

    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 )