Example #1
0
def proposal(proposal_id):
    proposal = (models.Proposal.query
                    .filter_by(id=proposal_id)
                    .first_or_404())

    activity = [
        {
            'date': parse_date(item['date']),
            'location': item['location'].lower(),
            'html': item['html'],
        }
        for item in flask.json.loads(proposal.activity or '[]')
    ]

    return flask.render_template('proposals/detail.html', **{
        'proposal': proposal,
        'policy_domain': proposal.policy_domain,
        'sponsorships': [{
                'id': sp.id,
                'mandate': sp.mandate,
                'match_data': flask.json.loads(sp.match.data or '{}'),
            } for sp in proposal.sponsorships],
        'activity': activity,
        'voting_sessions': proposal.voting_sessions.all(),
    })
Example #2
0
def transcript_contents(date_str):
    date_value = parse_date(date_str)
    return flask.render_template(
        'transcript_contents.html', **{
            'date': date_value,
            'chapters':
            models.TranscriptChapter.query.filter_by(date=date_value),
        })
Example #3
0
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))
Example #4
0
def transcript_chapter(date_str, chapter_serial):
    date_value = parse_date(date_str)
    chapter = (models.TranscriptChapter.query.filter_by(
        serial=chapter_serial).first_or_404())
    if chapter.date != date_value:
        flask.abort(404)
    return flask.render_template('transcript_chapter.html', **{
        'date': date_value,
        'chapter': chapter,
    })
Example #5
0
def transcript_chapter(date_str, chapter_serial):
    date_value = parse_date(date_str)
    chapter = (models.TranscriptChapter.query
                .filter_by(serial=chapter_serial)
                .first_or_404())
    if chapter.date != date_value:
        flask.abort(404)
    return flask.render_template('transcript_chapter.html', **{
        'date': date_value,
        'chapter': chapter,
    })
Example #6
0
def steno_chapter(date_str, chapter_serial_number):
    date_value = parse_date(date_str)
    chapter_serial = date_value.strftime('%Y-%m-%d/') + chapter_serial_number
    chapter = (models.StenoChapter.query
                .filter_by(serial=chapter_serial)
                .first_or_404())
    if chapter.date != date_value:
        flask.abort(404)
    return flask.render_template('steno_chapter.html', **{
        'date': date_value,
        'chapter': chapter,
    })
Example #7
0
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 votes(
        start=None,
        days=1,
        cache_name=None,
        throttle=None,
        no_commit=False,
        autoanalyze=False,
        ):
    from mptracker.scraper.votes import VoteScraper

    if start is None:
        start = models.db.session.execute(
            'select date from voting_session '
            'order by date desc limit 1').scalar() + ONE_DAY

    else:
        start = parse_date(start)

    days = int(days)

    http_session = create_session(cache_name=cache_name,
                                  throttle=throttle and float(throttle))
    vote_scraper = VoteScraper(http_session)


    voting_session_patcher = TablePatcher(
        models.VotingSession,
        models.db.session,
        key_columns=['cdeppk'],
    )

    vote_patcher = TablePatcher(
        models.Vote,
        models.db.session,
        key_columns=['voting_session_id', 'mandate_id'],
    )

    proposal_ids = {p.cdeppk_cdep: p.id for p in models.Proposal.query}
    mandate_lookup = models.MandateLookup()

    new_voting_session_list = []

    with voting_session_patcher.process() as add_voting_session:
        with vote_patcher.process() as add_vote:
            for delta in range(days):
                the_date = start + ONE_DAY * delta
                if the_date >= date.today():
                    # don't scrape today, maybe voting is not done yet!
                    break
                logger.info("Scraping votes from %s", the_date)
                for voting_session in vote_scraper.scrape_day(the_date):
                    record = model_to_dict(
                        voting_session,
                        ['cdeppk', 'subject', 'subject_html'],
                    )
                    record['date'] = the_date
                    proposal_cdeppk = voting_session.proposal_cdeppk
                    record['proposal_id'] = (proposal_ids.get(proposal_cdeppk)
                                             if proposal_cdeppk else None)
                    record['final'] = bool("vot final" in
                                           record['subject'].lower())
                    vs = add_voting_session(record).row
                    if vs.id is None:
                        models.db.session.flush()

                    new_voting_session_list.append(vs.id)

                    for vote in voting_session.votes:
                        record = model_to_dict(vote, ['choice'])
                        record['voting_session_id'] = vs.id
                        mandate = mandate_lookup.find(
                            vote.mandate_name,
                            vote.mandate_year,
                            vote.mandate_number,
                        )
                        record['mandate_id'] = mandate.id
                        add_vote(record)

    if no_commit:
        logger.warn("Rolling back the transaction")
        models.db.session.rollback()

    else:
        models.db.session.commit()

    if autoanalyze:
        from mptracker.votes import calculate_voting_session_loyalty
        logger.info("Scheduling %d jobs", len(new_voting_session_list))
        for voting_session_id in new_voting_session_list:
            calculate_voting_session_loyalty.delay(voting_session_id)
