Exemple #1
0
    def person_main():
        sort = request.args.get('sort', 'name')
        election_type = request.args.get('election_type', 'assembly')
        assembly_id = int(request.args.get('assembly_id', current_parliament_id(election_type)) or 0)

        if sort == 'cosponsorship':
            bill_t = Bill.__table__
            cosponsorship_count = db_session.query(
                        cosponsorship.c.person_id,
                        func.count(cosponsorship.c.id).label('cosponsorship_count')
                    )\
                .outerjoin(bill_t)\
                .filter(bill_t.c.assembly_id == assembly_id)\
                .group_by(cosponsorship.c.person_id)\
                .subquery('cosponsorship_count')

            officials = Person.query.order_by(
                                            desc(func.coalesce(func.sum(cosponsorship_count.c.cosponsorship_count), 0))
                                        )\
                                    .join(Candidacy)\
                                    .outerjoin(cosponsorship_count)\
                                    .filter(and_(Candidacy.type == election_type,
                                                 Candidacy.assembly_id == assembly_id,
                                                 Candidacy.is_elected == True))\
                                    .group_by(Person.id)
        else:
            officials = Person.query.order_by(Person.name)\
                                    .join(Candidacy)\
                                    .filter(and_(Candidacy.type == election_type,
                                                 Candidacy.assembly_id == assembly_id,
                                                 Candidacy.is_elected == True))

        return render_template('people.html',
                                officials=officials,
                                assembly_id=assembly_id)
    def legislator_of(cls, region_id, assembly_id=None):
        if not region_id:
            return None
        if not assembly_id:
            assembly_id = current_parliament_id('assembly')

        region = Region.query.filter_by(id=region_id).one()
        original_region = region

        legislator = None
        while not legislator and region:
            legislators = region.candidates\
                                .filter(Candidacy.assembly_id == assembly_id)\
                                .filter_by(is_elected=True)

            try:
                legislator = legislators.one()
            except MultipleResultsFound as e:
                legislator = guess_legislator(legislators, original_region,
                                              assembly_id)
                break
            except NoResultFound as e:
                region = region.parents.order_by(False)\
                                       .order_by(func.length(Region.id).desc())\
                                       .first()

        return legislator
Exemple #3
0
 def bill_main():
     assembly_id = int(request.args.get('assembly_id', current_parliament_id('assembly')) or 0)
     bills = Bill.query.filter(Bill.assembly_id==assembly_id)\
                       .order_by(Bill.proposed_date.desc().nullslast(),
                                 Bill.id.desc())
     return render_template('bills.html',\
             assembly_id=assembly_id, bills=bills)
Exemple #4
0
 def bill_main():
     assembly_id = int(
         request.args.get('assembly_id', current_parliament_id('assembly'))
         or 0)
     bills = Bill.query.filter(Bill.assembly_id==assembly_id)\
                       .order_by(Bill.proposed_date.desc().nullslast(),
                                 Bill.id.desc())
     return render_template('bills.html',\
             assembly_id=assembly_id, bills=bills)
Exemple #5
0
    def person_main():
        election_type = request.args.get('election_type', 'assembly')
        assembly_id = int(request.args.get('assembly_id', current_parliament_id(election_type)) or 0)
        officials = Person.query.order_by(Person.name)\
                                .join(Candidacy)\
                                .filter(and_(Candidacy.type == election_type,
                                             Candidacy.assembly_id == assembly_id,
                                             Candidacy.is_elected == True))

        return render_template('people.html',
                                officials=officials,
                                assembly_id=assembly_id)
Exemple #6
0
    def bills_list():
        def truncate(string, l=140):
            return (string[:l] +
                    '...') if string and len(string) > l else string

        def wrap(data):
            return [{
                'DT_RowId':
                d.id,
                'DT_RowClass':
                'clickable',
                'proposed_date':
                d.proposed_date.isoformat(),
                'name':
                '<b>%s</b>&nbsp;<small>(%s)</small><br><small>%s</small>' %
                (d.name, d.id, truncate(d.summary)),
                'sponsor':
                d.sponsor,
                'status':
                d.status
            } for d in data]

        assembly_id = int(
            request.args.get('assembly_id', current_parliament_id('assembly'))
            or 0)

        draw = int(request.args.get('draw', 1))  # iteration number
        start = int(request.args.get('start', 0))  # starting row's id
        length = int(request.args.get('length', 10))  # number of rows in page

        columns = ['proposed_date', 'name', 'sponsor', 'status']

        if request.args.get('order[0][column]'):
            order_column = columns[int(request.args.get('order[0][column]',
                                                        0))]
            if not request.args.get('order[0][dir]', 'asc') == 'asc':
                order_column += ' desc'
        else:
            order_column = 'proposed_date desc'  # default

        bills = Bill.query.filter(Bill.assembly_id==assembly_id)\
                          .order_by(order_column, Bill.id.desc())

        filtered = bills.offset(start).limit(length)

        response = {
            'draw': draw,
            'data': wrap(filtered),
            'recordsTotal': bills.count(),
            'recordsFiltered': bills.count()
        }
        return jsonify(**response)
