root / pithos / im / views.py @ 7e392d16
History | View | Annotate | Download (5.1 kB)
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 functools import wraps |
35 |
from math import ceil |
36 |
|
37 |
from django.conf import settings |
38 |
from django.http import HttpResponse, HttpResponseRedirect |
39 |
from django.utils.http import urlencode |
40 |
from django.shortcuts import redirect |
41 |
from django.template.loader import render_to_string |
42 |
|
43 |
from models import User |
44 |
|
45 |
|
46 |
def render_response(template, tab=None, status=200, **kwargs): |
47 |
if tab is None: |
48 |
tab = template.partition('_')[0] |
49 |
kwargs.setdefault('tab', tab)
|
50 |
html = render_to_string(template, kwargs) |
51 |
return HttpResponse(html, status=status)
|
52 |
|
53 |
|
54 |
def requires_admin(func): |
55 |
@wraps(func)
|
56 |
def wrapper(request, *args): |
57 |
if not settings.BYPASS_ADMIN_AUTH: |
58 |
if not request.user: |
59 |
next = urlencode({'next': request.build_absolute_uri()})
|
60 |
login_uri = settings.LOGIN_URL + '?' + next |
61 |
return HttpResponseRedirect(login_uri)
|
62 |
if not request.user_obj.is_admin: |
63 |
return HttpResponse('Forbidden', status=403) |
64 |
return func(request, *args)
|
65 |
return wrapper
|
66 |
|
67 |
|
68 |
def index(request): |
69 |
stats = {} |
70 |
stats['users'] = User.objects.count()
|
71 |
return render_response('index.html', tab='home', stats=stats) |
72 |
|
73 |
|
74 |
@requires_admin
|
75 |
def admin(request): |
76 |
stats = {} |
77 |
stats['users'] = User.objects.count()
|
78 |
return render_response('admin.html', tab='home', stats=stats) |
79 |
|
80 |
|
81 |
@requires_admin
|
82 |
def users_list(request): |
83 |
users = User.objects.order_by('id')
|
84 |
|
85 |
filter = request.GET.get('filter', '') |
86 |
if filter: |
87 |
if filter.startswith('-'): |
88 |
users = users.exclude(uniq__icontains=filter[1:]) |
89 |
else:
|
90 |
users = users.filter(uniq__icontains=filter)
|
91 |
|
92 |
try:
|
93 |
page = int(request.GET.get('page', 1)) |
94 |
except ValueError: |
95 |
page = 1
|
96 |
offset = max(0, page - 1) * settings.ADMIN_PAGE_LIMIT |
97 |
limit = offset + settings.ADMIN_PAGE_LIMIT |
98 |
|
99 |
npages = int(ceil(1.0 * users.count() / settings.ADMIN_PAGE_LIMIT)) |
100 |
prev = page - 1 if page > 1 else None |
101 |
next = page + 1 if page < npages else None |
102 |
return render_response('users_list.html', |
103 |
users=users[offset:limit], |
104 |
filter=filter,
|
105 |
pages=range(1, npages + 1), |
106 |
page=page, |
107 |
prev=prev, |
108 |
next=next)
|
109 |
|
110 |
|
111 |
@requires_admin
|
112 |
def users_create(request): |
113 |
if request.method == 'GET': |
114 |
return render_response('users_create.html') |
115 |
if request.method == 'POST': |
116 |
user = User() |
117 |
user.uniq = request.POST.get('uniq')
|
118 |
user.realname = request.POST.get('realname')
|
119 |
user.is_admin = True if request.POST.get('admin') else False |
120 |
user.affiliation = request.POST.get('affiliation')
|
121 |
user.quota = int(request.POST.get('quota') or 0) |
122 |
user.auth_token = request.POST.get('auth_token')
|
123 |
user.save() |
124 |
return redirect(users_info, user.id)
|
125 |
|
126 |
|
127 |
@requires_admin
|
128 |
def users_info(request, user_id): |
129 |
user = User.objects.get(id=user_id) |
130 |
return render_response('users_info.html', user=user) |
131 |
|
132 |
|
133 |
@requires_admin
|
134 |
def users_modify(request, user_id): |
135 |
user = User.objects.get(id=user_id) |
136 |
user.uniq = request.POST.get('uniq')
|
137 |
user.realname = request.POST.get('realname')
|
138 |
user.is_admin = True if request.POST.get('admin') else False |
139 |
user.affiliation = request.POST.get('affiliation')
|
140 |
user.quota = int(request.POST.get('quota') or 0) |
141 |
user.auth_token = request.POST.get('auth_token')
|
142 |
user.save() |
143 |
return redirect(users_info, user.id)
|
144 |
|
145 |
|
146 |
@requires_admin
|
147 |
def users_delete(request, user_id): |
148 |
user = User.objects.get(id=user_id) |
149 |
user.delete() |
150 |
return redirect(users_list)
|