Example #9
0
def transcript_chapter(date_str, chapter_serial):
    date_value = parse_date(date_str)
    chapter = models.TranscriptChapter.query.filter_by(serial=chapter_serial).first_or_404()
    if chapter.date != date_value:
        flask.abort(404)
    return flask.render_template("transcript_chapter.html", **{"date": date_value, "chapter": chapter})
Example #10
0
def transcript_contents(date_str):
    date_value = parse_date(date_str)
    return flask.render_template(
        "transcript_contents.html",
        **{"date": date_value, "chapters": models.TranscriptChapter.query.filter_by(date=date_value)}
    )
Example #11
0
def transcript_contents(date_str):
    date_value = parse_date(date_str)
    return flask.render_template('transcript_contents.html', **{
        'date': date_value,
        'chapters': models.TranscriptChapter.query.filter_by(date=date_value),
    })
Example #12
0
def votes(
        start=None,
        n_days=1,
        cache_name=None,
        throttle=None,
        ):
    from mptracker.scraper.votes import VoteScraper

    if start is None:
        start = models.db.session.execute(
            'select date from voting_session '
            'order by date desc limit 1').scalar() + ONE_DAY

    else:
        start = parse_date(start)

    n_days = int(n_days)

    http_session = create_session(cache_name=cache_name,
                                  throttle=throttle and float(throttle))
    vote_scraper = VoteScraper(http_session)


    voting_session_patcher = TablePatcher(
        models.VotingSession,
        models.db.session,
        key_columns=['cdeppk'],
    )

    vote_patcher = TablePatcher(
        models.Vote,
        models.db.session,
        key_columns=['voting_session_id', 'mandate_id'],
    )

    proposal_ids = {p.cdeppk_cdep: p.id for p in models.Proposal.query}
    mandate_lookup = models.MandateLookup()

    with voting_session_patcher.process() as add_voting_session:
        with vote_patcher.process() as add_vote:
            for delta in range(n_days):
                the_date = start + ONE_DAY * delta
                if the_date >= date.today():
                    # don't scrape today, maybe voting is not done yet!
                    break
                logger.info("Scraping votes from %s", the_date)
                for voting_session in vote_scraper.scrape_day(the_date):
                    record = model_to_dict(
                        voting_session,
                        ['cdeppk', 'subject', 'subject_html'],
                    )
                    record['date'] = the_date
                    proposal_cdeppk = voting_session.proposal_cdeppk
                    record['proposal_id'] = (proposal_ids.get(proposal_cdeppk)
                                             if proposal_cdeppk else None)
                    vs = add_voting_session(record).row
                    if vs.id is None:
                        models.db.session.flush()

                    for vote in voting_session.votes:
                        record = model_to_dict(vote, ['choice'])
                        record['voting_session_id'] = vs.id
                        mandate = mandate_lookup.find(
                            vote.mandate_name,
                            vote.mandate_year,
                            vote.mandate_number,
                        )
                        record['mandate_id'] = mandate.id
                        add_vote(record)

    models.db.session.commit()
Example #13
0
def steno_contents(date_str):
    date_value = parse_date(date_str)
    return flask.render_template('steno_contents.html', **{
        'date': date_value,
        'chapters': models.StenoChapter.query.filter_by(date=date_value),
    })
Example #14
0
def read_activity_item(item):
    return {
        'date': parse_date(item['date']),
        'location': item['location'].lower(),
        'html': item['html'],
    }