From 34163d9a2df5447ce9fe62f1f8e1da62b70b04f5 Mon Sep 17 00:00:00 2001
From: Bjarke Madsen <bjarke@nordu.net>
Date: Wed, 19 Apr 2023 15:25:52 +0200
Subject: [PATCH] add /api/staff endpoint for NREN staff data

---
 compendium_v2/routes/api.py   |  4 +-
 compendium_v2/routes/staff.py | 73 +++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+), 1 deletion(-)
 create mode 100644 compendium_v2/routes/staff.py

diff --git a/compendium_v2/routes/api.py b/compendium_v2/routes/api.py
index 31acbc8d..3bfa5bbe 100644
--- a/compendium_v2/routes/api.py
+++ b/compendium_v2/routes/api.py
@@ -4,7 +4,7 @@ API Endpoints
 
 .. contents:: :local:
 
-/api/data-entries/
+/api/
 ---------------------
 
 
@@ -18,11 +18,13 @@ from compendium_v2.routes import common
 from compendium_v2.routes.budget import routes as budget_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.staff import routes as staff_routes
 
 routes = Blueprint('compendium-v2-api', __name__)
 routes.register_blueprint(budget_routes, url_prefix='/budget')
 routes.register_blueprint(funding_routes, url_prefix='/funding')
 routes.register_blueprint(charging_routes, url_prefix='/charging')
+routes.register_blueprint(staff_routes, url_prefix='/staff')
 
 logger = logging.getLogger(__name__)
 
diff --git a/compendium_v2/routes/staff.py b/compendium_v2/routes/staff.py
new file mode 100644
index 00000000..0a57d57a
--- /dev/null
+++ b/compendium_v2/routes/staff.py
@@ -0,0 +1,73 @@
+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)
-- 
GitLab