admin section in left sidebar; project switch in navbar 68/6368/1
authorlombardofr <lombardo@everyup.it>
Wed, 18 Jul 2018 13:26:16 +0000 (15:26 +0200)
committerlombardofr <lombardo@everyup.it>
Thu, 19 Jul 2018 12:40:12 +0000 (14:40 +0200)
Change-Id: I3a74e4aee11d479efa0b7fb41957a8e6e86fa1cd
Signed-off-by: lombardofr <lombardo@everyup.it>
authosm/backend.py
authosm/models.py
authosm/views.py
lib/osm/osmclient/clientv2.py
projecthandler/template/project/osm/osm_project_details.html
projecthandler/template/project/osm/osm_project_left_sidebar.html
projecthandler/template/project/projectlist.html
projecthandler/views.py
template/base.html

index 6c23e91..9c316a9 100644 (file)
@@ -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
index 36a7b1b..790e684 100644 (file)
@@ -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})
index 65b1ed6..f82f4dd 100644 (file)
@@ -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})
index 536ef65..5e4f390 100644 (file)
@@ -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):
 
index 5158d8f..d5edc1e 100644 (file)
@@ -1,4 +1,5 @@
 {% 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>
index 82becc5..5bd34fd 100644 (file)
                     <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 -->
index 6e3793d..85abb59 100644 (file)
@@ -12,7 +12,7 @@
 {% endblock %}
 {% block left_sidebar %}
 
-{% include 'left_sidebar_base.html' %}
+{% include 'osm/osm_project_left_sidebar.html' %}
 
 {% endblock %}
 
index 575d253..0f0cf6c 100644 (file)
@@ -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,
index 0932c59..bb56c29 100644 (file)
       <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>