Commit 476af05e authored by Kostas Papadimitriou's avatar Kostas Papadimitriou
Browse files

Helpdesk improvements

- New json view to return a list of existing users with a prefix ending
  with "@" to be used for autocompletion in helpdesk account name input.
- Proper non existing account handling. If no vms or private networks exist
  for the specified account notify helpdesk user that the account
  doesn't seem to exist instead of displaying empty lists of vms/nets.
parent 2bbe42df
......@@ -3,7 +3,7 @@
"model": "db.VirtualMachine",
"pk": 1001,
"fields": {
"userid": "testuser",
"userid": "testuser@test.com",
"name": "user1 vm",
"created": "2011-02-06 00:00:00",
"updated": "2011-02-06 00:00:00",
......@@ -17,7 +17,7 @@
"model": "db.VirtualMachine",
"pk": 1002,
"fields": {
"userid": "testuser2",
"userid": "testuser2@test.com",
"name": "user2 vm1",
"created": "2011-02-10 00:00:00",
"updated": "2011-02-10 00:00:00",
......@@ -31,7 +31,7 @@
"model": "db.VirtualMachine",
"pk": 1003,
"fields": {
"userid": "testuser2",
"userid": "testuser2@test.com",
"name": "user2 vm2",
"created": "2011-02-10 00:00:00",
"updated": "2011-02-10 00:00:00",
......@@ -81,7 +81,7 @@
"state": "ACTIVE",
"public": 0,
"link": 1,
"userid": "testuser"
"userid": "testuser@test.com"
}
},
{
......
......@@ -5,6 +5,8 @@
{% endblock %}
{% block content %}
{% if account_exists %}
<div class="subnav">
<ul class="nav nav-pills">
......@@ -90,4 +92,7 @@
</div>
</div>
</div>
{% else %}
<p>Account <em>{{ account }}</em> does not exist in cyclades database</p>
{% endif %}
{% endblock %}
......@@ -75,20 +75,20 @@ class HelpdeskTests(TestCase):
# anonymous user gets 403
r = self.client.get(reverse('helpdesk-index'), user_token=None)
self.assertEqual(r.status_code, 403)
r = self.client.get(reverse('helpdesk-details', args=['testuser']),
r = self.client.get(reverse('helpdesk-details', args=['testuser@test.com']),
user_token=None)
self.assertEqual(r.status_code, 403)
# user not in helpdesk group gets 403
r = self.client.get(reverse('helpdesk-index'))
self.assertEqual(r.status_code, 403)
r = self.client.get(reverse('helpdesk-details', args=['testuser']))
r = self.client.get(reverse('helpdesk-details', args=['testuser@test.com']))
self.assertEqual(r.status_code, 403)
# user with helpdesk group gets 200
r = self.client.get(reverse('helpdesk-index'), user_token="0001")
self.assertEqual(r.status_code, 200)
r = self.client.get(reverse('helpdesk-details', args=['testuser']),
r = self.client.get(reverse('helpdesk-details', args=['testuser@test.com']),
user_token="0001")
self.assertEqual(r.status_code, 200)
......@@ -97,30 +97,60 @@ class HelpdeskTests(TestCase):
Test that view context data are filtered based on userid provided.
Check helpdesk_test.json to see the existing database data.
"""
r = self.client.get(reverse('helpdesk-details', args=['testuser']),
user_token="0001")
# 'testuser@test.com' details, see helpdes/fixtures/helpdesk_test.json for
# more details
r = self.client.get(reverse('helpdesk-details', args=['testuser@test.com']),
user_token="0001")
account = r.context['account']
vms = r.context['vms']
nets = r.context['networks']
self.assertEqual(account, "testuser")
self.assertEqual(account, "testuser@test.com")
self.assertEqual(vms[0].name, "user1 vm")
self.assertEqual(vms.count(), 1)
self.assertEqual(nets.count(), 2)
self.assertEqual(len(nets), 2)
self.assertEqual(r.context['account_exists'], True)
r = self.client.get(reverse('helpdesk-details', args=['testuser2']),
# 'testuser2@test.com' details, see helpdes/fixtures/helpdesk_test.json for
# more details
r = self.client.get(reverse('helpdesk-details', args=['testuser2@test.com']),
user_token="0001")
account = r.context['account']
vms = r.context['vms']
nets = r.context['networks']
self.assertEqual(account, "testuser2")
self.assertEqual(account, "testuser2@test.com")
self.assertEqual(vms[0].name, "user2 vm1")
self.assertEqual(vms[1].name, "user2 vm2")
self.assertEqual(vms.count(), 2)
self.assertEqual(nets.count(), 1)
self.assertEqual(len(nets), 1)
self.assertEqual(r.context['account_exists'], True)
# 'testuser5@test.com' does not exist, should be redirected to helpdesk home
r = self.client.get(reverse('helpdesk-details', args=['testuser5@test.com']),
user_token="0001")
vms = r.context['vms']
self.assertEqual(r.context['account_exists'], False)
self.assertEqual(vms.count(), 0)
# 1 public network
self.assertEqual(len(nets), 1)
self.assertEqual(r.context['account_exists'], False)
def test_json_users(self):
# invalid prefix gives 404 response
r = self.client.get(reverse('helpdesk-userslist') + "?prefix=test",
user_token="0001")
self.assertEqual(r.status_code, 404)
# no users exist
r = self.client.get(reverse('helpdesk-userslist') + "?prefix=test@",
user_token="0001")
self.assertEqual(r.status_code, 200)
self.assertEqual(r.content, "[]")
# 1 user exist
r = self.client.get(reverse('helpdesk-userslist') + "?prefix=testuser@",
user_token="0001")
self.assertEqual(r.status_code, 200)
self.assertEqual(r.content, '["testuser@test.com"]')
......@@ -2,6 +2,8 @@ from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('',
url(r'^$', 'synnefo.helpdesk.views.index', name='helpdesk-index'),
url(r'^api/users', 'synnefo.helpdesk.views.user_list',
name='helpdesk-userslist'),
url(r'^(?P<account>.*)$', 'synnefo.helpdesk.views.account',
name='helpdesk-details'),
)
......
from itertools import chain
from django.shortcuts import redirect
from django.views.generic.simple import direct_to_template
from django.db.models import get_apps
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.db.models import Q
from synnefo.db.models import *
from synnefo.lib.astakos import get_user, get_token_from_cookie
from django.http import Http404, HttpResponse
from django.utils import simplejson as json
from synnefo.lib.astakos import get_user, get_token_from_cookie
from synnefo.db.models import *
# TODO: here we mix ui setting with helpdesk
# TODO: here we mix ui setting with helpdesk settings
# if sometime in the future helpdesk gets splitted from the
# cyclades api code this should change and helpdesk should provide
# its own setting.
# its own setting HELPDESK_AUTH_COOKIE_NAME.
HELPDESK_AUTH_COOKIE = getattr(settings, 'UI_AUTH_COOKIE_NAME', '_pithos2_a')
def helpdesk_user_required(func, groups=['helpdesk']):
......@@ -22,7 +26,7 @@ def helpdesk_user_required(func, groups=['helpdesk']):
def wrapper(request, *args, **kwargs):
token = get_token_from_cookie(request, HELPDESK_AUTH_COOKIE)
get_user(request, settings.ASTAKOS_URL, fallback_token=token)
if hasattr(request, 'user'):
if hasattr(request, 'user') and request.user:
groups = request.user.get('groups', [])
if not groups:
......@@ -64,14 +68,37 @@ def account(request, account):
vms = VirtualMachine.objects.filter(userid=account).order_by('deleted')
# return all user private and public networks
netfilters = Q(userid=account, public=False) | Q(public=True)
networks = Network.objects.filter(netfilters).order_by('state')
public_networks = Network.objects.filter(public=True).order_by('state')
private_networks = Network.objects.filter(userid=account).order_by('state')
networks = list(public_networks) + list(private_networks)
account_exists = True
if vms.count() == 0 and private_networks.count() == 0:
account_exists = False
user_context = {
'vms': vms,
'account_exists': account_exists,
'account': account,
'vms': vms,
'networks': networks,
}
return direct_to_template(request, "helpdesk/account.html",
extra_context=user_context)
@helpdesk_user_required
def user_list(request):
"""
Return a json list of users based on the prefix provided. Prefix
should end with "@".
"""
prefix = request.GET.get('prefix', None)
if not prefix or not prefix.endswith("@"):
raise Http404
q = Q(userid__startswith=prefix) & ~Q(userid=None)
vm_users = VirtualMachine.objects.filter(q).values_list("userid", flat=True)
net_users = Network.objects.filter(q).values_list("userid", flat=True)
users = list(set(list(vm_users) + list(net_users)))
return HttpResponse(json.dumps(users), content_type="application/json")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment