from flask import (
    Blueprint,
    render_template,
    request,
    current_app,
    flash,
    abort,
    redirect,
    url_for
)

import requests

from check.frontend.forms import CheckForm

headers = {'Content-Type': 'application/json'}

frontend = Blueprint('frontend', __name__, template_folder='templates')


@frontend.route('/', methods=['GET', 'POST'])
def index():
    form = CheckForm()
    if form.validate_on_submit():
        try:
            register, key = form.data['register_id'].split(':')
            return redirect(url_for('frontend.check', register=register, key=key))
        except KeyError as e:
            message = "This application is not yet configured to check the %s register" % register
            flash(message)
            return redirect(url_for('.index'))

    return render_template('index.html', form=form)


@frontend.route('/check/<register>/<key>')
def check(register, key):
    try:
        url = _get_url(register, key)
        resp = requests.get(url, headers=headers)
        if resp.status_code == 200:
            entry = resp.json()
            address = _get_address(entry)
            return render_template('check.html', entry=entry, address=address)
        else:
            message = "There was a problem checking the %s register" % register
            flash(message)
            abort(resp.status_code)
    except KeyError as e:
        message = "This application is not yet configured to check the %sregister" % register
        flash(message)
    return redirect(url_for('.index'))


def _get_url(register, key):
    config_name = register.replace('-', '_').upper()
    reg_url = current_app.config[config_name]
    url = '%s/%s/%s.json' % (reg_url, register, key)
    return url


def _get_address(entry):
    address_key = entry['entry']['address']
    address_register_url = current_app.config['ADDRESS_REGISTER']
    url = '%s/address/%s.json' % (address_register_url, address_key)
    try:
        resp = requests.get(url, headers=headers)
        resp.raise_for_status()
        return resp.json()
    except Exception as e:
        return None

@@ -425,24 +425,71 @@
 n_submit():%0A
+        register_id = form.data%5B'register_id'%5D%0A
         try:
@@ -517,27 +517,16 @@
 , key = 
-form.data%5B'
 register
@@ -528,18 +528,16 @@
 ister_id
-'%5D
 .split('
@@ -634,32 +634,33 @@
      except 
-KeyError
+Exception
  as e:%0A     
@@ -683,53 +683,34 @@
  %22Th
-is application is not yet configured to
+ere was a problem
  check
+ing
  the
@@ -725,32 +725,35 @@
 ster%22 %25 register
+_id
 %0A            fla
@@ -780,43 +780,18 @@
     
-return redirect(url_for('.index'))%0A
+abort(500)
 %0A   
@@ -1019,40 +1019,32 @@
     
-if 
 resp.
-status_code == 200:%0A    
+raise_for_status()%0A
     
@@ -1075,20 +1075,16 @@
         
-    
 address 
@@ -1105,20 +1105,16 @@
 (entry)%0A
-    
         
@@ -1188,22 +1188,51 @@
     
-    else:%0A    
+except requests.exceptions.HTTPError as e:%0A
     
@@ -1286,36 +1286,49 @@
  %25s register
+ with key %25s
 %22 %25 
+(
 register
 %0A           
@@ -1311,29 +1311,31 @@
  %25 (register
-%0A    
+, key)%0A
         flas
@@ -1349,28 +1349,24 @@
 ge)%0A        
- 
-   
 abort(resp.s
@@ -1428,68 +1428,50 @@
  %22Th
-is application is not yet configured to
+ere was a problem
  check
+ing
  the %25s
+ 
 register
 %22 %25 
@@ -1466,28 +1466,41 @@
 register
+ with key %25s
 %22 %25 
+(
 register
 %0A       
@@ -1491,16 +1491,22 @@
 register
+, key)
 %0A       
@@ -1517,24 +1517,44 @@
 sh(message)%0A
+        abort(404)%0A%0A
     return r

clean up