def export_activity(year): if not perm.admin.can(): flask.abort(403) cols = [ 'nume', 'propuneri-total', 'propuneri-inlucru', 'propuneri-acceptate', 'propuneri-respinse', 'luari-de-cuvant', 'intrebari', 'intrebari-locale', ] rows = ( { 'nume': row['name'], 'propuneri-total': row['proposals_total'], 'propuneri-inlucru': row['proposals_inprogress'], 'propuneri-acceptate': row['proposals_approved'], 'propuneri-respinse': row['proposals_rejected'], 'luari-de-cuvant': row['transcripts'], 'intrebari': row['questions'], 'intrebari-locale': row['questions-local'], } for row in dal.get_mandate_activity(year=year) ) data = buffer_on_disk(csv_lines(cols, rows)) return csv_response(data)
def question_dump(): cols = ['name', 'legislature', 'date', 'title', 'score'] ask_query = ( models.Ask.query .join(models.Ask.question) .options( joinedload('question'), joinedload('mandate'), joinedload('mandate.person'), joinedload('match_row'), ) .order_by(models.Question.date.desc()) ) def make_row(ask): score = ask.match.score return { 'name': ask.mandate.person.name, 'legislature': str(ask.mandate.year), 'date': str(ask.question.date), 'title': str(ask.question.title), 'score': '' if score is None else str(score), } rows = (make_row(ask) for ask in ask_query.yield_per(10)) data = buffer_on_disk(csv_lines(cols, rows)) return flask.Response(data, mimetype='text/csv')
def export_policy(): policy_slugs = [p['slug'] for p in dal.get_policy_list()] cols = ['nume'] for policy_slug in policy_slugs: cols.extend([ policy_slug + '-procent', policy_slug + '-propuneri', policy_slug + '-intrebari', ]) ZERO = {'proposal_count': 0, 'question_count': 0, 'interest': 0} def policy_row(mandate_info): rv = {'nume': mandate_info['name']} person = dal.get_person(mandate_info['person_slug']) policy_map = {p['slug']: p for p in person.get_top_policies()} for policy_slug in policy_slugs: p = policy_map.get(policy_slug, ZERO) rv.update({ policy_slug + '-procent': p['interest'], policy_slug + '-propuneri': p['proposal_count'], policy_slug + '-intrebari': p['question_count'], }) return rv def mandates(): mandates_by_county = dal.get_2016_mandates_by_county() for county_list in mandates_by_county.values(): for mandate_info in county_list: yield mandate_info rows = (policy_row(m) for m in mandates()) data = buffer_on_disk(csv_lines(cols, rows)) return csv_response(data)
def export_proposals(year): if not perm.admin.can(): flask.abort(403) cols = [ 'nr-cdep', 'nr-senat', 'nr-bpi', 'data-propunere', 'data-actualizare', 'status', 'titlu', 'url-cdep', 'url-senat', 'initiatori-cdep', 'initiatori-senat', ] rows = ( { 'nr-cdep': row['number_cdep'], 'nr-senat': row['number_senate'], 'nr-bpi': row['number_bpi'], 'data-propunere': row['date'].isoformat(), 'data-actualizare': row['modification_date'].isoformat(), 'status': PROPOSAL_STATUS_LABEL[row['status']], 'titlu': row['title'], 'url-cdep': row['url_cdep'], 'url-senat': row['url_senate'], 'initiatori-cdep': row['namelist_cdep'], 'initiatori-senat': row['namelist_senate'], } for row in dal.get_all_proposals(year=year) ) data = buffer_on_disk(csv_lines(cols, rows)) return csv_response(data)
def export_bounded_mandates(rq): out_list = [ { 'nume': row['name'], 'partid': row['group'], 'inceput': row['start'].isoformat(), 'sfarsit': row['end'].isoformat(), } for row in dal.get_bounded_mandates(rq) ] cols = ['nume', 'partid', 'inceput', 'sfarsit'] return csv_response(csv_lines(cols, out_list))
def export_votes(year): cols = ['data', 'cod cdep', 'nume', 'vot', 'vot grup'] rows = ( { 'data': row['date'].isoformat(), 'cod cdep': row['cdeppk'], 'nume': row['name'], 'vot': VOTE_LABEL.get(row['choice'], ''), 'vot grup': VOTE_LABEL.get(row['group_choice'], ''), } for row in dal.get_all_votes(year=year) ) data = buffer_on_disk(csv_lines(cols, rows)) return csv_response(data)
def dump(): voting_sessions = ( models.VotingSession.query .order_by( models.VotingSession.date, models.VotingSession.cdeppk, )) def iter_rows(): for voting_session in voting_sessions: cols_voting_session = { 'data': str(voting_session.date), 'titlu': voting_session.subject, 'cod cdep': voting_session.cdeppk, 'link cdep': voting_session.cdep_url, } print(voting_session.cdeppk, file= sys.stderr) votes = ( models.db.session.query( models.Vote, models.Person, models.MpGroup, ) .filter(models.Vote.voting_session == voting_session) .join(models.Vote.mandate) .join(models.Mandate.person) .join(models.Mandate.group_memberships) .filter( models.MpGroupMembership.interval .contains(voting_session.date) ) .join(models.MpGroupMembership.mp_group) .order_by(models.Person.name) ) for (vote, person, mp_group) in votes: yield dict(cols_voting_session, **{ 'nume': str(person), 'grup': mp_group.short_name, 'vot': VOTE_LABEL.get(vote.choice, "??"), }) csv = csv_lines( ['data', 'titlu', 'cod cdep', 'link cdep', 'nume', 'grup', 'vot'], iter_rows(), ) for line in csv: sys.stdout.write(line)
def export_questions(year): cols = ['data', 'numar', 'tip', 'titlu', 'nume', 'destinatar', 'scor'] rows = ( { 'data': row['date'].isoformat(), 'numar': row['number'], 'tip': QUESTION_TYPE_LABEL[row['type']], 'titlu': row['title'], 'nume': row['name'], 'destinatar': row['addressee'], 'scor': int(row['local_score']), } for row in dal.get_all_questions(year=year) ) data = buffer_on_disk(csv_lines(cols, rows)) return csv_response(data)
def export_migrations(): results = dal.get_group_migrations( start=parse_date(flask.request.args['start']), end=parse_date(flask.request.args.get('end', '9999-12-31')), ) membership_list = [ { 'nume': row['name'], 'data': row['date'].isoformat(), 'partid_vechi': row['group_old'], 'partid_nou': row['group_new'], } for row in results ] cols = ['nume', 'data', 'partid_vechi', 'partid_nou'] return csv_response(csv_lines(cols, membership_list))
def export_group_membership(): results = dal.get_group_membership( day=parse_date(flask.request.args.get('date')) or date.today() ) membership_list = [ { 'nume': row['name'], 'inceput': row['start'].isoformat(), 'sfarsit': '' if row['end'] is None else row['end'].isoformat(), 'partid': row['group'], } for row in results ] return csv_response( csv_lines(['nume', 'inceput', 'sfarsit', 'partid'], membership_list), )
def mandate_questions(mandate_id): mandate = (models.Mandate.query.filter_by(id=mandate_id).join( models.Mandate.person).first_or_404()) addressee_count = defaultdict(int) questions = [] for ask in mandate.asked.join(models.Question): q = ask.question questions.append({ 'id': q.id, 'title': q.title, 'date': q.date, 'question_type': q.type, 'addressee': q.addressee, 'is_local_topic_flag': ask.get_meta('is_local_topic'), 'score': ask.match.score or 0, }) for name in q.addressee.split(';'): addressee_count[name.strip()] += 1 if flask.request.args.get('format') == 'csv': cols = [ 'id', 'title', 'date', 'question_type', 'is_local_topic_flag', 'score', 'addressee' ] return flask.Response(csv_lines(cols, questions), mimetype='text/csv') addressee_top = sorted(((n, name) for name, n in addressee_count.items()), reverse=True)[:5] def sort_key(question): if question['is_local_topic_flag']: return 11.0 else: return question['score'] or 0 questions.sort(key=sort_key, reverse=True) return flask.render_template( 'questions/mandate.html', **{ 'mandate': mandate, 'questions': questions, 'addressee_top': addressee_top, })
def mandate_questions(mandate_id): mandate = (models.Mandate.query .filter_by(id=mandate_id) .join(models.Mandate.person) .first_or_404()) addressee_count = defaultdict(int) questions = [] for ask in mandate.asked.join(models.Question): q = ask.question questions.append({ 'id': q.id, 'title': q.title, 'date': q.date, 'question_type': q.type, 'addressee': q.addressee, 'is_local_topic_flag': ask.get_meta('is_local_topic'), 'score': ask.match.score or 0, }) for name in q.addressee.split(';'): addressee_count[name.strip()] += 1 if flask.request.args.get('format') == 'csv': cols = ['id', 'title', 'date', 'question_type', 'is_local_topic_flag', 'score', 'addressee'] return flask.Response(csv_lines(cols, questions), mimetype='text/csv') addressee_top = sorted(((n, name) for name, n in addressee_count.items()), reverse=True)[:5] def sort_key(question): if question['is_local_topic_flag']: return 11.0 else: return question['score'] or 0 questions.sort(key=sort_key, reverse=True) return flask.render_template('questions/mandate.html', **{ 'mandate': mandate, 'questions': questions, 'addressee_top': addressee_top, })
def question_dump(): cols = ['name', 'legislature', 'date', 'title', 'score'] ask_query = ( models.Ask.query .options( joinedload('question'), joinedload('mandate'), joinedload('mandate.person'), joinedload('match_row'), ) ) def make_row(ask): score = ask.match.score return { 'name': ask.mandate.person.name, 'legislature': str(ask.mandate.year), 'date': str(ask.question.date), 'title': str(ask.question.title), 'score': '' if score is None else str(score), } rows = (make_row(ask) for ask in ask_query.yield_per(10)) data = buffer_on_disk(csv_lines(cols, rows)) return flask.Response(data, mimetype='text/csv')