1 # Copyright 2011 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
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.
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.
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.
34 from datetime import datetime
35 from functools import wraps
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
44 from models import User
45 from util import isoformat
48 def render_response(template, tab=None, status=200, **kwargs):
50 tab = template.partition('_')[0]
51 kwargs.setdefault('tab', tab)
52 html = render_to_string(template, kwargs)
53 return HttpResponse(html, status=status)
56 def requires_admin(func):
58 def wrapper(request, *args):
59 if not settings.BYPASS_ADMIN_AUTH:
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)
71 # TODO: Get and pass on next variable.
72 return render_response('index.html')
78 stats['users'] = User.objects.count()
79 return render_response('admin.html', tab='home', stats=stats)
83 def users_list(request):
84 users = User.objects.order_by('id')
86 filter = request.GET.get('filter', '')
88 if filter.startswith('-'):
89 users = users.exclude(uniq__icontains=filter[1:])
91 users = users.filter(uniq__icontains=filter)
94 page = int(request.GET.get('page', 1))
97 offset = max(0, page - 1) * settings.ADMIN_PAGE_LIMIT
98 limit = offset + settings.ADMIN_PAGE_LIMIT
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],
106 pages=range(1, npages + 1),
113 def users_create(request):
114 if request.method == 'GET':
115 return render_response('users_create.html')
116 if request.method == 'POST':
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
125 return redirect(users_info, user.id)
129 def users_info(request, user_id):
130 user = User.objects.get(id=user_id)
131 return render_response('users_info.html', user=user)
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')
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
150 return redirect(users_info, user.id)
154 def users_delete(request, user_id):
155 user = User.objects.get(id=user_id)
157 return redirect(users_list)