Exemple #7
0
    def bill_main():

        assembly_id = int(request.args.get('assembly_id', current_parliament_id('assembly')) or 0)
        statuses = BillStatus.query.all()
        status_counts = filter(lambda x: x['value']!=0, [{
                'id': s.id,
                'name': s.name,
                'value': status_count(s, assembly_id),
                'url': url_for('search', target='bills',\
                       status_id=s.id, assembly_id=assembly_id)
            } for s in statuses])

        return render_template('bills.html',\
                assembly_id=assembly_id, status_counts=status_counts)
Exemple #8
0
    def party_main():
        election_type = request.args.get('election_type', 'assembly')
        assembly_id = int(request.args.get('assembly_id', current_parliament_id(election_type)) or 0)

        parties = Party.query\
                        .join(Candidacy)\
                        .filter(Candidacy.assembly_id == assembly_id)\
                        .filter(Candidacy.type == election_type)\
                        .group_by(Party.id)\
                        .order_by(func.count().desc())

        return render_template('parties.html',\
                                assembly_id=assembly_id,\
                                parties=parties)
    def people_list():
        def wrap(data):
            return [{
                'DT_RowId': d.id,
                'DT_RowClass': 'clickable',
                'image': '<img src="%s" width="30px" height="40px">' % d.image,
                'name': d.name,
                'gender': '여' if d.gender == 'f' else '남',
                'birthday': d.birthday_year,
                'cur_party': d.cur_party.name,
                'nelected': d.nelected
            } for d in data]

        election_type = request.args.get('election_type', 'assembly')
        assembly_id = int(\
                request.args.get('assembly_id', current_parliament_id(election_type)) or 0)

        draw = int(request.args.get('draw', 1))  # iteration number
        start = int(request.args.get('start', 0))  # starting row's id
        length = int(request.args.get('length', 10))  # number of rows in page

        # order by
        columns = [
            'image', 'name', 'gender', 'birthday', 'cur_party', 'nelected'
        ]
        if draw == 1:
            order_column = func.random()  # shuffle rows
        elif request.args.get('order[0][column]'):
            order_column = columns[int(request.args.get('order[0][column]'))]
            if request.args.get('order[0][dir]', 'asc') == 'desc':
                order_column += ' desc'
        else:
            order_column = None

        officials = Person.query.join(Candidacy)\
                                .filter(and_(Candidacy.type==election_type,
                                             Candidacy.assembly_id==assembly_id,
                                             Candidacy.is_elected==True))\
                                .order_by(order_column)

        filtered = officials.offset(start).limit(length)

        response = {
            'draw': draw,
            'data': wrap(filtered),
            'recordsTotal': officials.count(),
            'recordsFiltered': officials.count()
        }
        return jsonify(**response)
Exemple #10
0
    def people_list():

        def wrap(data):
            return [{
                'DT_RowId': d.id,
                'DT_RowClass': 'clickable',
                'image': '<img src="%s" width="30px" height="40px">' % d.image,
                'name': d.name,
                'gender': '여' if d.gender=='f' else '남',
                'birthday': d.birthday_year,
                'cur_party': d.cur_party.name,
                'nelected': d.nelected
                } for d in data]

        election_type = request.args.get('election_type', 'assembly')
        assembly_id = int(\
                request.args.get('assembly_id', current_parliament_id(election_type)) or 0)

        draw = int(request.args.get('draw', 1))  # iteration number
        start = int(request.args.get('start', 0))  # starting row's id
        length = int(request.args.get('length', 10))  # number of rows in page

        # order by
        columns = ['image', 'name', 'gender', 'birthday', 'cur_party', 'nelected']
        if draw==1:
            order_column = func.random()  # shuffle rows
        elif request.args.get('order[0][column]'):
            order_column = columns[int(request.args.get('order[0][column]'))]
            if request.args.get('order[0][dir]', 'asc')=='desc':
                order_column += ' desc'
        else:
            order_column = None

        officials = Person.query.join(Candidacy)\
                                .filter(and_(Candidacy.type==election_type,
                                             Candidacy.assembly_id==assembly_id,
                                             Candidacy.is_elected==True))\
                                .order_by(order_column)

        filtered = officials.offset(start).limit(length)

        response = {
            'draw': draw,
            'data': wrap(filtered),
            'recordsTotal': officials.count(),
            'recordsFiltered': officials.count()
        }
        return jsonify(**response)
