46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
# encoding: utf-8
|
|
|
|
from flask import request, current_app
|
|
from flask_restful import Resource
|
|
from sqlalchemy import or_
|
|
|
|
from app.model.country import CountryModel
|
|
|
|
|
|
class CountriesApi(Resource):
|
|
def get(self):
|
|
page = int(request.args.get("page", 1))
|
|
query = CountryModel.query
|
|
if request.args.get("name", "") != "":
|
|
query = query.filter(
|
|
CountryModel
|
|
.name
|
|
.like('%%%s%%' % request.args.get("name", ""))
|
|
)
|
|
if request.args.get("iso2", "") != "":
|
|
query = query.filter_by(iso2=request.args.get("iso2", "").upper())
|
|
if request.args.get("iso3", "") != "":
|
|
query = query.filter_by(iso3=request.args.get("iso3", "").upper())
|
|
if request.args.get("m49", "") != "":
|
|
query = query.filter_by(m49=request.args.get("m49", "").upper())
|
|
query = query.order_by(CountryModel.name)
|
|
return [
|
|
country.serialize()
|
|
for country
|
|
in query
|
|
.paginate(page, current_app.config['API_PER_PAGE'], error_out=False)
|
|
.items
|
|
]
|
|
|
|
|
|
class CountryApi(Resource):
|
|
def get(self, country_id):
|
|
country = CountryModel.query.filter(or_(
|
|
CountryModel.iso2==country_id,
|
|
CountryModel.iso3==country_id,
|
|
CountryModel.m49==country_id,
|
|
)).first()
|
|
if country is None:
|
|
return None, 404
|
|
return country.serialize()
|