From: lombardofr Date: Tue, 26 Jun 2018 21:44:40 +0000 (+0200) Subject: project: list, create, delete X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F48%2F6348%2F2;p=osm%2FLW-UI.git project: list, create, delete Change-Id: Ie609bdf317373ebc2fca3a400748f4c32f1c2088 Signed-off-by: lombardofr --- diff --git a/lib/osm/osmclient/clientv2.py b/lib/osm/osmclient/clientv2.py index 7103459..9acb590 100644 --- a/lib/osm/osmclient/clientv2.py +++ b/lib/osm/osmclient/clientv2.py @@ -39,6 +39,77 @@ class Client(object): return result + def project_list(self, token): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/yaml", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + + _url = "{0}/admin/v1/projects".format(self._base_path) + 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_get(self, token, id): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/yaml", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/admin/v1/projects/{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) + + def project_create(self, token, project_data): + + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + + _url = "{0}/admin/v1/projects".format(self._base_path) + + try: + r = requests.post(_url, json=project_data, verify=False, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + print r.status_code + if r.status_code == requests.codes.created: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + return result + + def project_delete(self, token, id): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/yaml", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + + _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id) + try: + r = requests.delete(_url, params=None, verify=False, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.no_content: + result['error'] = False + else: + result['data'] = Util.json_loads_byteified(r.text) + return result + def nsd_list(self, token): result = {'error': True, 'data': ''} headers = {"Content-Type": "application/yaml", "accept": "application/json", diff --git a/projecthandler/template/project/modal/new_project.html b/projecthandler/template/project/modal/new_project.html new file mode 100644 index 0000000..c346ec8 --- /dev/null +++ b/projecthandler/template/project/modal/new_project.html @@ -0,0 +1,32 @@ + \ No newline at end of file diff --git a/projecthandler/template/project/new_project.html b/projecthandler/template/project/new_project.html deleted file mode 100644 index 44fb435..0000000 --- a/projecthandler/template/project/new_project.html +++ /dev/null @@ -1,129 +0,0 @@ -{% extends "base.html" %} - -{% load staticfiles %} - -{% block head_block %} -{{ block.super }} - - - -{% endblock %} - -{% block left_sidebar %} - {% include 'left_sidebar_base.html' %} -{% endblock %} - -{% block breadcrumb_body %} -{{ block.super }} -
  • New Project
  • -{% endblock %} - -{% block content_body %} -{{ block.super }} -
    -
    - -
    -
    -

    New Project

    -
    - - -
    - {% csrf_token %} -
    -
    -
    -

    Project Type

    - - - -
    -
    -

    Project Name

    - -
    -
    -

    Info

    - -
    - - -
    -
    - -
    -
    -
    - {% autoescape off %}{{ type_container_template }}{% endautoescape %} -
    -
    - - -
    - -
    -
    - - -
    -
    -{% endblock %} - -{% block resource_block %} -{{ block.super }} - - - - - -{% endblock %} - diff --git a/projecthandler/template/project/projectlist.html b/projecthandler/template/project/projectlist.html index 033b0e0..6339679 100644 --- a/projecthandler/template/project/projectlist.html +++ b/projecthandler/template/project/projectlist.html @@ -1,5 +1,6 @@ {% extends "base.html" %} - +{% load get %} +{% load date_tag %} {% load staticfiles %} {% block head_block %} @@ -31,7 +32,10 @@

    Projects

    - New Project +
    @@ -40,12 +44,10 @@ Name - Info - Type + Modification Date Creation Date - Owner @@ -55,15 +57,13 @@ - {{ p.name }} + {{ p.name }} - {{ p.info }} - {{ p.get_type }} - {{ p.updated_date }} - {{ p.created_date }} - {{ p.owner.username }} - Delete + {{ p|get_sub:"_admin,modified"|get_date}} + {{ p|get_sub:"_admin,created"|get_date}} + + Delete {% endfor %} @@ -74,11 +74,89 @@
    +{% include "modal/new_project.html" %} {% endblock %} {% block resource_block %} {{ block.super }} - + {% endblock %} \ No newline at end of file diff --git a/projecthandler/views.py b/projecthandler/views.py index 6194b0a..0d50e10 100644 --- a/projecthandler/views.py +++ b/projecthandler/views.py @@ -38,18 +38,26 @@ def home(request): @login_required def create_new_project(request): - return render(request, 'home.html', {}) + if request.method == 'POST': + client = Client() + new_project_dict = request.POST.dict() + keys = ["name"] + project_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, new_project_dict.items())) + result = client.project_create(request.session['token'], project_data) + if isinstance(result, dict) and 'error' in result and result['error']: + print result + return __response_handler(request, result['data'], url=None, + status=result['data']['status'] if 'status' in result['data'] else 500) + else: + return __response_handler(request, {}, url=None, status=200) @login_required def user_projects(request): - csrf_token_value = get_token(request) - user = request.user - projects = user.get_projects() - + client = Client() + result = client.project_list(request.session['token']) return render(request, 'projectlist.html', { - 'projects': list(projects), - 'csrf_token': csrf_token_value + 'projects': result['data'] if result and result['error'] is False else [], }) @@ -85,23 +93,14 @@ def open_project(request, project_id=None): @login_required def delete_project(request, project_id=None): - if request.method == 'POST': - - try: - ##TODO delete project - return redirect('projects:projects_list') - except Exception as e: - print e - return render(request, 'error.html', {'error_msg': 'Error deleting Project.'}) - - elif request.method == 'GET': - try: - return render(request, 'osm/osm_project_delete.html', - {'project_id': project_id, 'project_name': project_id}) - - except Exception as e: - print e - return render(request, 'error.html', {'error_msg': 'Project not found.'}) + client = Client() + result = client.project_delete(request.session['token'], project_id) + if isinstance(result, dict) and 'error' in result and result['error']: + print result + return __response_handler(request, result['data'], url=None, + status=result['data']['status'] if 'status' in result['data'] else 500) + else: + return __response_handler(request, {}, url=None, status=200) @login_required @@ -198,7 +197,6 @@ def new_descriptor(request, project_id=None, descriptor_type=None): if result['error']: print result return __response_handler(request, result['data'], url=None, status=result['data']['status'] if 'status' in result['data'] else 500) - else: return __response_handler(request, {}, url=None, status=200)