blob: 17032330092e4b5773f0cbf15aa97ce413760e43 [file] [log] [blame]
lombardofr3218b2b2018-10-29 13:41:08 +01001#
2# Copyright 2018 EveryUP Srl
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
lombardofr10b52d12018-07-17 23:42:28 +020017from django.shortcuts import render, redirect
lombardofr4908f382018-09-10 11:36:06 +020018from sf_t3d.decorators import login_required
lombardofr10b52d12018-07-17 23:42:28 +020019from django.http import HttpResponse
20import json
21import logging
22import authosm.utils as osmutils
23from lib.osm.osmclient.clientv2 import Client
24
25logging.basicConfig(level=logging.DEBUG)
26log = logging.getLogger(__name__)
27
28
29@login_required
lombardofr8b12a9b2018-10-03 11:52:15 +020030def user_list(request):
lombardofr10b52d12018-07-17 23:42:28 +020031 user = osmutils.get_user(request)
32 client = Client()
33 result = client.user_list(user.get_token())
lombardofr8da23132019-06-02 17:18:48 +020034 result_projects = client.project_list(user.get_token())
35 p_map = {'admin': 'admin'}
36 for p in result_projects['data']:
37 p_map[p['_id']] = p['name']
38 users = result['data'] if result and result['error'] is False else []
39 for user in users:
40 user_project_ids = user['projects']
41 user_project_names = []
42 for p_id in user_project_ids:
43 if p_id in p_map:
44 user_project_names.append(p_map[p_id])
45 user['projects'] = user_project_names
46
lombardofr10b52d12018-07-17 23:42:28 +020047 result = {
48 'users': result['data'] if result and result['error'] is False else []
49 }
lombardofr8da23132019-06-02 17:18:48 +020050
lombardofr10b52d12018-07-17 23:42:28 +020051 return __response_handler(request, result, 'user_list.html')
52
53
54@login_required
55def create(request):
56 user = osmutils.get_user(request)
57 client = Client()
58 user_data ={
59 "username": request.POST['username'],
60 "password": request.POST['password'],
61 "projects": request.POST.getlist('projects')
62 }
lombardofr10b52d12018-07-17 23:42:28 +020063 result = client.user_create(user.get_token(), user_data)
lombardofra91d0322019-05-19 13:24:36 +020064 if result['error']:
65 return __response_handler(request, result['data'], url=None,
66 status=result['data']['status'] if 'status' in result['data'] else 500)
67 else:
68 return __response_handler(request, {}, url=None, status=200)
lombardofr10b52d12018-07-17 23:42:28 +020069
70
71@login_required
72def delete(request, user_id=None):
73 user = osmutils.get_user(request)
74 try:
75 client = Client()
lombardofra91d0322019-05-19 13:24:36 +020076 result = client.user_delete(user.get_token(), user_id)
lombardofr10b52d12018-07-17 23:42:28 +020077 except Exception as e:
78 log.exception(e)
lombardofra91d0322019-05-19 13:24:36 +020079 result = {'error': True, 'data': str(e)}
80 if result['error']:
81 return __response_handler(request, result['data'], url=None,
82 status=result['data']['status'] if 'status' in result['data'] else 500)
83 else:
84 return __response_handler(request, {}, url=None, status=200)
lombardofr10b52d12018-07-17 23:42:28 +020085
lombardofr88d09562018-09-11 18:14:39 +020086@login_required
87def update(request, user_id=None):
88 user = osmutils.get_user(request)
89 try:
90 client = Client()
lombardofr8b12a9b2018-10-03 11:52:15 +020091 projects_old = request.POST.get('projects_old').split(',')
92 projects_new = request.POST.getlist('projects')
93 default_project = request.POST.get('default_project')
94 projects_new.append(default_project)
95 projects_to_add = list(set(projects_new) - set(projects_old))
96 projects_to_remove = list(set(projects_old) - set(projects_new))
97
lombardofr5aea5b72018-10-15 14:35:59 +020098 project_payload = {}
lombardofr8b12a9b2018-10-03 11:52:15 +020099
100 for p in projects_to_remove:
lombardofr5aea5b72018-10-15 14:35:59 +0200101 project_payload["$"+str(p)] = None
lombardofr8b12a9b2018-10-03 11:52:15 +0200102 for p in projects_to_add:
103 if p not in projects_old:
lombardofr5aea5b72018-10-15 14:35:59 +0200104 project_payload["$+"+str(p)] = str(p)
105 project_payload["$" + default_project] = None
106 project_payload["$+[0]"] = default_project
107 payload = {}
108 if project_payload:
109 payload["projects"] = project_payload
110 if request.POST.get('password') and request.POST.get('password') is not '':
111 payload["password"] = request.POST.get('password')
lombardofr8b12a9b2018-10-03 11:52:15 +0200112
lombardofr5aea5b72018-10-15 14:35:59 +0200113 update_res = client.user_update(user.get_token(), user_id, payload)
lombardofr88d09562018-09-11 18:14:39 +0200114 except Exception as e:
115 log.exception(e)
lombardofr5aea5b72018-10-15 14:35:59 +0200116 update_res = {'error': True, 'data': str(e)}
117 if update_res['error']:
118 return __response_handler(request, update_res['data'], url=None,
119 status=update_res['data']['status'] if 'status' in update_res['data'] else 500)
120 else:
121 return __response_handler(request, {}, url=None, status=200)
122 #return __response_handler(request, {}, 'users:list', to_redirect=True, )
lombardofr88d09562018-09-11 18:14:39 +0200123
124
lombardofr10b52d12018-07-17 23:42:28 +0200125def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs):
126 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
127 if 'application/json' in raw_content_types or url is None:
128 return HttpResponse(json.dumps(data_res), content_type="application/json", *args, **kwargs)
129 elif to_redirect:
130 return redirect(url, *args, **kwargs)
131 else:
132 return render(request, url, data_res)