Skip to content

Commit

Permalink
Merge pull request #23 from tkell/log-on-failure
Browse files Browse the repository at this point in the history
Log on failure
  • Loading branch information
jillh510 authored Apr 24, 2018
2 parents 34dc3b0 + a66a5a2 commit 9a013c7
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions sync/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@
from knackload import knackload

from django.conf import settings
from django.http import HttpResponse, HttpResponseForbidden
from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseForbidden
from django.http import HttpResponseServerError
from django.views.decorators.csrf import csrf_exempt

def log_debug(string):
if getattr(settings, 'DEBUG'):
print(string)

@csrf_exempt
def sync(request):
"""
Entry point. Receives a POST from ActBlue, transforms it, and POSTs it on to Knack
Returns a 200, 403, or 500 to ActBlue.
"""
authorized = auth(request)
if request.method == 'POST' and authorized:
actblue_data = json.loads(request.body)
Expand All @@ -23,12 +32,24 @@ def sync(request):
# This _could_ cause timeouts, but might be OK?
# Will depend on how many line items we get.
for knack_value in knack_values:
if getattr(settings, 'DEBUG'):
print("sent {} to knack".format(json.dumps(knack_value, indent=4)))
(return_status, result_string) = knackload.load( json.dumps(knack_value) )
result_data = json.loads(result_string)
if getattr(settings, 'DEBUG'):
print(json.dumps(result_data, indent=4))
log_debug('sent {} to knack'.format(json.dumps(knack_value, indent=4)))

return_status, result_string = knackload.load(json.dumps(knack_value))

if return_status != 200:
order_id = actblue_data['contribution']['orderNumber']
entity_id_key = settings.ACTBLUE_TO_KNACK_MAPPING_SCALARS['lineitems#entityId']
lineitem_entity_id = knack_value[entity_id_key]

error_msg = 'Error: We failed to send order {}, lineitem {} to knack'
print(error_msg.format(order_id, lineitem_entity_id))
log_debug('Error: We failed to send {}'.format(json.dumps(knack_value, indent=4)))

return HttpResponseServerError()
else:
result_data = json.loads(result_string)
log_debug(json.dumps(result_data, indent=4))

return HttpResponse('')
else:
return HttpResponseForbidden()
Expand All @@ -42,13 +63,13 @@ def get_lineitems(actblue_values, mapping):
"""
knack_lineitems = []
lineitems = actblue_values['lineitems']
amountKey = mapping['lineitems#amount']
entityKey = mapping['lineitems#entityId']
amount_key = mapping['lineitems#amount']
entity_key = mapping['lineitems#entityId']

for lineitem in lineitems:
knack_lineitem = {}
knack_lineitem[amountKey] = lineitem.get('amount')
knack_lineitem[entityKey] = lineitem.get('entityId')
knack_lineitem[amount_key] = lineitem.get('amount')
knack_lineitem[entity_key] = lineitem.get('entityId')
knack_lineitems.append(knack_lineitem)

return knack_lineitems
Expand All @@ -71,8 +92,8 @@ def transform(actblue_values):
for key, value in scalar_mapping:
path = key.split('#')
if isinstance(value, list):
for v in value:
knack_values[v] = walk(path, actblue_values)
for val in value:
knack_values[val] = walk(path, actblue_values)
else:
knack_values[value] = walk(path, actblue_values)

Expand Down Expand Up @@ -107,6 +128,11 @@ def walk(path, container):


def auth(request):
"""
Make sure an incoming request is authorized.
Returns a boolean.
"""
auth_header = request.META['HTTP_AUTHORIZATION']
encoded = auth_header.split(' ')[1].encode('ascii')
username, password = base64.urlsafe_b64decode(encoded).split(b':')
Expand Down

0 comments on commit 9a013c7

Please sign in to comment.