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

Finished feature COMP-150-NREN-ORGANISATION-TABLE.

parents 0dc0f8c4 e9eee39f
No related branches found
No related tags found
No related merge requests found
...@@ -19,12 +19,14 @@ from compendium_v2.routes.budget import routes as budget_routes ...@@ -19,12 +19,14 @@ 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 from compendium_v2.routes.staff import routes as staff_routes
from compendium_v2.routes.organization import routes as org_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') routes.register_blueprint(staff_routes, url_prefix='/staff')
routes.register_blueprint(org_routes, url_prefix='/organization')
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('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)
...@@ -227,3 +227,62 @@ def test_charging_structure_data(): ...@@ -227,3 +227,62 @@ def test_charging_structure_data():
nren=nren, year=year, nren=nren, year=year,
fee_type=fee_type) fee_type=fee_type)
) )
@pytest.fixture
def test_organization_data():
def _generate_sub_org_data():
for nren in ["nren" + str(i) for i in range(1, 50)]:
for year in range(2016, 2021):
yield {
'nren': nren,
'year': year,
'name': 'sub_org' + str(random.randint(1, 100)),
'role': random.choice(['technical centre', 'stuff', 'test123']),
}
def _generate_org_data():
for nren in ["nren" + str(i) for i in range(1, 50)]:
for year in range(2016, 2021):
yield {
'nren': nren,
'year': year,
'name': 'org' + str(year)
}
with db.session_scope() as session:
org_data = list(_generate_org_data())
sub_org_data = list(_generate_sub_org_data())
nren_dict = {nren_name: model.NREN(name=nren_name)
for nren_name in set(d['nren'] for d in [*org_data, *sub_org_data])}
session.add_all(nren_dict.values())
for org in org_data:
nren = nren_dict[org["nren"]]
year = org["year"]
name = org["name"]
session.add(
model.ParentOrganization(
nren=nren,
year=year,
organization=name
)
)
for sub_org in sub_org_data:
nren = nren_dict[sub_org["nren"]]
year = sub_org["year"]
name = sub_org["name"]
role = sub_org["role"]
session.add(
model.SubOrganization(
nren=nren,
year=year,
organization=name,
role=role
)
)
session.commit()
import json
import jsonschema
from compendium_v2.routes.organization import ORGANIZATION_RESPONSE_SCHEMA
def test_parentorganization_response(client, test_organization_data):
rv = client.get(
'/api/organization/parent',
headers={'Accept': ['application/json']})
assert rv.status_code == 200
result = json.loads(rv.data.decode('utf-8'))
jsonschema.validate(result, ORGANIZATION_RESPONSE_SCHEMA)
assert result
def test_suborganization_response(client, test_organization_data):
rv = client.get(
'/api/organization/sub',
headers={'Accept': ['application/json']})
assert rv.status_code == 200
result = json.loads(rv.data.decode('utf-8'))
jsonschema.validate(result, ORGANIZATION_RESPONSE_SCHEMA)
assert result
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment