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
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
try:
return OsmUser.objects.get(pk=user_id)
except OsmUser.DoesNotExist:
- return None
\ No newline at end of file
+ return None
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})
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})
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",
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):
{% extends "project_details.html" %}
+{% load date_tag %}
{% block head_base %}
{{ block.super }}
{% endblock %}
<li><a><b>Project Name:</b> <span
class="pull-right">{{project_overview.name}}</span></a>
</li>
- <li><a><b>Info:</b> <span
- class="pull-right">{{project_overview.info}}</span></a></li>
- <li><a><b>Last updated:</b> <span
- class="pull-right">{{project_overview.updated_date}}</span></a>
+ <li><a><b>Created:</b> <span
+ class="pull-right">{{project_overview.created_date|get_date}}</span></a></li>
+ <li><a><b>Modified:</b> <span
+ class="pull-right">{{project_overview.updated_date|get_date}}</span></a>
</li>
- <li><a><b>Owner:</b> <span
- class="pull-right">{{project_overview.owner}}</span></a></li>
+
</ul>
</div>
<i class="fa fa-server fa-fw"></i> <span>VIM Accounts</span>
</a>
</li>
-
+ {% if user.is_admin %}
+ <li class="header">ADMIN</li>
+ {% url "users:list" as user_list_url %}
+ <li {% if request.get_full_path == user_list_url %} class="active" {% endif %}>
+ <a href='{% url "users:list" %}'>
+ <i class="fas fa-users"></i> <span>Users</span>
+ </a>
+ </li>
+ {% url "projects:projects_list" as proj_list_url %}
+ <li {% if request.get_full_path == proj_list_url %} class="active" {% endif %}>
+ <a href='{% url "projects:projects_list" %}'>
+ <i class="fas fa-folder"></i> <span>Projects</span>
+ </a>
+ </li>
+ {% endif %}
</ul>
</section>
<!-- /.sidebar -->
{% endblock %}
{% block left_sidebar %}
-{% include 'left_sidebar_base.html' %}
+{% include 'osm/osm_project_left_sidebar.html' %}
{% endblock %}
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())
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,
<div class="navbar-custom-menu">
<ul class="nav navbar-nav">
{% if user.get_username %}
+ <li class="dropdown tasks-menu">
+ <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
+ <span class="far fa-folder-open"> </span>
+ <span>{{ user.project_id }}</span>
+ <span class="fa fa-caret-down"></span>
+ </a>
+ <ul class="dropdown-menu">
+ <li class="header">Projects:</li>
+ <li>
+ <!-- inner menu: contains the actual data -->
+ <ul class="menu">
+ {% for p in request.session.projects %}
+ {% if p == user.project_id %}
+ <li>
+ <a href="#">
+ <h3>
+ <i class="fa fa-folder-open"></i> <span>{{ p }}</span>
+ </h3>
+
+ </a>
+ </li>
+ {% else %}
+ <li>
+ <a href="{% url 'projects:switch_project' project_id=p %}">
+ <h3>
+ <i class="fa fa-folder"></i> <span>{{ p }}</span>
+ </h3>
+ </a>
+ </li>
+ {% endif %}
+ {% endfor %}
+ </ul>
+ </li>
+
+ </ul>
+ </li>
<!-- User Account: style can be found in dropdown.less -->
<li class="dropdown user user-menu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<img src="/static/assets/img/account_circle.png" class="user-image" alt="User Image">
<span class="hidden-xs">{{ user.username }}</span>
</a>
- <ul class="dropdown-menu">
- <!-- User image -->
- <li class="user-header">
- <img src="/static/assets/img/account_circle.png" class="img-circle" alt="User Image">
- <p id="user_menu_name_role">
- {{ user.get_username }}
- <small id="user_menu_role">{{ user.get_user_role_name }}</small>
- </p>
- </li>
- <!-- Menu Footer-->
- <li class="user-footer">
- <div class="pull-left">
- <a href="#" class="btn btn-default btn-flat" disabled>Settings</a>
- </div>
- <div class="pull-right">
- <a href="/auth" class="btn btn-default btn-flat">Sign out</a>
- </div>
- </li>
- </ul>
+ <ul class="dropdown-menu">
+ <!-- User image -->
+ <li class="user-header">
+ <img src="/static/assets/img/account_circle.png" class="img-circle" alt="User Image">
+ <p id="user_menu_name_role">
+ {{ user.get_username }}
+ <small id="user_menu_role">{{ user.get_user_role_name }}</small>
+ </p>
+ </li>
+ <!-- Menu Footer-->
+ <li class="user-footer">
+ <div class="pull-left">
+ <a href="#" class="btn btn-default btn-flat" disabled>Settings</a>
+ </div>
+ <div class="pull-right">
+ <a href="/auth" class="btn btn-default btn-flat">Sign out</a>
+ </div>
+ </li>
+ </ul>
</li>
{% endif %}
</ul>