<s> import json , re <EOL> from reconciliation . utils . json_helpers import render_to_json , render_to_json_via_template <EOL> from reconciliation . fec_reconciler import run_fec_query <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django . conf import settings <EOL> from django . views . decorators . cache import cache_page <EOL> from django . shortcuts import get_object_or_404 , render_to_response <EOL> from django . http import Http404 <EOL> from ftpdata . models import Candidate <EOL> try : <EOL> PREVIEW_BASE_URL = settings . PREVIEW_BASE_URL <EOL> PREVIEW_WIDTH = settings . PREVIEW_WIDTH <EOL> PREVIEW_HEIGHT = settings . PREVIEW_HEIGHT <EOL> except : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def get_metadata ( callbackarg , reconciliation_type ) : <EOL> reconciliation_name = '<STR_LIT>' <EOL> PREVIEW_BASE = "<STR_LIT>" <EOL> if ( reconciliation_type == '<STR_LIT>' or reconciliation_type == '<STR_LIT>' ) : <EOL> reconciliation_name = '<STR_LIT>' <EOL> if ( reconciliation_type == '<STR_LIT>' ) : <EOL> reconciliation_name = '<STR_LIT>' <EOL> PREVIEW_BASE = PREVIEW_BASE_URL % ( '<STR_LIT>' ) <EOL> else : <EOL> return Http404 <EOL> """<STR_LIT>""" <EOL> return render_to_json_via_template ( "<STR_LIT>" , { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : PREVIEW_BASE , <EOL> '<STR_LIT>' : PREVIEW_BASE , <EOL> '<STR_LIT>' : reconciliation_name , <EOL> '<STR_LIT>' : callbackarg , <EOL> '<STR_LIT>' : PREVIEW_WIDTH , <EOL> '<STR_LIT>' : PREVIEW_HEIGHT <EOL> } ) <EOL> def normalize_properties ( query ) : <EOL> properties = query . get ( '<STR_LIT>' ) <EOL> if not properties : <EOL> return None <EOL> newprop_array = [ ] <EOL> for this_prop in properties : <EOL> try : <EOL> this_prop [ '<STR_LIT>' ] <EOL> newprop_array . append ( { this_prop [ '<STR_LIT>' ] : this_prop [ '<STR_LIT:v>' ] } ) <EOL> except KeyError : <EOL> newprop_array . append ( { this_prop [ '<STR_LIT:p>' ] : this_prop [ '<STR_LIT:v>' ] } ) <EOL> return newprop_array <EOL> def flatten_properties ( query ) : <EOL> properties = query . get ( '<STR_LIT>' ) <EOL> if not properties : <EOL> return query <EOL> for this_prop in properties : <EOL> try : <EOL> this_prop [ '<STR_LIT>' ] <EOL> newprop_array . append ( { this_prop [ '<STR_LIT>' ] : this_prop [ '<STR_LIT:v>' ] } ) <EOL> except KeyError : <EOL> newprop_array . append ( { this_prop [ '<STR_LIT:p>' ] : this_prop [ '<STR_LIT:v>' ] } ) <EOL> return newprop_array <EOL> def do_fec_query ( query , fuzzy = True ) : <EOL> properties = normalize_properties ( query ) <EOL> state = None <EOL> office = None <EOL> cycle = None <EOL> if properties : <EOL> for thisproperty in properties : <EOL> for key in thisproperty : <EOL> if key == '<STR_LIT:state>' : <EOL> state = thisproperty [ '<STR_LIT:state>' ] <EOL> elif key == '<STR_LIT>' : <EOL> office = thisproperty [ '<STR_LIT>' ] <EOL> elif key == '<STR_LIT>' : <EOL> cycle = thisproperty [ '<STR_LIT>' ] <EOL> match_key_hash = run_fec_query ( query [ '<STR_LIT>' ] , state = state , office = office , cycle = cycle , fuzzy = fuzzy ) <EOL> return match_key_hash <EOL> def do_query ( query , reconciliation_type ) : <EOL> if reconciliation_type == '<STR_LIT>' : <EOL> return do_legislator_query ( query ) <EOL> elif reconciliation_type == '<STR_LIT>' : <EOL> return do_fec_query ( query , fuzzy = True ) <EOL> elif reconciliation_type == '<STR_LIT>' : <EOL> return do_fec_query ( query , fuzzy = False ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % ( reconciliation_type ) ) <EOL> @ csrf_exempt <EOL> def refine ( request , reconciliation_type ) : <EOL> if request . REQUEST . get ( '<STR_LIT>' ) : <EOL> callbackarg = request . REQUEST . get ( '<STR_LIT>' ) <EOL> return get_metadata ( callbackarg , reconciliation_type ) <EOL> query = request . REQUEST . get ( '<STR_LIT>' ) <EOL> queries = request . REQUEST . get ( '<STR_LIT>' ) <EOL> result = { } <EOL> if query : <EOL> print "<STR_LIT>" % query <EOL> if not re . search ( '<STR_LIT>' , query ) : <EOL> query = "<STR_LIT>" % query <EOL> q = json . loads ( query ) <EOL> result = do_query ( q , reconciliation_type ) <EOL> thisjson = { } <EOL> thisjson [ '<STR_LIT:result>' ] = result <EOL> return render_to_json ( json . dumps ( thisjson ) ) <EOL> elif queries : <EOL> q = json . loads ( queries ) <EOL> thisjson = { } <EOL> if q is not None : <EOL> for key , query in q . iteritems ( ) : <EOL> result = do_query ( query , reconciliation_type ) <EOL> thisjson [ key ] = { '<STR_LIT:result>' : result } <EOL> return render_to_json ( json . dumps ( thisjson ) ) <EOL> else : <EOL> message = "<STR_LIT>" <EOL> return render_to_json ( "<STR_LIT>" % message ) <EOL> @ csrf_exempt <EOL> def refine_json ( request , reconciliation_type ) : <EOL> fuzzy = True <EOL> if reconciliation_type == '<STR_LIT>' : <EOL> pass <EOL> elif reconciliation_type == '<STR_LIT>' : <EOL> fuzzy = False <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % ( reconciliation_type ) ) <EOL> queries = request . REQUEST . get ( '<STR_LIT>' ) <EOL> if queries : <EOL> q = json . loads ( queries ) <EOL> thisjson = { } <EOL> if q is not None : <EOL> for key , query in q . iteritems ( ) : <EOL> state = query . get ( '<STR_LIT:state>' , None ) <EOL> office = query . get ( '<STR_LIT>' , None ) <EOL> cycle = query . get ( '<STR_LIT>' , None ) <EOL> result = run_fec_query ( query [ '<STR_LIT>' ] , state = state , office = office , cycle = cycle , fuzzy = fuzzy ) <EOL> thisjson [ key ] = { '<STR_LIT:result>' : result } <EOL> return render_to_json ( json . dumps ( thisjson ) ) <EOL> else : <EOL> message = "<STR_LIT>" <EOL> return render_to_json ( "<STR_LIT>" % message ) <EOL> def preview ( request , fec_id ) : <EOL> candidates = Candidate . objects . filter ( cand_id = fec_id ) . order_by ( '<STR_LIT>' ) <EOL> try : <EOL> candidate = candidates [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> raise Http404 <EOL> return render_to_response ( '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : candidate , <EOL> '<STR_LIT>' : candidates , <EOL> '<STR_LIT>' : PREVIEW_HEIGHT - <NUM_LIT:30> , <EOL> '<STR_LIT>' : PREVIEW_WIDTH - <NUM_LIT:20> <EOL> } ) </s>
