-
Bjarke Madsen authoredBjarke Madsen authored
organization.py 2.90 KiB
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('organization', __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__)
ORGANIZATION_RESPONSE_SCHEMA = {
'$schema': 'http://json-schema.org/draft-07/schema#',
'definitions': {
'parent_organization': {
'type': 'object',
'properties': {
'nren': {'type': 'string'},
'year': {'type': 'integer'},
'name': {'type': 'string'}
},
'required': ['nren', 'year', 'name'],
'additionalProperties': False
},
'sub_organization': {
'type': 'object',
'properties': {
'nren': {'type': 'string'},
'year': {'type': 'integer'},
'name': {'type': 'string'},
'role': {'type': 'string'},
},
'required': ['nren', 'year', 'name', 'role'],
'additionalProperties': False
}
},
'type': 'array',
'items': {
'oneOf': [
{'$ref': '#/definitions/parent_organization'},
{'$ref': '#/definitions/sub_organization'}
]
}
}
@routes.route('/parent', methods=['GET'])
@common.require_accepts_json
def parent_organization_view() -> Any:
"""
handler for /api/organization/parent requests
returns parent organizations for each NREN/year combination
response will be formatted as:
.. asjson::
compendium_v2.routes.organization.ORGANIZATION_RESPONSE_SCHEMA
:return:
"""
def _extract_parent(entry: model.ParentOrganization):
return {
'nren': entry.nren.name,
'year': entry.year,
'name': entry.organization
}
with db.session_scope() as session:
result = [_extract_parent(org) for org in session.query(model.ParentOrganization)]
return jsonify(result)
@routes.route('/sub', methods=['GET'])
@common.require_accepts_json
def sub_organization_view() -> Any:
"""
handler for /api/organization/sub requests
returns sub-organizations for each NREN/year combination
response will be formatted as:
.. asjson::
compendium_v2.routes.organization.ORGANIZATION_RESPONSE_SCHEMA
:return:
"""
def _extract_sub(entry: model.SubOrganization):
return {
'nren': entry.nren.name,
'year': entry.year,
'name': entry.organization,
'role': entry.role
}
with db.session_scope() as session:
result = [_extract_sub(org) for org in session.query(model.SubOrganization)]
return jsonify(result)