-
Mohammad Torkashvand authoredMohammad Torkashvand authored
institutions_urls.py 1.91 KiB
from typing import Any
from flask import Blueprint, jsonify
from sqlalchemy import select
from compendium_v2.db import db
from compendium_v2.db.model import NREN, InstitutionURLs
from compendium_v2.routes import common
routes = Blueprint('institutions-urls', __name__)
INSTITUTION_URLS_RESPONSE_SCHEMA = {
'$schema': 'http://json-schema.org/draft-07/schema#',
'definitions': {
'institution_urls': {
'type': 'object',
'properties': {
'nren': {'type': 'string'},
'nren_country': {'type': 'string'},
'year': {'type': 'integer'},
'urls': {'type': 'array', 'items': {'type': 'string'}}
},
'required': ['nren', 'nren_country', 'year', 'urls'],
'additionalProperties': False
}
},
'type': 'array',
'items': {'$ref': '#/definitions/institution_urls'}
}
@routes.route('/', methods=['GET'])
@common.require_accepts_json
def institutions_urls_view() -> Any:
"""
handler for /api/institutions-urls/ requests
Endpoint for getting the URLs of institutions connected to the NREN.
This endpoint retrieves the URLs of webpages that list the institutions
or organizations connected to the NREN.
Many NRENs have one or more pages on their website listing such user institutions.
response will be formatted as per INSTITUTION_URLS_RESPONSE_SCHEMA
:return:
"""
def _extract_data(institution: InstitutionURLs) -> dict:
return {
'nren': institution.nren.name,
'nren_country': institution.nren.country,
'year': institution.year,
'urls': institution.urls
}
entries = []
records = db.session.scalars(
select(InstitutionURLs).join(NREN).order_by(NREN.name.asc(), InstitutionURLs.year.desc()))
for entry in records:
entries.append(_extract_data(entry))
return jsonify(entries)