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

add /api/staff endpoint for NREN staff data

parent 9b003ce9
No related branches found
No related tags found
1 merge request!6Feature/comp 125 staffing graph
...@@ -4,7 +4,7 @@ API Endpoints ...@@ -4,7 +4,7 @@ API Endpoints
.. contents:: :local: .. contents:: :local:
/api/data-entries/ /api/
--------------------- ---------------------
...@@ -18,11 +18,13 @@ from compendium_v2.routes import common ...@@ -18,11 +18,13 @@ from compendium_v2.routes import common
from compendium_v2.routes.budget import routes as budget_routes from compendium_v2.routes.budget import routes as budget_routes
from compendium_v2.routes.funding import routes as funding_routes 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
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')
routes.register_blueprint(funding_routes, url_prefix='/funding') 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')
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('staff', __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__)
STAFF_RESPONSE_SCHEMA = {
'$schema': 'http://json-schema.org/draft-07/schema#',
'definitions': {
'staff': {
'type': 'object',
'properties': {
'nren': {'type': 'string'},
'year': {'type': 'integer'},
'permanent_fte': {'type': 'number'},
'subcontracted_fte': {'type': 'number'},
'technical_fte': {'type': 'number'},
'non_technical_fte': {'type': 'number'}
},
'required': ['nren', 'year', 'permanent_fte', 'subcontracted_fte', 'technical_fte', 'non_technical_fte'],
'additionalProperties': False
}
},
'type': 'array',
'items': {'$ref': '#/definitions/staff'}
}
@routes.route('/', methods=['GET'])
@common.require_accepts_json
def staff_view() -> Any:
"""
handler for /api/staff/ requests
response will be formatted as:
.. asjson::
compendium_v2.routes.staff.STAFF_RESPONSE_SCHEMA
:return:
"""
def _extract_data(entry: model.NrenStaff):
return {
'nren': entry.nren.name,
'year': entry.year,
'permanent_fte': entry.permanent_fte,
'subcontracted_fte': entry.subcontracted_fte,
'technical_fte': entry.technical_fte,
'non_technical_fte': entry.non_technical_fte
}
with db.session_scope() as session:
entries = [_extract_data(entry) for entry in session.query(
model.NrenStaff).join(model.NREN).order_by(model.NREN.name.asc(), model.NrenStaff.year.desc())]
return jsonify(entries)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment