From 835a0b7c317c0ad76a15f90acd01571cb1f62eb3 Mon Sep 17 00:00:00 2001 From: lombardofr Date: Wed, 18 Jul 2018 15:26:16 +0200 Subject: [PATCH] admin section in left sidebar; project switch in navbar Change-Id: I3a74e4aee11d479efa0b7fb41957a8e6e86fa1cd Signed-off-by: lombardofr --- authosm/backend.py | 19 ++--- authosm/models.py | 8 ++ authosm/views.py | 4 +- lib/osm/osmclient/clientv2.py | 17 +++++ .../project/osm/osm_project_details.html | 12 +-- .../project/osm/osm_project_left_sidebar.html | 16 +++- .../template/project/projectlist.html | 2 +- projecthandler/views.py | 7 +- template/base.html | 74 ++++++++++++++----- 9 files changed, 116 insertions(+), 43 deletions(-) diff --git a/authosm/backend.py b/authosm/backend.py index 6c23e91..9c316a9 100644 --- a/authosm/backend.py +++ b/authosm/backend.py @@ -19,8 +19,8 @@ from .models import OsmUser from lib.osm.osmclient.clientv2 import Client from .exceptions import OSMAuthException -class OsmBackend(object): +class OsmBackend(object): def authenticate(self, **kwargs): ''' kwargs will receive the python dict that may contain @@ -40,20 +40,17 @@ class OsmBackend(object): try: user = OsmUser.objects.get(username=username) user.psw = password - user.token=result['data']['id'] - user.project_id=result['data']['project_id'] - user.token_expires=result['data']['expires'] - user.session = result['data'] + user.token = result['data']['id'] + user.project_id = result['data']['project_id'] + user.token_expires = result['data']['expires'] + user.is_admin = bool(result['data']['admin']) user.save() - except OsmUser.DoesNotExist: user = OsmUser(username=username, psw=password, token=result['data']['id'], - project_id=result['data']['project_id'], - token_expires=result['data']['expires']) - user.session = result['data'] + project_id=result['data']['project_id'], + token_expires=result['data']['expires'], is_admin=result['data']['admin']) user.save() - return user return None @@ -62,4 +59,4 @@ class OsmBackend(object): try: return OsmUser.objects.get(pk=user_id) except OsmUser.DoesNotExist: - return None \ No newline at end of file + return None diff --git a/authosm/models.py b/authosm/models.py index 36a7b1b..790e684 100644 --- a/authosm/models.py +++ b/authosm/models.py @@ -89,6 +89,14 @@ class AbstractOsmUser(AbstractBaseUser, PermissionsMixin): return {'id': self.token, 'expires': self.token_expires, 'project_id': self.project_id} return None + def get_projects(self): + client = Client() + result = client.get_user_info(self.get_token(), self.username) + if 'error' in result and result['error'] is True: + return [] + else: + return result['data']['projects'] + def switch_project(self, project_id): client = Client() result = client.switch_project({'project_id': project_id, 'username': self.username, 'password': self.psw}) diff --git a/authosm/views.py b/authosm/views.py index 65b1ed6..f82f4dd 100644 --- a/authosm/views.py +++ b/authosm/views.py @@ -43,11 +43,11 @@ def user_login(request): if user and user.is_active: if user.is_authenticated: login(request, user) - request.session['token'] = user.session + request.session['projects'] = user.get_projects() if next_page == "" or next_page is None: return HttpResponseRedirect('/home') else: return HttpResponseRedirect(next_page) else: error_message = 'Login failed!' - return render(request, 'login.html', {'error_message':error_message, 'collapsed_sidebar': False}) + return render(request, 'login.html', {'error_message': error_message, 'collapsed_sidebar': False}) diff --git a/lib/osm/osmclient/clientv2.py b/lib/osm/osmclient/clientv2.py index 536ef65..5e4f390 100644 --- a/lib/osm/osmclient/clientv2.py +++ b/lib/osm/osmclient/clientv2.py @@ -110,6 +110,22 @@ class Client(object): result['data'] = Util.json_loads_byteified(r.text) return result + def get_user_info(self, token, id): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/yaml", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/admin/v1/users/{1}".format(self._base_path, id) + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + return result + def project_list(self, token): result = {'error': True, 'data': ''} headers = {"Content-Type": "application/yaml", "accept": "application/json", @@ -142,6 +158,7 @@ class Client(object): if r.status_code == requests.codes.ok: result['error'] = False result['data'] = Util.json_loads_byteified(r.text) + return result def project_create(self, token, project_data): diff --git a/projecthandler/template/project/osm/osm_project_details.html b/projecthandler/template/project/osm/osm_project_details.html index 5158d8f..d5edc1e 100644 --- a/projecthandler/template/project/osm/osm_project_details.html +++ b/projecthandler/template/project/osm/osm_project_details.html @@ -1,4 +1,5 @@ {% extends "project_details.html" %} +{% load date_tag %} {% block head_base %} {{ block.super }} {% endblock %} @@ -24,13 +25,12 @@
  • Project Name: {{project_overview.name}}
  • -
  • Info: {{project_overview.info}}
  • -
  • Last updated: {{project_overview.updated_date}} +
  • Created: {{project_overview.created_date|get_date}}
  • +
  • Modified: {{project_overview.updated_date|get_date}}
  • -
  • Owner: {{project_overview.owner}}
  • + diff --git a/projecthandler/template/project/osm/osm_project_left_sidebar.html b/projecthandler/template/project/osm/osm_project_left_sidebar.html index 82becc5..5bd34fd 100644 --- a/projecthandler/template/project/osm/osm_project_left_sidebar.html +++ b/projecthandler/template/project/osm/osm_project_left_sidebar.html @@ -78,7 +78,21 @@ VIM Accounts - + {% if user.is_admin %} +
  • ADMIN
  • + {% url "users:list" as user_list_url %} +
  • + + Users + +
  • + {% url "projects:projects_list" as proj_list_url %} +
  • + + Projects + +
  • + {% endif %} diff --git a/projecthandler/template/project/projectlist.html b/projecthandler/template/project/projectlist.html index 6e3793d..85abb59 100644 --- a/projecthandler/template/project/projectlist.html +++ b/projecthandler/template/project/projectlist.html @@ -12,7 +12,7 @@ {% endblock %} {% block left_sidebar %} -{% include 'left_sidebar_base.html' %} +{% include 'osm/osm_project_left_sidebar.html' %} {% endblock %} diff --git a/projecthandler/views.py b/projecthandler/views.py index 575d253..0f0cf6c 100644 --- a/projecthandler/views.py +++ b/projecthandler/views.py @@ -255,6 +255,7 @@ def open_project(request): client = Client() ##TODO change with adhoc api call + prj = client.project_get(user.get_token(), project_id) nsd = client.nsd_list(user.get_token()) vnfd = client.vnfd_list(user.get_token()) ns = client.ns_list(user.get_token()) @@ -262,9 +263,9 @@ def open_project(request): project_overview = { 'owner': user.username, 'name': project_id, - 'updated_date': '-', - 'created_date': '-', - 'info': '-', + 'updated_date': prj['data']['_admin']['modified'] if prj and prj['error'] is False else '-', + 'created_date': prj['data']['_admin']['created'] if prj and prj['error'] is False else '-', + 'type': 'osm', 'nsd': len(nsd['data']) if nsd and nsd['error'] is False else 0, 'vnfd': len(vnfd['data']) if vnfd and vnfd['error'] is False else 0, diff --git a/template/base.html b/template/base.html index 0932c59..bb56c29 100644 --- a/template/base.html +++ b/template/base.html @@ -49,31 +49,67 @@