politikorama/app/controller/api/representative.py

36 lines
1.1 KiB
Python

# encoding: utf-8
from flask import request, current_app
from flask_restful import Resource
from sqlalchemy import or_
from app.model.representative import RepresentativeModel
class RepresentativesApi(Resource):
def get(self):
page = int(request.args.get("page", 1))
query = RepresentativeModel.query
if request.args.get("name", "") != "":
query = query.filter(
RepresentativeModel
.name
.like('%%%s%%' % request.args.get("name", ""))
)
query = query.order_by(RepresentativeModel.name)
return [
representative.serialize()
for representative
in query
.paginate(page, current_app.config['API_PER_PAGE'], error_out=False)
.items
]
class RepresentativeApi(Resource):
def get(self, representative_id):
representative = RepresentativeModel.query.get(representative_id)
if representative is None:
return None, 404
return representative.serialize()