Private
Public Access
1
0

adding create_contact

This commit is contained in:
2024-11-18 19:56:52 +01:00
parent 9215e8b0bd
commit 97cc0594fc

View File

@@ -14,6 +14,16 @@ contacts_url = 'https://api.idoklad.cz/v3/Contacts'
from rowers.models import iDokladToken
idoklad_countries = json.loads(open('rowers/idoklad_countries.json').read())["Data"]["Items"]
def get_country_id(code):
for c in idoklad_countries:
if c['Code'] == code:
return c['Id']
return 1
def idoklad_token():
try:
token = iDokladToken.objects.get(id=1)
@@ -38,7 +48,7 @@ def idoklad_token():
if response.status_code == 200:
token = response.json()
token['updated_at'] = timezone.now()
iDokladToken.objects.filter(id=1).update(**token)
token = iDokladToken.objects.filter(id=1).update(**token)
return token
else:
return None
@@ -84,10 +94,14 @@ def get_contacts(rower):
return None # pragma
def create_contact(rower):
post_data = {
token = idoklad_token()
if token is None:
return None
data = {
'City': rower.city,
'CompanyName': rower.user.first_name+" "+rower.user.last_name,
'CountryId': rower.country.code, # country id
'CountryId': get_country_id(rower.country.code),
'Email': rower.user.email,
'Firstname': rower.user.first_name,
'PostalCode': rower.postal_code,
@@ -95,4 +109,27 @@ def create_contact(rower):
'Surname': rower.user.last_name,
'DeliveryAddresses': []
}
if rower.country.numeric is None:
data['CountryId'] = 1
dologging('idoklad.log','Creating idoklad contact for '+str(rower.user.email)+'\n')
headers = {
'Authorization': 'Bearer {t}'.format(t=token.access_token),
"Content-Type": "application/json",
"Accept": "application/json",
}
res = requests.post(contacts_url, json=data, headers=headers)
if res.status_code not in [200, 201]:
return 0
id = res.json()['Data']['Id']
return id