Created
June 19, 2017 01:34
-
-
Save mianos/30e80c054643120eb58b89a07b876333 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Blueprint, render_template, request, flash, jsonify, current_app | |
# from flask.ext.login import login_required | |
import requests | |
import xmltodict | |
from menu import add_menu | |
from jsonp import support_jsonp | |
from cnabort import abort | |
import forms | |
abn = Blueprint('abn', __name__, template_folder='templates') | |
@add_menu('ABN Tests', 'Test ABN', 'abn', abn) | |
@abn.route('/test', methods=['GET', 'POST']) | |
def test_abn(): | |
form = forms.ABNForm(request.form) | |
if request.method == 'POST': | |
if form.validate_on_submit(): | |
flash("Accepted ABN of '%s' name '%s' %s" % (form.abn.data, form.name.data, "Valid" if form.valid.data else "INVALID")) | |
else: | |
flash(str(form.errors), category='danger') | |
return render_template('abn.html', form=form) | |
@abn.route('/search_num') | |
@support_jsonp | |
def search_num(): | |
queryd = { | |
'searchString': None, | |
'includeHistoricalDetails': 'N', | |
'authenticationGuid': current_app.config['ABN_UID'] | |
} | |
args_dict = request.args.to_dict() | |
if 'searchString' in args_dict: | |
args_dict['searchString'] = "".join(args_dict['searchString'].split()) | |
else: | |
abort(400, {'error': 'searchString not provided'}) | |
queryd.update(args_dict) | |
uri = "%sv%s?" % (current_app.config['QUERY_BY_ABN'], current_app.config['QBA_VERSION']) | |
result = requests.get(uri, params=queryd, proxies=current_app.config['PROXIES']) | |
adict = xmltodict.parse(result.content) | |
adict = adict['ABRPayloadSearchResults']['response'] | |
if 'exception' in adict: | |
abort(400, {'error': adict['exception']['exceptionDescription']}) | |
# strip the version of the business entity to normalise the output for the service version | |
full_business_entity = 'businessEntity%s' % current_app.config['QBA_VERSION'] | |
adict['businessEntity'] = adict[full_business_entity] | |
del adict[full_business_entity] | |
if 'usageStatement' in adict: | |
del adict['usageStatement'] | |
return jsonify(adict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment