Skip to content
Snippets Groups Projects
Commit a5daa45c authored by Bjarke Madsen's avatar Bjarke Madsen
Browse files

create /api/ec-projects endpoint

parent 62547b73
Branches
Tags
1 merge request!11merge feature/COMP-152-EC-PROJECTS-TABLE into develop
...@@ -20,6 +20,7 @@ from compendium_v2.routes.funding import routes as funding_routes ...@@ -20,6 +20,7 @@ from compendium_v2.routes.funding import routes as funding_routes
from compendium_v2.routes.charging import routes as charging_routes from compendium_v2.routes.charging import routes as charging_routes
from compendium_v2.routes.staff import routes as staff_routes from compendium_v2.routes.staff import routes as staff_routes
from compendium_v2.routes.organization import routes as org_routes from compendium_v2.routes.organization import routes as org_routes
from compendium_v2.routes.ec_projects import routes as ec_routes
routes = Blueprint('compendium-v2-api', __name__) routes = Blueprint('compendium-v2-api', __name__)
routes.register_blueprint(budget_routes, url_prefix='/budget') routes.register_blueprint(budget_routes, url_prefix='/budget')
...@@ -27,6 +28,7 @@ routes.register_blueprint(funding_routes, url_prefix='/funding') ...@@ -27,6 +28,7 @@ routes.register_blueprint(funding_routes, url_prefix='/funding')
routes.register_blueprint(charging_routes, url_prefix='/charging') routes.register_blueprint(charging_routes, url_prefix='/charging')
routes.register_blueprint(staff_routes, url_prefix='/staff') routes.register_blueprint(staff_routes, url_prefix='/staff')
routes.register_blueprint(org_routes, url_prefix='/organization') routes.register_blueprint(org_routes, url_prefix='/organization')
routes.register_blueprint(ec_routes, url_prefix='/ec-project')
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
......
import logging
from flask import Blueprint, jsonify, current_app
from compendium_v2 import db
from compendium_v2.routes import common
from compendium_v2.db import model
from typing import Any
routes = Blueprint('ec-projects', __name__)
@routes.before_request
def before_request():
config = current_app.config['CONFIG_PARAMS']
dsn_prn = config['SQLALCHEMY_DATABASE_URI']
db.init_db_model(dsn_prn)
logger = logging.getLogger(__name__)
EC_PROJECTS_RESPONSE_SCHEMA = {
'$schema': 'http://json-schema.org/draft-07/schema#',
'definitions': {
'ec_project': {
'type': 'object',
'properties': {
'nren': {'type': 'string'},
'year': {'type': 'integer'},
'project': {'type': 'string'}
},
'required': ['nren', 'year', 'project'],
'additionalProperties': False
}
},
'type': 'array',
'items': {'$ref': '#/definitions/ec_project'}
}
@routes.route('/', methods=['GET'])
@common.require_accepts_json
def ec_projects_view() -> Any:
"""
handler for /api/ec-projects requests
returns EC projects for each NREN/year combination
response will be formatted as:
.. asjson::
compendium_v2.routes.ec_projects.EC_PROJECTS_RESPONSE_SCHEMA
:return:
"""
def _extract_project(entry: model.ECProject):
return {
'nren': entry.nren.name,
'year': entry.year,
'project': entry.project
}
with db.session_scope() as session:
result = [_extract_project(project) for project in session.query(model.ECProject)]
return jsonify(result)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment