]>
git.frykholm.com Git - tranquillity.git/blob - tranquillity/auth_backend.py
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
6 class EmailBackend(ModelBackend
):
7 def fetch_fortnox(self
):
8 """ Fetch all active customers from Fortnox API. Return as dict keyed on email."""
11 headers
= {"Access-Token":FORTNOX_Access_Token
,
12 "Client-Secret":FORTNOX_Client_Secret
,
13 "Content-Type":"application/json",
14 "Accept":"application/json" }
16 res
= requests
.get("https://api.fortnox.se/3/customers?filter=active", headers
=headers
)
17 for customer
in res
.json()['Customers']:
18 customers
[customer
['Email']] = customer
21 def authenticate(self
, username
=None, password
=None, **kwargs
):
22 UserModel
= get_user_model()
24 user
= UserModel
.objects
.get(email
=username
)
25 except UserModel
.DoesNotExist
:
26 customers
= self
.fetch_fortnox()
27 if username
in customers
:
28 if ' ' in customers
[username
]['Name']:
29 (fname
,lname
) = customers
[username
]['Name'].split(' ',1)
30 user
= User
.objects
.create_user(username
=username
,
36 fname
= customers
[username
]['Name']
37 user
= User
.objects
.create_user(username
=username
,
43 if user
.check_password(password
):