blob: 0c12a848acb72349a2df0db3ede27b926d072b6d [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())
34 result = {
35 'users': result['data'] if result and result['error'] is False else []
36 }
37 return __response_handler(request, result, 'user_list.html')
38
39
40@login_required
41def create(request):
42 user = osmutils.get_user(request)
43 client = Client()
44 user_data ={
45 "username": request.POST['username'],
46 "password": request.POST['password'],
47 "projects": request.POST.getlist('projects')
48 }
lombardofr10b52d12018-07-17 23:42:28 +020049 result = client.user_create(user.get_token(), user_data)
lombardofra91d0322019-05-19 13:24:36 +020050 if result['error']:
51 return __response_handler(request, result['data'], url=None,
52 status=result['data']['status'] if 'status' in result['data'] else 500)
53 else:
54 return __response_handler(request, {}, url=None, status=200)
lombardofr10b52d12018-07-17 23:42:28 +020055
56
57@login_required
58def delete(request, user_id=None):
59 user = osmutils.get_user(request)
60 try:
61 client = Client()
lombardofra91d0322019-05-19 13:24:36 +020062 result = client.user_delete(user.get_token(), user_id)
lombardofr10b52d12018-07-17 23:42:28 +020063 except Exception as e:
64 log.exception(e)
lombardofra91d0322019-05-19 13:24:36 +020065 result = {'error': True, 'data': str(e)}
66 if result['error']:
67 return __response_handler(request, result['data'], url=None,
68 status=result['data']['status'] if 'status' in result['data'] else 500)
69 else:
70 return __response_handler(request, {}, url=None, status=200)
lombardofr10b52d12018-07-17 23:42:28 +020071
lombardofr88d09562018-09-11 18:14:39 +020072@login_required
73def update(request, user_id=None):
74 user = osmutils.get_user(request)
75 try:
76 client = Client()
lombardofr8b12a9b2018-10-03 11:52:15 +020077 projects_old = request.POST.get('projects_old').split(',')
78 projects_new = request.POST.getlist('projects')
79 default_project = request.POST.get('default_project')
80 projects_new.append(default_project)
81 projects_to_add = list(set(projects_new) - set(projects_old))
82 projects_to_remove = list(set(projects_old) - set(projects_new))
83
lombardofr5aea5b72018-10-15 14:35:59 +020084 project_payload = {}
lombardofr8b12a9b2018-10-03 11:52:15 +020085
86 for p in projects_to_remove:
lombardofr5aea5b72018-10-15 14:35:59 +020087 project_payload["$"+str(p)] = None
lombardofr8b12a9b2018-10-03 11:52:15 +020088 for p in projects_to_add:
89 if p not in projects_old:
lombardofr5aea5b72018-10-15 14:35:59 +020090 project_payload["$+"+str(p)] = str(p)
91 project_payload["$" + default_project] = None
92 project_payload["$+[0]"] = default_project
93 payload = {}
94 if project_payload:
95 payload["projects"] = project_payload
96 if request.POST.get('password') and request.POST.get('password') is not '':
97 payload["password"] = request.POST.get('password')
lombardofr8b12a9b2018-10-03 11:52:15 +020098
lombardofr5aea5b72018-10-15 14:35:59 +020099 update_res = client.user_update(user.get_token(), user_id, payload)
lombardofr88d09562018-09-11 18:14:39 +0200100 except Exception as e:
101 log.exception(e)
lombardofr5aea5b72018-10-15 14:35:59 +0200102 update_res = {'error': True, 'data': str(e)}
103 if update_res['error']:
104 return __response_handler(request, update_res['data'], url=None,
105 status=update_res['data']['status'] if 'status' in update_res['data'] else 500)
106 else:
107 return __response_handler(request, {}, url=None, status=200)
108 #return __response_handler(request, {}, 'users:list', to_redirect=True, )
lombardofr88d09562018-09-11 18:14:39 +0200109
110
lombardofr10b52d12018-07-17 23:42:28 +0200111def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs):
112 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
113 if 'application/json' in raw_content_types or url is None:
114 return HttpResponse(json.dumps(data_res), content_type="application/json", *args, **kwargs)
115 elif to_redirect:
116 return redirect(url, *args, **kwargs)
117 else:
118 return render(request, url, data_res)