async def landing(request: Request):
    """Show a human-readable landing page when the base URL is accessed."""

    meta = meta_values(request.url, 1, 1, more_data_available=False)
    major_version = __api_version__.split(".")[0]
    versioned_url = f"{get_base_url(request.url)}/v{major_version}/"

    context = {
        "request": request,
        "request_url": request.url,
        "api_version": __api_version__,
        "implementation": meta.implementation,
        "versioned_url": versioned_url,
        "provider": meta.provider,
        "index_base_url": CONFIG.index_base_url,
        "endpoints": list(ENTRY_COLLECTIONS.keys()) + ["info"],
    }

    return TEMPLATES.TemplateResponse("landing_page.html", context)
def test_get_attribute_fields():
    """Test get_attribute_fields() method"""
    from optimade.models import (
        LinksResourceAttributes,
        ReferenceResourceAttributes,
        StructureResourceAttributes,
    )
    from optimade.server.routers import ENTRY_COLLECTIONS

    entry_name_attributes = {
        "links": LinksResourceAttributes,
        "references": ReferenceResourceAttributes,
        "structures": StructureResourceAttributes,
    }

    # Make sure we're hitting all collections
    assert set(entry_name_attributes.keys()) == set(ENTRY_COLLECTIONS.keys())

    for entry_name, attributes_model in entry_name_attributes.items():
        assert (
            set(attributes_model.__fields__.keys())
            == ENTRY_COLLECTIONS[entry_name].get_attribute_fields()
        )
        if (CONFIG.database_backend.value in ("mongomock", "mongodb")
                and endpoint_name == "links"):
            LOGGER.debug(
                "Adding Materials-Consortia providers to links from optimade.org"
            )
            providers = get_providers(add_mongo_id=True)
            for doc in providers:
                endpoint_collection.collection.replace_one(
                    filter={"_id": ObjectId(doc["_id"]["$oid"])},
                    replacement=bson.json_util.loads(
                        bson.json_util.dumps(doc)),
                    upsert=True,
                )
        LOGGER.debug("Done inserting test %s!", endpoint_name)

    for name, collection in ENTRY_COLLECTIONS.items():
        load_entries(name, collection)

# Add CORS middleware first
app.add_middleware(CORSMiddleware, allow_origins=["*"])

# Then add required OPTIMADE middleware
for middleware in OPTIMADE_MIDDLEWARE:
    app.add_middleware(middleware)

# Add exception handlers
for exception, handler in OPTIMADE_EXCEPTIONS:
    app.add_exception_handler(exception, handler)

# Add various endpoints to unversioned URL
for endpoint in (info, links, references, structures, landing, versions):
Exemple #4
0
def render_landing_page(url: str) -> HTMLResponse:
    """Render and cache the landing page.

    This function uses the template file `./static/landing_page.html`, adapted
    from the original Jinja template. Instead of Jinja, some basic string
    replacement is used to fill out the fields from the server configuration.

    !!! warning "Careful"
        The removal of Jinja means that the fields are no longer validated as
        web safe before inclusion in the template.

    """
    meta = meta_values(url, 1, 1, more_data_available=False)
    major_version = __api_version__.split(".")[0]
    versioned_url = f"{get_base_url(url)}/v{major_version}/"

    template_dir = Path(__file__).parent.joinpath("static").resolve()

    html = (template_dir / "landing_page.html").read_text()

    # Build a dictionary that maps the old Jinja keys to the new simplified replacements
    replacements = {
        "api_version": __api_version__,
    }

    if meta.provider:
        replacements.update({
            "provider.name":
            meta.provider.name,
            "provider.prefix":
            meta.provider.prefix,
            "provider.description":
            meta.provider.description,
            "provider.homepage":
            str(meta.provider.homepage) or "",
        })

    if meta.implementation:
        replacements.update({
            "implementation.name":
            meta.implementation.name or "",
            "implementation.version":
            meta.implementation.version or "",
            "implementation.source_url":
            str(meta.implementation.source_url or ""),
        })

    for replacement in replacements:
        html = html.replace(f"{{{{ {replacement} }}}}",
                            replacements[replacement])

    # Build the list of endpoints. The template already opens and closes the `<ul>` tag.
    endpoints_list = [
        f'<li><a href="{versioned_url}{endp}">{versioned_url}{endp}</a></li>'
        for endp in list(ENTRY_COLLECTIONS.keys()) + ["info"]
    ]
    html = html.replace("{% ENDPOINTS %}", "\n".join(endpoints_list))

    # If the index base URL has been configured, also list it
    index_base_url_html = ""
    if CONFIG.index_base_url:
        index_base_url_html = f"""<h3>Index base URL:</h3>
<p><a href={CONFIG.index_base_url}>{CONFIG.index_base_url}</a></p>
"""
    html = html.replace("{% INDEX_BASE_URL %}", index_base_url_html)

    return HTMLResponse(html)