From a5daa45c18894c9b471063ad4ca4db224c22fabf Mon Sep 17 00:00:00 2001
From: Bjarke Madsen <bjarke@nordu.net>
Date: Tue, 25 Apr 2023 14:57:09 +0200
Subject: [PATCH] create /api/ec-projects endpoint
---
compendium_v2/routes/api.py | 2 +
compendium_v2/routes/ec_projects.py | 67 +++++++++++++++++++++++++++++
2 files changed, 69 insertions(+)
create mode 100644 compendium_v2/routes/ec_projects.py
diff --git a/compendium_v2/routes/api.py b/compendium_v2/routes/api.py
index 551c08c2..e4fd4104 100644
--- a/compendium_v2/routes/api.py
+++ b/compendium_v2/routes/api.py
@@ -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.staff import routes as staff_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.register_blueprint(budget_routes, url_prefix='/budget')
@@ -27,6 +28,7 @@ routes.register_blueprint(funding_routes, url_prefix='/funding')
routes.register_blueprint(charging_routes, url_prefix='/charging')
routes.register_blueprint(staff_routes, url_prefix='/staff')
routes.register_blueprint(org_routes, url_prefix='/organization')
+routes.register_blueprint(ec_routes, url_prefix='/ec-project')
logger = logging.getLogger(__name__)
diff --git a/compendium_v2/routes/ec_projects.py b/compendium_v2/routes/ec_projects.py
new file mode 100644
index 00000000..7365d55b
--- /dev/null
+++ b/compendium_v2/routes/ec_projects.py
@@ -0,0 +1,67 @@
+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)
--
GitLab