Skip to content

gateways

GatewaysMapper

map_back(doc) classmethod

Map properties from MongoDB to OPTIMADE.

Starting from a MongoDB document doc, map the DB fields to the corresponding OPTIMADE fields. Then, the fields are all added to the top-level field "attributes", with the exception of other top-level fields, defined in cls.TOP_LEVEL_NON_ATTRIBUTES_FIELDS. All fields not in cls.TOP_LEVEL_NON_ATTRIBUTES_FIELDS + "attributes" will be removed. Finally, the type is given the value of the specified cls.ENDPOINT.

Parameters:

Name Type Description Default
doc dict

A resource object in MongoDB format.

required

Returns:

Type Description
dict

A resource object in OPTIMADE format.

Source code in optimade_gateway/mappers/gateways.py
@classmethod
def map_back(cls, doc: dict) -> dict:
    from optimade.server.routers.utils import BASE_URL_PREFIXES

    if "_id" in doc:
        _id = str(doc.pop("_id"))
        if "id" not in doc:
            doc["id"] = _id

    doc["links"] = {
        "self": AnyUrl(
            url=f"{CONFIG.base_url.strip('/')}{BASE_URL_PREFIXES['major']}/{cls.ENDPOINT}/{doc['id']}",
            scheme=CONFIG.base_url.split("://")[0],
            host=CONFIG.base_url.split("://")[1].split("/")[0],
        )
    }
    return super().map_back(doc)
Back to top