blob: 4bf9e729cca7182f097c22490e5ea34df377508a [file] [log] [blame]
lombardofr099364f2018-06-12 11:21:02 +02001#
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#
16from .models import OsmUser
lombardofr07930222018-06-19 16:59:45 +020017from lib.osm.osmclient.clientv2 import Client
lombardofr099364f2018-06-12 11:21:02 +020018
19class OsmBackend(object):
20
21 def authenticate(self, **kwargs):
22 '''
23 kwargs will receive the python dict that may contain
24 {username, password, project-id} to authenticate
25 '''
26 if all(k in kwargs for k in ('username', 'password', 'project_id')):
27 username = kwargs['username']
28 password = kwargs['password']
29 project_id = kwargs['project_id']
30
lombardofr07930222018-06-19 16:59:45 +020031 client = Client()
32 result = client.auth(kwargs)
33 print "######"
34 print result
lombardofr099364f2018-06-12 11:21:02 +020035
lombardofr07930222018-06-19 16:59:45 +020036 if 'error' in result and result['error'] == True:
37 return None
38 else:
lombardofr099364f2018-06-12 11:21:02 +020039
lombardofr07930222018-06-19 16:59:45 +020040 try:
41 user = OsmUser.objects.get(username=username)
42
43 except OsmUser.DoesNotExist:
44 # Create a new user. There's no need to set a password
45 # we will keep just some preferences
46 user = OsmUser(username=username)
47
48 user.save()
49 user.session = result['data']
lombardofr099364f2018-06-12 11:21:02 +020050 return user
51
52 return None
53
54 def get_user(self, user_id):
55 try:
56 return OsmUser.objects.get(pk=user_id)
57 except OsmUser.DoesNotExist:
58 return None