views.py 4.31 KB
Newer Older
1
from go.models import URL, RegisteredUser
Jean Michel Rouly's avatar
Jean Michel Rouly committed
2
from go.forms import URLForm, SignupForm
3
from datetime import timedelta
4
from django.conf import settings
Jean Michel Rouly's avatar
Jean Michel Rouly committed
5
from django.http import Http404
6
from django.utils import timezone
Jean Michel Rouly's avatar
Jean Michel Rouly committed
7
from django.contrib.auth.models import User
8
from django.core.exceptions import PermissionDenied
9
from django.contrib.auth.decorators import login_required
Jean Michel Rouly's avatar
Jean Michel Rouly committed
10
from django.shortcuts import render, get_object_or_404, redirect
11
import os
12

13
14
def is_registered( user ):
    try:
15
        registered = RegisteredUser.objects.get( username=user.username )
16
17
18
19
        return True
    except RegisteredUser.DoesNotExist:
        return False

20
21
22
23
24
25
26
27
28
29
30
31
# 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
32
# Homepage view.
33
@login_required
Jean Michel Rouly's avatar
Jean Michel Rouly committed
34
35
def index(request):

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

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

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

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

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

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

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

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

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

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

    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
112
113
114

# Signup page.
def signup(request):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
115
116
117
118
119
120
121
122
    form = SignupForm()

    if request.method == 'POST':
        form = SignupForm( request.POST )
        if form.is_valid():
            username = form.cleaned_data.get('username')
            full_name = form.cleaned_data.get('full_name')
            description = form.cleaned_data.get('description')
123
124
125
126
127
128
129
130
131
132
133
134

            f = open(os.path.join(settings.MEDIA_ROOT, 'registrations.txt'), 'a')
            f.write( str(timezone.now()) )
            f.write( str('\n') )
            f.write( str(username) )
            f.write( str('\n') )
            f.write( str(full_name) )
            f.write( str('\n') )
            f.write( str(description) )
            f.write( str('\n\n\n') )
            f.close()

Jean Michel Rouly's avatar
Jean Michel Rouly committed
135
            return redirect('index')
Jean Michel Rouly's avatar
Jean Michel Rouly committed
136

Jean Michel Rouly's avatar
Jean Michel Rouly committed
137
138
    return render(request, 'signup.html', {
        'form': form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
139
140
    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
141
142
143
144

# Redirection view.
def redirection(request, short):
    try:
Jean Michel Rouly's avatar
Jean Michel Rouly committed
145
146
        # case insensitive matching
        url = URL.objects.get( short__iexact = short )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
147
148
149
150
151
    except URL.DoesNotExist:
        raise Http404("Target URL not found.")

    url.clicks = url.clicks + 1
    url.save()
152
153
154
155
156
157
158
159

    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 )