]>
git.frykholm.com Git - tranquillity.git/blob - tranquillity/auth_backend.py
e8c682a34b1bdb89b8be167278240f9d377c0320
1 from django
.contrib
.auth
import get_user_model
2 from django
.contrib
.auth
.models
import User
3 from django
.contrib
.auth
.backends
import ModelBackend
5 from django
.conf
import settings
7 class EmailBackend(ModelBackend
):
8 def fetch_fortnox(self
):
9 """ Fetch all active customers from Fortnox API. Return as dict keyed on email."""
12 headers
= {"Access-Token":settings
.FORTNOX_ACCESS_TOKEN
,
13 "Client-Secret":settings
.FORTNOX_CLIENT_SECRET
,
14 "Content-Type":"application/json",
15 "Accept":"application/json" }
17 res
= requests
.get("https://api.fortnox.se/3/customers?filter=active", headers
=headers
)
18 for customer
in res
.json()['Customers']:
19 customers
[customer
['Email']] = customer
22 def authenticate(self
, username
=None, password
=None, **kwargs
):
23 UserModel
= get_user_model()
25 user
= UserModel
.objects
.get(email
=username
)
26 except UserModel
.DoesNotExist
:
27 customers
= self
.fetch_fortnox()
28 if username
in customers
:
29 if ' ' in customers
[username
]['Name']:
30 (fname
,lname
) = customers
[username
]['Name'].split(' ',1)
31 user
= User
.objects
.create_user(username
=username
,
37 fname
= customers
[username
]['Name']
38 user
= User
.objects
.create_user(username
=username
,
44 if user
.check_password(password
):