Skip to content

Commit

Permalink
Updates according to @ml-evs review
Browse files Browse the repository at this point in the history
Better documentation of `map_back` and _only_ use `task_id` for
structures ids.
  • Loading branch information
CasperWA committed Dec 2, 2019
1 parent 42cc88e commit 8ce3e80
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 12 deletions.
12 changes: 9 additions & 3 deletions optimade/server/mappers/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ResourceMapper:

ENDPOINT: str = ""
ALIASES: Tuple[Tuple[str, str]] = ()
TOP_LEVEL_NON_ATTRIBUTES_FIELDS: set = {"id", "type", "relationships", "links"}

@classmethod
def all_aliases(cls) -> Tuple[Tuple[str, str]]:
Expand Down Expand Up @@ -36,6 +37,12 @@ def alias_for(cls, field: str) -> str:
def map_back(cls, doc: dict) -> dict:
"""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.TOPLEVEL_NON_ATTRIBUTES_FIELDS``.
All fields not in ``cls.TOPLEVEL_NON_ATTRIBUTES_FIELDS`` + "attributes" will be removed.
Finally, the ``type`` is given the value of the specified ``cls.ENDPOINT``.
:param doc: A resource object in MongoDB format
:type doc: dict
Expand All @@ -59,11 +66,10 @@ def map_back(cls, doc: dict) -> dict:
raise Exception("Will overwrite doc field!")
attributes = newdoc.copy()

top_level_entry_fields = {"id", "type", "relationships", "links"}
for k in top_level_entry_fields:
for k in cls.TOP_LEVEL_NON_ATTRIBUTES_FIELDS:
attributes.pop(k, None)
for k in list(newdoc.keys()):
if k not in top_level_entry_fields:
if k not in cls.TOP_LEVEL_NON_ATTRIBUTES_FIELDS:
del newdoc[k]

newdoc["type"] = cls.ENDPOINT
Expand Down
1 change: 0 additions & 1 deletion optimade/server/mappers/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
class LinksMapper(ResourceMapper):

ENDPOINT = "links"
ALIASES = (("id", "task_id"),)

@classmethod
def map_back(cls, doc: dict) -> dict:
Expand Down
1 change: 0 additions & 1 deletion optimade/server/mappers/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@
class ReferenceMapper(ResourceMapper):

ENDPOINT = "references"
ALIASES = (("id", "task_id"),)
2 changes: 1 addition & 1 deletion optimade/server/tests/test_links.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"_id": {
"$oid": "5cfb441f053b174410700d03"
},
"task_id": "index",
"id": "index",
"last_modified": {
"$date": "2019-11-12T14:24:37.331Z"
},
Expand Down
6 changes: 3 additions & 3 deletions optimade/server/tests/test_references.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"_id": {
"$oid": "5cfb441f053b174410701bec"
},
"task_id": "dijkstra1968",
"id": "dijkstra1968",
"last_modified": {
"$date": "2019-11-12T14:24:37.331Z"
},
Expand All @@ -23,7 +23,7 @@
"_id": {
"$oid": "5cfb441f053b174410701bed"
},
"task_id": "maddox1988",
"id": "maddox1988",
"last_modified": {
"$date": "2019-11-27T14:24:37.331Z"
},
Expand All @@ -43,7 +43,7 @@
"_id": {
"$oid": "5cfb441f053b174410701bea"
},
"task_id": "dummy2019",
"id": "dummy2019",
"last_modified": {
"$date": "2019-11-23T14:24:37.332Z"
},
Expand Down
5 changes: 2 additions & 3 deletions optimade/server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def get_providers():
from bson.objectid import ObjectId

mat_consortia_providers = requests.get(
"https://raw.githubusercontent.com/Materials-Consortia/OPTiMaDe/develop/providers.json"
"https://raw.githubusercontent.com/Materials-Consortia/OPTiMaDe/master/providers.json"
).json()

providers_list = []
Expand All @@ -296,11 +296,10 @@ def get_providers():
if provider["id"] == "exmpl":
continue

provider["task_id"] = provider.pop("id")
provider.update(provider.pop("attributes"))

# Create MongoDB id
oid = provider["task_id"] + provider["type"]
oid = provider["id"] + provider["type"]
if len(oid) < 12:
oid = oid + "0" * (12 - len(oid))
elif len(oid) > 12:
Expand Down

0 comments on commit 8ce3e80

Please sign in to comment.