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

Jean Michel Rouly's avatar
Jean Michel Rouly committed
18
# Homepage view.
19
@login_required
Jean Michel Rouly's avatar
Jean Michel Rouly committed
20 21
def index(request):

22 23 24
    if not is_registered(request.user):
        return render(request, 'not_registered.html')

25 26 27 28 29 30 31 32 33 34
    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

35
            short = url_form.cleaned_data.get('short').strip()
36 37 38 39
            if len(short) > 0:
                url.short = short
            else:
                url.short = URL.generate_valid_short()
40 41

            expires = url_form.cleaned_data.get('expires')
42 43 44 45 46 47 48 49 50 51 52 53

            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
54
            return redirect('view', url.short)
55 56 57

    return render(request, 'index.html', {
        'form': url_form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
58 59 60
    },
    )

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

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

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

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

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

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

    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
105 106 107 108

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

    url.clicks = url.clicks + 1
    url.save()
116 117 118 119 120 121 122 123

    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 )