Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log on failure #23

Merged
merged 5 commits into from
Apr 24, 2018
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ' + knack_value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to json.dumps here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!


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