root / snf-cyclades-app / synnefo / helpdesk / views.py @ 9e3519e0
History | View | Annotate | Download (3.3 kB)
1 |
from itertools import chain |
---|---|
2 |
|
3 |
from django.shortcuts import redirect |
4 |
from django.views.generic.simple import direct_to_template |
5 |
from django.db.models import get_apps |
6 |
from django.conf import settings |
7 |
from django.core.exceptions import PermissionDenied |
8 |
from django.db.models import Q |
9 |
from django.http import Http404, HttpResponse |
10 |
from django.utils import simplejson as json |
11 |
|
12 |
from synnefo.lib.astakos import get_user, get_token_from_cookie |
13 |
from synnefo.db.models import * |
14 |
|
15 |
# TODO: here we mix ui setting with helpdesk settings
|
16 |
# if sometime in the future helpdesk gets splitted from the
|
17 |
# cyclades api code this should change and helpdesk should provide
|
18 |
# its own setting HELPDESK_AUTH_COOKIE_NAME.
|
19 |
HELPDESK_AUTH_COOKIE = getattr(settings, 'UI_AUTH_COOKIE_NAME', '_pithos2_a') |
20 |
|
21 |
def helpdesk_user_required(func, groups=['helpdesk']): |
22 |
"""
|
23 |
Django view wrapper that checks if identified request user has helpdesk
|
24 |
permissions (exists in helpdesk group)
|
25 |
"""
|
26 |
def wrapper(request, *args, **kwargs): |
27 |
token = get_token_from_cookie(request, HELPDESK_AUTH_COOKIE) |
28 |
get_user(request, settings.ASTAKOS_URL, fallback_token=token) |
29 |
if hasattr(request, 'user') and request.user: |
30 |
groups = request.user.get('groups', [])
|
31 |
|
32 |
if not groups: |
33 |
raise PermissionDenied
|
34 |
|
35 |
for g in groups: |
36 |
if not g in groups: |
37 |
raise PermissionDenied
|
38 |
else:
|
39 |
raise PermissionDenied
|
40 |
|
41 |
return func(request, *args, **kwargs)
|
42 |
|
43 |
return wrapper
|
44 |
|
45 |
|
46 |
@helpdesk_user_required
|
47 |
def index(request): |
48 |
"""
|
49 |
Helpdesk index view.
|
50 |
"""
|
51 |
|
52 |
# if form submitted redirect to details
|
53 |
account = request.GET.get('account', None) |
54 |
if account:
|
55 |
return redirect('synnefo.helpdesk.views.account', account=account) |
56 |
|
57 |
# show index template
|
58 |
return direct_to_template(request, "helpdesk/index.html") |
59 |
|
60 |
|
61 |
@helpdesk_user_required
|
62 |
def account(request, account): |
63 |
"""
|
64 |
Account details view.
|
65 |
"""
|
66 |
|
67 |
# all user vms
|
68 |
vms = VirtualMachine.objects.filter(userid=account).order_by('deleted')
|
69 |
|
70 |
# return all user private and public networks
|
71 |
public_networks = Network.objects.filter(public=True).order_by('state') |
72 |
private_networks = Network.objects.filter(userid=account).order_by('state')
|
73 |
networks = list(public_networks) + list(private_networks) |
74 |
|
75 |
account_exists = True
|
76 |
if vms.count() == 0 and private_networks.count() == 0: |
77 |
account_exists = False
|
78 |
|
79 |
user_context = { |
80 |
'account_exists': account_exists,
|
81 |
'account': account,
|
82 |
'vms': vms,
|
83 |
'networks': networks,
|
84 |
} |
85 |
return direct_to_template(request, "helpdesk/account.html", |
86 |
extra_context=user_context) |
87 |
|
88 |
@helpdesk_user_required
|
89 |
def user_list(request): |
90 |
"""
|
91 |
Return a json list of users based on the prefix provided. Prefix
|
92 |
should end with "@".
|
93 |
"""
|
94 |
|
95 |
prefix = request.GET.get('prefix', None) |
96 |
if not prefix or not prefix.endswith("@"): |
97 |
raise Http404
|
98 |
|
99 |
q = Q(userid__startswith=prefix) & ~Q(userid=None)
|
100 |
vm_users = VirtualMachine.objects.filter(q).values_list("userid", flat=True) |
101 |
net_users = Network.objects.filter(q).values_list("userid", flat=True) |
102 |
users = list(set(list(vm_users) + list(net_users))) |
103 |
return HttpResponse(json.dumps(users), content_type="application/json") |
104 |
|