views.py 4.42 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', {
    },
    )

32
33
34
35
def qrcode(url):
    img = qrcode.make(url)
    return img

Jean Michel Rouly's avatar
Jean Michel Rouly committed
36
# Homepage view.
37
@login_required
Jean Michel Rouly's avatar
Jean Michel Rouly committed
38
39
def index(request):

40
41
42
    if not is_registered(request.user):
        return render(request, 'not_registered.html')

43
44
45
46
47
48
49
50
51
52
    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

53
            short = url_form.cleaned_data.get('short').strip()
54
55
56
57
            if len(short) > 0:
                url.short = short
            else:
                url.short = URL.generate_valid_short()
58
59

            expires = url_form.cleaned_data.get('expires')
60
61
62
63
64
65
66
67
68
69
70
71

            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
72
            return redirect('view', url.short)
73
74
75

    return render(request, 'index.html', {
        'form': url_form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
76
77
78
    },
    )

Jean Michel Rouly's avatar
Jean Michel Rouly committed
79
80
81
# Preview a link.
def view(request, short):
    url = get_object_or_404(URL, short__iexact = short)
82
83
84

    qrcode = qrcode(url)

Jean Michel Rouly's avatar
Jean Michel Rouly committed
85
    return render(request, 'view.html', {
86
        'url': url,
87
        'qrcode' : qrcode,
88
89
90
    },
    )

91
92
# My-Links page.
@login_required
93
def my_links(request):
94
95
    if not is_registered(request.user):
        return render(request, 'not_registered.html')
96
    urls = URL.objects.filter( owner = request.user )
97
    return render(request, 'my_links.html', {
98
        'urls' : urls,
99
100
101
102
    },
    )

# Delete link page.
103
@login_required
104
def delete(request, short):
105
106
    if not is_registered(request.user):
        return render(request, 'not_registered.html')
Jean Michel Rouly's avatar
Jean Michel Rouly committed
107
    url = get_object_or_404(URL, short__iexact = short )
108
109
    if url.owner == request.user:
        url.delete()
110
111
        return redirect('my_links')
    else:
112
        raise PermissionDenied()
113

Jean Michel Rouly's avatar
Jean Michel Rouly committed
114
115
116
117
118
119
# About page, static.
def about(request):
    return render(request, 'about.html', {

    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
120
121
122

# Signup page.
def signup(request):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
123
124
125
126
127
128
129
130
    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')
131
132
133
134
135
136
137
138
139
140
141
142

            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
143
            return redirect('index')
Jean Michel Rouly's avatar
Jean Michel Rouly committed
144

Jean Michel Rouly's avatar
Jean Michel Rouly committed
145
146
    return render(request, 'signup.html', {
        'form': form,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
147
148
    },
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
149
150
151
152

# Redirection view.
def redirection(request, short):
    try:
Jean Michel Rouly's avatar
Jean Michel Rouly committed
153
154
        # case insensitive matching
        url = URL.objects.get( short__iexact = short )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
155
156
157
158
159
    except URL.DoesNotExist:
        raise Http404("Target URL not found.")

    url.clicks = url.clicks + 1
    url.save()
160
161
162
163
164
165
166
167

    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 )