def set_loader_timeout(racedate): if request.method == 'POST': payload = utils.clean_payload(dict(request.form)) timeout = payload.get('timeout', '') os.system('echo export ELEX_LOADER_TIMEOUT=%s > /tmp/elex_loader_timeout.sh' % timeout) return json.dumps({"message": "success", "output": "0"})
def set_loader_timeout(raceyear): if request.method == 'POST': payload = utils.clean_payload(dict(request.form)) timeout = payload.get('timeout', '') os.system( 'echo export ELEX_LOADER_TIMEOUT=%s > /tmp/elex_loader_timeout.sh' % timeout) return json.dumps({"message": "success", "output": "0"})
def index(): if request.method == 'GET': context = utils.build_context(request) page = utils.get_page(request) all_entities = models.Entity.select()\ .where(models.Entity.active == True)\ .where(models.Entity.canonical_entity >> None) context['entities'] = all_entities\ .order_by(models.Entity.created.desc())\ .paginate(page, settings.ITEMS_PER_PAGE) context = utils.paginate(request, all_entities.count(), page, context) return render_template('entity_list.html', **context) if request.method == 'POST': payload = utils.clean_payload(dict(request.form)) if not payload.get('name', None): return Response('bad request', 400) lookup = dict([(e.name, e.id) for e in models.Entity.select()]) entity_list = list(lookup.keys()) name = payload['name'] score = 0 if len(entity_list) > 0: name, score = process.extractOne(payload['name'], entity_list) response = {} response['request'] = {} response['request']['name'] = payload['name'] response['request']['create_if_below'] = payload.get( 'create_if_below', settings.MINIMUM_SCORE) response['response'] = {} response['response']['score'] = score if payload.get('create_if_below', None): if score < int(payload['create_if_below']): response = create_entity(response) return jsonify(response) if score < settings.MINIMUM_SCORE: response = create_entity(response) return jsonify(response) response['response']['created'] = False response['response']['name'] = name response['response']['uuid'] = lookup[name] return jsonify(response)
def candidate_detail(racedate, candidateid): racedate_db = PostgresqlExtDatabase('elex_%s' % racedate, user=os.environ.get('ELEX_ADMIN_USER', 'elex'), host=os.environ.get('ELEX_ADMIN_HOST', '127.0.0.1') ) models.database_proxy.initialize(racedate_db) if request.method == 'POST': payload = utils.clean_payload(dict(request.form)) try: oc = models.OverrideCandidate.get(models.OverrideCandidate.candidate_candidateid == candidateid) except models.OverrideCandidate.DoesNotExist: oc = models.OverrideCandidate.create(candidate_candidateid=candidateid) utils.update_model(oc, payload) utils.update_views(models.database_proxy) return json.dumps({"message": "success"})
def candidate_order(racedate): racedate_db = PostgresqlExtDatabase('elex_%s' % racedate, user=os.environ.get('ELEX_ADMIN_USER', 'elex'), host=os.environ.get('ELEX_ADMIN_HOST', '127.0.0.1') ) models.database_proxy.initialize(racedate_db) if request.method == 'POST': payload = utils.clean_payload(dict(request.form)) if payload.get('candidates', None): print payload['candidates'] for idx, candidateid in enumerate(payload['candidates'].split(',')): oc = models.OverrideCandidate.update(nyt_display_order=idx).where(models.OverrideCandidate.candidate_candidateid == candidateid) oc.execute() utils.update_views(models.database_proxy) return json.dumps({"message": "success"})
def remove_canonical_entity(): if request.method == "POST": payload = utils.clean_payload(dict(request.form)) print("Payload: ", payload) if payload.get('entity', None): if utils.valid_uuid(payload['entity']): e = models.Entity\ .update(canonical_entity=None)\ .where(models.Entity.id==payload['entity']) e.execute() return (jsonify({ "entity": payload['entity'], "canonical_entity": None })) return Response('bad request', 400) return Response('bad request', 400) return Response('bad request', 400)
def candidate_detail(racedate, candidateid, raceyear): racedate_db = PostgresqlExtDatabase( 'elex_%s' % racedate, user=os.environ.get('ELEX_ADMIN_USER', 'elex'), host=os.environ.get('ELEX_ADMIN_HOST', '127.0.0.1')) models.database_proxy.initialize(racedate_db) if request.method == 'POST': payload = utils.clean_payload(dict(request.form)) try: oc = models.OverrideCandidate.get( models.OverrideCandidate.candidate_unique_id == candidateid) except models.OverrideCandidate.DoesNotExist: oc = models.OverrideCandidate.create( candidate_unique_id=candidateid) utils.update_model(oc, payload) utils.update_views(models.database_proxy) return json.dumps({"message": "success"})
def state_detail(racedate, statepostal): racedate_db = PostgresqlExtDatabase('elex_%s' % racedate, user=os.environ.get('ELEX_ADMIN_USER', 'elex'), host=os.environ.get('ELEX_ADMIN_HOST', '127.0.0.1') ) models.database_proxy.initialize(racedate_db) if request.method == 'POST': payload = utils.clean_payload(dict(request.form)) races = ["%s-%s" % (r.statepostal, r.raceid) for r in models.ElexRace.select().where(models.ElexRace.statepostal == statepostal)] for r in races: o = models.OverrideRace.get( models.OverrideRace.race_raceid == r.split('-')[1], models.OverrideRace.race_statepostal == r.split('-')[0] ) o.report=payload['report'] o.report_description=payload['report_description'] o.save() utils.update_views(models.database_proxy) return json.dumps({"message": "success"})
def state_detail(racedate, statepostal, raceyear): racedate_db = PostgresqlExtDatabase( 'elex_%s' % racedate, user=os.environ.get('ELEX_ADMIN_USER', 'elex'), host=os.environ.get('ELEX_ADMIN_HOST', '127.0.0.1')) models.database_proxy.initialize(racedate_db) if request.method == 'POST': payload = utils.clean_payload(dict(request.form)) races = [ r.race_unique_id for r in models.ElexRace.select().where( models.ElexRace.statepostal == statepostal) ] for r in races: o = models.OverrideRace.get( models.OverrideRace.race_unique_id == r) o.report = payload['report'] o.save() utils.update_views(models.database_proxy) return json.dumps({"message": "success"})
def set_canonical_entity(): if request.method == "POST": payload = utils.clean_payload(dict(request.form)) if payload.get('entity', None) and payload.get('canonical_entity', None): if utils.valid_uuid(payload['entity']) and utils.valid_uuid( payload['canonical_entity']): # If payload['entity'] == payload['canonical_entity'], # then the user moved an entity back to where it was originally # placed on the page. This could signify the user rectifying # a mistake in placement. if payload['entity'] == payload['canonical_entity']: e = models.Entity\ .update(canonical_entity=None)\ .where(models.Entity.id==payload['entity']) e.execute() return (jsonify({ "entity": payload['entity'], "canonical_entity": None })) else: e = models.Entity\ .update(canonical_entity=payload['canonical_entity'])\ .where(models.Entity.id==payload['entity']) e.execute() return (jsonify({ "entity": payload['entity'], "canonical_entity": payload['canonical_entity'] })) return Response('bad request', 400) return Response('bad request', 400) return Response('bad request', 400)
state_dict['report_description'] = race.report_description context['states'].append(state_dict) return render_template('race_detail.html', **context) except peewee.OperationalError, e: context['error'] = e return render_template('error.html', **context) if request.method == 'POST': racedate_db = PostgresqlExtDatabase('elex_%s' % racedate, user=os.environ.get('ELEX_ADMIN_USER', 'elex'), host=os.environ.get('ELEX_ADMIN_HOST', '127.0.0.1') ) models.database_proxy.initialize(racedate_db) payload = utils.clean_payload(dict(request.form)) try: r = models.OverrideRace.get(models.OverrideRace.race_raceid == raceid.split('-')[1], models.OverrideRace.race_statepostal == raceid.split('-')[0]) except models.OverrideRace.DoesNotExist: r = models.OverrideRace.create(race_raceid=raceid.split('-')[1], race_statepostal=raceid.split('-')[0]) print payload utils.set_winner(payload['nyt_winner'], raceid) utils.update_model(r, payload) utils.update_views(models.database_proxy) return json.dumps({"message": "success"}) @app.route('/elections/2016/admin/<racedate>/candidateorder/', methods=['POST'])
def race_detail(racedate, raceid, raceyear): if request.method == 'GET': try: racedate_db = PostgresqlExtDatabase( 'elex_%s' % racedate, user=os.environ.get('ELEX_ADMIN_USER', 'elex'), host=os.environ.get('ELEX_ADMIN_HOST', '127.0.0.1')) models.database_proxy.initialize(racedate_db) context = utils.build_context(racedate, raceyear) context['race'] = [ r for r in models.ElexResult.raw( """select officename, seatname, race_unique_id, raceid, statepostal, accept_ap_calls from elex_results where race_unique_id = '%s' group by officename, seatname, race_unique_id, raceid, statepostal, accept_ap_calls""" % raceid) ][0] context['candidates'] = models.ElexResult.raw( """select nyt_runoff, party, nyt_winner, candidate_unique_id, first, last from elex_results where race_unique_id = '%s' group by nyt_runoff, party, nyt_winner, candidate_unique_id, first, last order by last, first DESC;""" % raceid) context['ap_winner'] = None ap_winner = [ m for m in models.ElexResult.raw( """select candidate_unique_id, first, last, winner, nyt_winner, nyt_called from elex_results where race_unique_id = '%s' and winner = 'true' group by candidate_unique_id, first, last, winner, nyt_winner, nyt_called order by last, first DESC;""" % raceid) ] if len(ap_winner) > 0: context['ap_winner'] = ap_winner[0] context['states'] = [] state_list = sorted(list( set([race.statepostal for race in models.ElexRace.select()])), key=lambda x: x) for state in state_list: race = models.ElexRace.select().where( models.ElexRace.statepostal == state)[0] state_dict = {} state_dict['statepostal'] = state state_dict['report'] = None state_dict['report_description'] = None context['states'].append(state_dict) return render_template('race_detail.html', **context) except peewee.OperationalError as e: context['error'] = e return render_template('error.html', **context) if request.method == 'POST': racedate_db = PostgresqlExtDatabase( 'elex_%s' % racedate, user=os.environ.get('ELEX_ADMIN_USER', 'elex'), host=os.environ.get('ELEX_ADMIN_HOST', '127.0.0.1')) models.database_proxy.initialize(racedate_db) payload = utils.clean_payload(dict(request.form)) try: r = models.OverrideRace.get( models.OverrideRace.race_unique_id == raceid) except models.OverrideRace.DoesNotExist: r = models.OverrideRace.create(race_unique_id=raceid, raceid=raceid.split('-')[1], statepostal=raceid.split('-')[0]) # nyt_winner is a single ID, there can only be one winner. utils.set_winner(payload['nyt_winner'], raceid) print(payload) # nyt_runoff is a list of ids, there can be 2 or more advancing. runoff_cands = [] if payload.get('nyt_runoff', None): runoff_cands = [ x.strip() for x in payload['nyt_runoff'].split(',') ] utils.set_runoff(runoff_cands, raceid) utils.update_model(r, payload) utils.update_views(models.database_proxy) return json.dumps({"message": "success"})