politikorama/app/controller/api/representative.py

43 lines
1.3 KiB
Python
Raw Normal View History

2021-07-23 17:35:29 +02:00
# 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))
2022-05-31 07:11:49 +02:00
print(page)
2021-07-23 17:35:29 +02:00
query = RepresentativeModel.query
if request.args.get("name", "") != "":
2022-05-31 07:11:49 +02:00
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)
2021-07-23 17:35:29 +02:00
return [
representative.serialize()
for representative
2022-05-31 07:11:49 +02:00
in query.paginate(
page, current_app.config["API_PER_PAGE"], error_out=False
).items
2021-07-23 17:35:29 +02:00
]
class RepresentativeApi(Resource):
def get(self, representative_id):
representative = RepresentativeModel.query.get(representative_id)
if representative is None:
return None, 404
2022-05-31 07:11:49 +02:00
print(representative.serialize())
2021-07-23 17:35:29 +02:00
return representative.serialize()