*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
views.py 2.51 KiB
Newer Older
# Copyright (C) 2020 - 2021 TU Wien.
#
# Invenio-Theme-TUW is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.

"""TU Wien theme for Invenio (RDM)."""

Wörister, Florian's avatar
Wörister, Florian committed
from flask import Blueprint, render_template
from invenio_cache import current_cache
def fetch_schemaorg_jsonld(doi_url):
    """Fetch the Schema.org metadata for the DOI."""
    key = f"schemaorg_{doi_url}"
    metadata = current_cache.get(key)
        try:
            response = requests.get(
                doi_url,
                headers={"Accept": "application/vnd.schemaorg.ld+json"},
            )
            if response.status_code == 200:
                metadata = response.text.strip()
                current_cache.set(key, metadata)
    return metadata


def create_blueprint(app):
    """Create a blueprint with routes and resources."""

    blueprint = Blueprint(
        "invenio_theme_tuw",
        __name__,
        template_folder="templates",
        static_folder="static",
    )

    @blueprint.app_template_filter("tuw_doi_identifier")
    def tuw_doi_identifier(identifiers):
        """Extract DOI from sequence of identifiers."""
        if identifiers is not None:
            for identifier in identifiers:
                if identifier.get("scheme") == "doi":
                    return identifier.get("identifier")

    @blueprint.app_template_global("tuw_create_schemaorg_metadata")
    def tuw_create_schemaorg_metadata(record):
        """Create schema.org metadata to include in a <script> tag."""
        metadata = None

        # get the DOI from the managed PIDs, or from the metadata as fallback
        rec_pids = record.get("pids", {})
        if "doi" in rec_pids:
            doi = rec_pids["doi"].get("identifier")
        else:
            rec_meta = record.get("metadata", {})
            doi = tuw_doi_identifier(rec_meta.get("identifiers"))
        if doi is not None:
            doi_url = (
                doi if doi.startswith("https://") else ("https://doi.org/%s" % doi)
            )
            metadata = fetch_schemaorg_jsonld(doi_url)

        return metadata
Moser, Maximilian's avatar
Moser, Maximilian committed

    @blueprint.route("/tuw/policies")
Wörister, Florian's avatar
Wörister, Florian committed
    def tuw_policies():
Moser, Maximilian's avatar
Moser, Maximilian committed
        return render_template("invenio_theme_tuw/policies.html")
Moser, Maximilian's avatar
Moser, Maximilian committed
    @blueprint.route("/tuw/contact")
Wörister, Florian's avatar
Wörister, Florian committed
    def tuw_contact():
Moser, Maximilian's avatar
Moser, Maximilian committed
        return render_template("invenio_theme_tuw/contact.html")