adding create_contact
This commit is contained in:
@@ -14,6 +14,16 @@ contacts_url = 'https://api.idoklad.cz/v3/Contacts'
|
|||||||
|
|
||||||
from rowers.models import iDokladToken
|
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():
|
def idoklad_token():
|
||||||
try:
|
try:
|
||||||
token = iDokladToken.objects.get(id=1)
|
token = iDokladToken.objects.get(id=1)
|
||||||
@@ -38,7 +48,7 @@ def idoklad_token():
|
|||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
token = response.json()
|
token = response.json()
|
||||||
token['updated_at'] = timezone.now()
|
token['updated_at'] = timezone.now()
|
||||||
iDokladToken.objects.filter(id=1).update(**token)
|
token = iDokladToken.objects.filter(id=1).update(**token)
|
||||||
return token
|
return token
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
@@ -84,10 +94,14 @@ def get_contacts(rower):
|
|||||||
return None # pragma
|
return None # pragma
|
||||||
|
|
||||||
def create_contact(rower):
|
def create_contact(rower):
|
||||||
post_data = {
|
token = idoklad_token()
|
||||||
|
if token is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
data = {
|
||||||
'City': rower.city,
|
'City': rower.city,
|
||||||
'CompanyName': rower.user.first_name+" "+rower.user.last_name,
|
'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,
|
'Email': rower.user.email,
|
||||||
'Firstname': rower.user.first_name,
|
'Firstname': rower.user.first_name,
|
||||||
'PostalCode': rower.postal_code,
|
'PostalCode': rower.postal_code,
|
||||||
@@ -95,4 +109,27 @@ def create_contact(rower):
|
|||||||
'Surname': rower.user.last_name,
|
'Surname': rower.user.last_name,
|
||||||
'DeliveryAddresses': []
|
'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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user