views.py 3.13 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
    if not is_registered(request.user):
        return render(request, 'not_registered.html')

18 19 20 21 22 23 24 25 26 27
    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

28
            short = url_form.cleaned_data.get('short').strip()
29 30 31 32
            if len(short) > 0:
                url.short = short
            else:
                url.short = URL.generate_valid_short()
33 34

            expires = url_form.cleaned_data.get('expires')
35 36 37 38 39 40 41 42 43 44 45 46

            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
47
            return redirect('view', url.short)
48 49 50

    return render(request, 'index.html', {
        'form': url_form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
51 52 53
    },
    )

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

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

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

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

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

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

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

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

    url.clicks = url.clicks + 1
    url.save()
109 110 111 112 113 114 115 116

    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 )