adding idoklad.py
This commit is contained in:
98
rowers/idoklad.py
Normal file
98
rowers/idoklad.py
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
import requests
|
||||||
|
import json
|
||||||
|
from requests.auth import HTTPBasicAuth
|
||||||
|
import urllib.parse
|
||||||
|
from rowers.utils import dologging
|
||||||
|
import datetime
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
from rowsandall_app.settings import (
|
||||||
|
IDOKLAD_CLIENT_ID, IDOKLAD_CLIENT_SECRET,
|
||||||
|
)
|
||||||
|
|
||||||
|
contacts_url = 'https://api.idoklad.cz/v3/Contacts'
|
||||||
|
|
||||||
|
from rowers.models import iDokladToken
|
||||||
|
|
||||||
|
def idoklad_token():
|
||||||
|
try:
|
||||||
|
token = iDokladToken.objects.get(id=1)
|
||||||
|
except iDokladToken.DoesNotExist:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if token.updated_at + datetime.timedelta(seconds=token.expires_in) < timezone.now():
|
||||||
|
headers = {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
}
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'grant_type': 'refresh_token',
|
||||||
|
'client_id': IDOKLAD_CLIENT_ID,
|
||||||
|
'client_secret': IDOKLAD_CLIENT_SECRET,
|
||||||
|
'scope': 'eet offline_access',
|
||||||
|
'refresh_token': token.refresh_token,
|
||||||
|
}
|
||||||
|
|
||||||
|
response = requests.post('https://identity.idoklad.cz/server/connect/token', headers=headers, data=data)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
token = response.json()
|
||||||
|
token['updated_at'] = timezone.now()
|
||||||
|
iDokladToken.objects.filter(id=1).update(**token)
|
||||||
|
return token
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return token
|
||||||
|
|
||||||
|
def prepare_invoice():
|
||||||
|
token = idoklad_token()
|
||||||
|
if token is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'templateId': 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_contacts(rower):
|
||||||
|
token = idoklad_token()
|
||||||
|
if token is None:
|
||||||
|
return None
|
||||||
|
headers = {
|
||||||
|
'Authorization': 'Bearer {t}'.format(t=token.access_token),
|
||||||
|
}
|
||||||
|
|
||||||
|
url = contacts_url+'?filter=(Email~eq~'+urllib.parse.quote(rower.user.email)+')'
|
||||||
|
dologging('idoklad.log','Searching Contact url: '+str(url))
|
||||||
|
|
||||||
|
res = requests.get(url, headers=headers)
|
||||||
|
|
||||||
|
dologging('idoklad.log','Searching Contact Status code '+str(res.status_code)+'\n')
|
||||||
|
|
||||||
|
if res.status_code != 200: # pragma: no cover
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
dologging('idoklad.log','Status Code '+json.dumps(res.json())+'\n')
|
||||||
|
|
||||||
|
data = res.json()['Data']['Items']
|
||||||
|
|
||||||
|
if len(data) >= 1:
|
||||||
|
r = data[0]
|
||||||
|
return r['Id']
|
||||||
|
|
||||||
|
return None # pragma
|
||||||
|
|
||||||
|
def create_contact(rower):
|
||||||
|
post_data = {
|
||||||
|
'City': rower.city,
|
||||||
|
'CompanyName': rower.user.first_name+" "+rower.user.last_name,
|
||||||
|
'CountryId': rower.country.code, # country id
|
||||||
|
'Email': rower.user.email,
|
||||||
|
'Firstname': rower.user.first_name,
|
||||||
|
'PostalCode': rower.postal_code,
|
||||||
|
'Street': rower.street_address,
|
||||||
|
'Surname': rower.user.last_name,
|
||||||
|
'DeliveryAddresses': []
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user