views.py 3.51 KB
Newer Older
1
from go.models import URL, RegisteredUser
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
def is_registered( user ):
    try:
13
        registered = RegisteredUser.objects.get( username=user.username )
14 15 16 17
        return True
    except RegisteredUser.DoesNotExist:
        return False

18 19 20 21 22 23 24 25 26 27 28 29
# Error 404
def error_404(request):
    return render(request, '404.html', {
    },
    )

# Error 500
def error_500(request):
    return render(request, '500.html', {
    },
    )

Jean Michel Rouly's avatar
Jean Michel Rouly committed
30
# Homepage view.
31
@login_required
Jean Michel Rouly's avatar
Jean Michel Rouly committed
32 33
def index(request):

34 35 36
    if not is_registered(request.user):
        return render(request, 'not_registered.html')

37 38 39 40 41 42 43 44 45 46
    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

47
            short = url_form.cleaned_data.get('short').strip()
48 49 50 51
            if len(short) > 0:
                url.short = short
            else:
                url.short = URL.generate_valid_short()
52 53

            expires = url_form.cleaned_data.get('expires')
54 55 56 57 58 59 60 61 62 63 64 65

            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
66
            return redirect('view', url.short)
67 68 69

    return render(request, 'index.html', {
        'form': url_form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
70 71 72
    },
    )

Jean Michel Rouly's avatar
Jean Michel Rouly committed
73 74 75 76
# Preview a link.
def view(request, short):
    url = get_object_or_404(URL, short__iexact = short)
    return render(request, 'view.html', {
77 78 79 80
        'url': url,
    },
    )

81 82
# My-Links page.
@login_required
83
def my_links(request):
84 85
    if not is_registered(request.user):
        return render(request, 'not_registered.html')
86
    urls = URL.objects.filter( owner = request.user )
87
    return render(request, 'my_links.html', {
88
        'urls' : urls,
89 90 91 92
    },
    )

# Delete link page.
93
@login_required
94
def delete(request, short):
95 96
    if not is_registered(request.user):
        return render(request, 'not_registered.html')
Jean Michel Rouly's avatar
Jean Michel Rouly committed
97
    url = get_object_or_404(URL, short__iexact = short )
98 99
    if url.owner == request.user:
        url.delete()
100 101
        return redirect('my_links')
    else:
102
        raise PermissionDenied()
103

Jean Michel Rouly's avatar
Jean Michel Rouly committed
104 105 106 107 108 109
# About page, static.
def about(request):
    return render(request, 'about.html', {

    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
110 111 112 113 114 115 116

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

    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
117 118 119 120

# Redirection view.
def redirection(request, short):
    try:
Jean Michel Rouly's avatar
Jean Michel Rouly committed
121 122
        # case insensitive matching
        url = URL.objects.get( short__iexact = short )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
123 124 125 126 127
    except URL.DoesNotExist:
        raise Http404("Target URL not found.")

    url.clicks = url.clicks + 1
    url.save()
128 129 130 131 132 133 134 135

    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 )