Exemple #11
0
    def person_main():
        sort = request.args.get("sort", "name")
        election_type = request.args.get("election_type", "assembly")
        assembly_id = int(request.args.get("assembly_id", current_parliament_id(election_type)) or 0)

        if sort == "cosponsorship":
            bill_t = Bill.__table__
            cosponsorship_count = (
                db_session.query(cosponsorship.c.person_id, func.count(cosponsorship.c.id).label("cosponsorship_count"))
                .outerjoin(bill_t)
                .filter(bill_t.c.assembly_id == assembly_id)
                .group_by(cosponsorship.c.person_id)
                .subquery("cosponsorship_count")
            )

            officials = (
                Person.query.order_by(desc(func.coalesce(func.sum(cosponsorship_count.c.cosponsorship_count), 0)))
                .join(Candidacy)
                .outerjoin(cosponsorship_count)
                .filter(
                    and_(
                        Candidacy.type == election_type,
                        Candidacy.assembly_id == assembly_id,
                        Candidacy.is_elected == True,
                    )
                )
                .group_by(Person.id)
            )
        else:
            officials = (
                Person.query.order_by(Person.name)
                .join(Candidacy)
                .filter(
                    and_(
                        Candidacy.type == election_type,
                        Candidacy.assembly_id == assembly_id,
                        Candidacy.is_elected == True,
                    )
                )
            )

        # party list
        party_count = defaultdict(int)
        for official in officials:
            party_count[official.cur_party] += 1
        party_list = [p[0] for p in sorted(party_count.items(), key=lambda x: x[1], reverse=True)]

        return render_template("people.html", officials=officials, assembly_id=assembly_id, party_list=party_list)
Exemple #12
0
    def party_main():
        election_type = request.args.get('election_type', 'assembly')
        assembly_id = int(
            request.args.get('assembly_id',
                             current_parliament_id(election_type)) or 0)

        parties = Party.query\
                        .join(Candidacy)\
                        .filter(Candidacy.assembly_id == assembly_id)\
                        .filter(Candidacy.type == election_type)\
                        .group_by(Party.id)\
                        .order_by(func.count().desc())

        return render_template('parties.html',\
                                assembly_id=assembly_id,\
                                parties=parties)
Exemple #13
0
    def bill_main():

        assembly_id = int(
            request.args.get('assembly_id', current_parliament_id('assembly'))
            or 0)
        statuses = BillStatus.query.all()
        status_counts = filter(lambda x: x['value']!=0, [{
                'id': s.id,
                'name': s.name,
                'value': status_count(s, assembly_id),
                'url': url_for('search', target='bills',\
                       status_id=s.id, assembly_id=assembly_id)
            } for s in statuses])

        return render_template('bills.html',\
                assembly_id=assembly_id, status_counts=status_counts)
Exemple #14
0
    def bill_main():

        assembly_id = int(request.args.get('assembly_id', current_parliament_id('assembly')) or 0)
        bills = Bill.query.filter(Bill.assembly_id==assembly_id)\
                          .order_by(Bill.proposed_date.desc().nullslast(),
                                    Bill.id.desc())
        statuses = BillStatus.query.all()
        status_counts = filter(lambda x: x['value']!=0, [{
                'id': s.id,
                'name': s.name,
                'value': status_count(s, assembly_id),
                'url': url_for('search', target='bills',\
                       status_id=s.id, assembly_id=assembly_id)
            } for s in statuses])

        return render_template('bills.html',\
                assembly_id=assembly_id, bills=bills,
                status_counts=status_counts)
    def person_main():
        election_type = request.args.get('election_type', 'assembly')
        assembly_id = int(
            request.args.get('assembly_id',
                             current_parliament_id(election_type)) or 0)
        view_type = request.args.get('type', 'list')

        if view_type == 'card':
            officials = Person.query.order_by(Person.name)\
                                .join(Candidacy)\
                                .filter(and_(Candidacy.type==election_type,
                                             Candidacy.assembly_id==assembly_id,
                                             Candidacy.is_elected==True))
            return render_template('people-card.html',
                                   officials=officials,
                                   assembly_id=assembly_id)

        else:  # default
            return render_template('people-list.html', assembly_id=assembly_id)
