Re-enable cookie from shibboleth.
[pithos] / pithos / im / views.py
1 # Copyright 2011 GRNET S.A. All rights reserved.
2
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
5 # conditions are met:
6
7 #   1. Redistributions of source code must retain the above
8 #      copyright notice, this list of conditions and the following
9 #      disclaimer.
10
11 #   2. Redistributions in binary form must reproduce the above
12 #      copyright notice, this list of conditions and the following
13 #      disclaimer in the documentation and/or other materials
14 #      provided with the distribution.
15
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
33
34 from datetime import datetime
35 from functools import wraps
36 from math import ceil
37
38 from django.conf import settings
39 from django.http import HttpResponse, HttpResponseRedirect
40 from django.utils.http import urlencode
41 from django.shortcuts import redirect
42 from django.template.loader import render_to_string
43
44 from models import User
45 from util import isoformat
46
47
48 def render_response(template, tab=None, status=200, **kwargs):
49     if tab is None:
50         tab = template.partition('_')[0]
51     kwargs.setdefault('tab', tab)
52     html = render_to_string(template, kwargs)
53     return HttpResponse(html, status=status)
54
55
56 def requires_admin(func):
57     @wraps(func)
58     def wrapper(request, *args):
59         if not settings.BYPASS_ADMIN_AUTH:
60             if not request.user:
61                 next = urlencode({'next': request.build_absolute_uri()})
62                 login_uri = settings.LOGIN_URL + '?' + next
63                 return HttpResponseRedirect(login_uri)
64             if not request.user_obj.is_admin:
65                 return HttpResponse('Forbidden', status=403)
66         return func(request, *args)
67     return wrapper
68
69
70 def index(request):
71     # TODO: Get and pass on next variable.
72     return render_response('index.html')
73
74
75 @requires_admin
76 def admin(request):
77     stats = {}
78     stats['users'] = User.objects.count()
79     return render_response('admin.html', tab='home', stats=stats)
80
81
82 @requires_admin
83 def users_list(request):
84     users = User.objects.order_by('id')
85     
86     filter = request.GET.get('filter', '')
87     if filter:
88         if filter.startswith('-'):
89             users = users.exclude(uniq__icontains=filter[1:])
90         else:
91             users = users.filter(uniq__icontains=filter)
92     
93     try:
94         page = int(request.GET.get('page', 1))
95     except ValueError:
96         page = 1
97     offset = max(0, page - 1) * settings.ADMIN_PAGE_LIMIT
98     limit = offset + settings.ADMIN_PAGE_LIMIT
99     
100     npages = int(ceil(1.0 * users.count() / settings.ADMIN_PAGE_LIMIT))
101     prev = page - 1 if page > 1 else None
102     next = page + 1 if page < npages else None
103     return render_response('users_list.html',
104                             users=users[offset:limit],
105                             filter=filter,
106                             pages=range(1, npages + 1),
107                             page=page,
108                             prev=prev,
109                             next=next)
110
111
112 @requires_admin
113 def users_create(request):
114     if request.method == 'GET':
115         return render_response('users_create.html')
116     if request.method == 'POST':
117         user = User()
118         user.uniq = request.POST.get('uniq')
119         user.realname = request.POST.get('realname')
120         user.is_admin = True if request.POST.get('admin') else False
121         user.affiliation = request.POST.get('affiliation')
122         user.quota = int(request.POST.get('quota') or 0) * (1024 ** 3)  # In GiB
123         user.renew_token()
124         user.save()
125         return redirect(users_info, user.id)
126
127
128 @requires_admin
129 def users_info(request, user_id):
130     user = User.objects.get(id=user_id)
131     return render_response('users_info.html', user=user)
132
133
134 @requires_admin
135 def users_modify(request, user_id):
136     user = User.objects.get(id=user_id)
137     user.uniq = request.POST.get('uniq')
138     user.realname = request.POST.get('realname')
139     user.is_admin = True if request.POST.get('admin') else False
140     user.affiliation = request.POST.get('affiliation')
141     user.quota = int(request.POST.get('quota') or 0) * (1024 ** 3)  # In GiB
142     user.auth_token = request.POST.get('auth_token')
143     try:
144         auth_token_expires = request.POST.get('auth_token_expires')
145         d = datetime.strptime(auth_token_expires, '%Y-%m-%dT%H:%MZ')
146         user.auth_token_expires = d
147     except ValueError:
148         pass
149     user.save()
150     return redirect(users_info, user.id)
151
152
153 @requires_admin
154 def users_delete(request, user_id):
155     user = User.objects.get(id=user_id)
156     user.delete()
157     return redirect(users_list)