]> git.frykholm.com Git - tranquillity.git/commitdiff
Added initial fortnox api client.
authorMikael Frykholm <mikael@frykholm.com>
Mon, 14 May 2018 11:05:13 +0000 (13:05 +0200)
committerMikael Frykholm <mikael@frykholm.com>
Mon, 14 May 2018 11:05:13 +0000 (13:05 +0200)
.gitignore
tranquillity/auth_backend.py
tranquillity/settings.py

index c1cc9338c4c50027f5c24dbb024bf9dfc9b3a85b..1fe043ace5c5a351ae8537335d323f21b3103d72 100644 (file)
@@ -1,3 +1,4 @@
 __pycache__/
 *.py[cod]
 /db.sqlite3
 __pycache__/
 *.py[cod]
 /db.sqlite3
+/tranquillity/settings_secret.py
index 44edaeebb9d25163ad65d7883141dc2b30a582af..bd410d15185cc1aacca6a58c14e6bc136531d914 100644 (file)
@@ -1,12 +1,43 @@
 from django.contrib.auth import get_user_model
 from django.contrib.auth import get_user_model
+from django.contrib.auth.models import User
 from django.contrib.auth.backends import ModelBackend
 from django.contrib.auth.backends import ModelBackend
+import requests
 
 class EmailBackend(ModelBackend):
 
 class EmailBackend(ModelBackend):
+    def fetch_fortnox(self):
+        """ Fetch all active customers from Fortnox API. Return as dict keyed on email."""
+        res = None
+        customers = {}
+        headers = {"Access-Token":FORTNOX_Access_Token,
+           "Client-Secret":FORTNOX_Client_Secret,
+           "Content-Type":"application/json",
+           "Accept":"application/json" }
+
+        res = requests.get("https://api.fortnox.se/3/customers?filter=active", headers=headers)
+        for customer in res.json()['Customers']:
+            customers[customer['Email']] = customer
+        return customers
+
     def authenticate(self, username=None, password=None, **kwargs):
         UserModel = get_user_model()
         try:
             user = UserModel.objects.get(email=username)
         except UserModel.DoesNotExist:
     def authenticate(self, username=None, password=None, **kwargs):
         UserModel = get_user_model()
         try:
             user = UserModel.objects.get(email=username)
         except UserModel.DoesNotExist:
+            customers = self.fetch_fortnox()
+            if username in customers:
+                if ' ' in customers[username]['Name']:
+                    (fname,lname) = customers[username]['Name'].split(' ',1)
+                    user = User.objects.create_user(username=username,
+                                     email=username,
+                                     first_name=fname,
+                                     last_name=lname)
+                    return user
+                else:
+                    fname = customers[username]['Name']
+                    user = User.objects.create_user(username=username,
+                                 email=username,
+                                 first_name=fname)
+                    return user
             return None
         else:
             if user.check_password(password):
             return None
         else:
             if user.check_password(password):
index cc3367792137bf11fa5d361f7d6ea5da970aec8e..74d30b0e42d6933159f42cbd5bb2363829863ba5 100644 (file)
@@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/2.0/ref/settings/
 """
 
 import os
 """
 
 import os
+from settings_secret import *
 
 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))