politikorama/app/controller/api/representative.py

43 lines
1.3 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))
print(page)
query = RepresentativeModel.query
if request.args.get("name", "") != "":
query = query.filter(or_(
RepresentativeModel.last_name.like(
f"%{request.args.get('name')}%"
),
RepresentativeModel.first_name.like(
f"%{request.args.get('name')}%"
),
))
query = query.order_by(RepresentativeModel.last_name)
print(request.args.get("name"))
print(query)
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
print(representative.serialize())
return representative.serialize()