Exemple #16
0
    def bills_list():

        def truncate(string, l=140):
            return (string[:l] + '...') if string and len(string) > l else string

        def wrap(data):
            return [{
                'DT_RowId': d.id,
                'DT_RowClass': 'clickable',
                'proposed_date': d.proposed_date.isoformat(),
                'name': '<b>%s</b>&nbsp;<small>(%s)</small><br><small>%s</small>' % (d.name, d.id, truncate(d.summary)),
                'sponsor': d.sponsor,
                'status': d.status
            } for d in data]

        assembly_id = int(request.args.get('assembly_id', current_parliament_id('assembly')) or 0)

        draw = int(request.args.get('draw', 1))  # iteration number
        start = int(request.args.get('start', 0))  # starting row's id
        length = int(request.args.get('length', 10))  # number of rows in page

        columns = ['proposed_date', 'name', 'sponsor', 'status']

        if request.args.get('order[0][column]'):
            order_column = columns[int(request.args.get('order[0][column]', 0))]
            if not request.args.get('order[0][dir]', 'asc')=='asc':
                order_column += ' desc'
        else:
            order_column = 'proposed_date desc'  # default

        bills = Bill.query.filter(Bill.assembly_id==assembly_id)\
                          .order_by(order_column, Bill.id.desc())

        filtered = bills.offset(start).limit(length)

        response = {
            'draw': draw,
            'data': wrap(filtered),
            'recordsTotal': bills.count(),
            'recordsFiltered': bills.count()
        }
        return jsonify(**response)
Exemple #17
0
    def person_main():
        sort = request.args.get('sort', 'name')
        election_type = request.args.get('election_type', 'assembly')
        assembly_id = int(request.args.get('assembly_id', current_parliament_id(election_type)) or 0)

        if sort == 'cosponsorship':
            bill_t = Bill.__table__
            cosponsorship_count = db_session.query(
                        cosponsorship.c.person_id,
                        func.count(cosponsorship.c.id).label('cosponsorship_count')
                    )\
                .outerjoin(bill_t)\
                .filter(bill_t.c.assembly_id == assembly_id)\
                .group_by(cosponsorship.c.person_id)\
                .subquery('cosponsorship_count')

            officials = Person.query.order_by(
                                            desc(func.coalesce(func.sum(cosponsorship_count.c.cosponsorship_count), 0))
                                        )\
                                    .join(Candidacy)\
                                    .outerjoin(cosponsorship_count)\
                                    .filter(and_(Candidacy.type == election_type,
                                                 Candidacy.assembly_id == assembly_id,
                                                 Candidacy.is_elected == True))\
                                    .group_by(Person.id)
        else:
            officials = Person.query.order_by(Person.name)\
                                    .join(Candidacy)\
                                    .filter(and_(Candidacy.type == election_type,
                                                 Candidacy.assembly_id == assembly_id,
                                                 Candidacy.is_elected == True))

        # party list
        party_count = defaultdict(int)
        for official in officials:
            party_count[official.cur_party] += 1
        party_list = [p[0] for p in sorted(party_count.items(), key=lambda x: x[1], reverse=True)]

        return render_template('people.html',
                                officials=officials,
                                assembly_id=assembly_id,
                                party_list=party_list)
Exemple #18
0
    def bill_main():

        assembly_id = int(
            request.args.get('assembly_id', current_parliament_id('assembly'))
            or 0)
        bills = Bill.query.filter(Bill.assembly_id==assembly_id)\
                          .order_by(Bill.proposed_date.desc().nullslast(),
                                    Bill.id.desc())
        statuses = BillStatus.query.all()
        status_counts = filter(lambda x: x['value']!=0, [{
                'id': s.id,
                'name': s.name,
                'value': status_count(s, assembly_id),
                'url': url_for('search', target='bills',\
                       status_id=s.id, assembly_id=assembly_id)
            } for s in statuses])

        return render_template('bills.html',\
                assembly_id=assembly_id, bills=bills,
                status_counts=status_counts)