Skip to content

Commit

Permalink
serializers: use cached communities slugs
Browse files Browse the repository at this point in the history
* use the cached slug when fetching the community slug in
  the serializers
  • Loading branch information
ntarocco committed Sep 28, 2023
1 parent 3f542b8 commit 9b2a1f1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
13 changes: 9 additions & 4 deletions invenio_rdm_records/resources/serializers/datacite/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@

"""DataCite based Schema for Invenio RDM Records."""

import time

from edtf import parse_edtf
from edtf.parser.grammar import ParseException
from flask import current_app, g
from flask import current_app
from flask_resources.serializers import BaseSerializerSchema
from invenio_access.permissions import system_identity
from invenio_communities import current_communities
from invenio_communities.communities.services.service import get_cached_community_slug
from invenio_i18n import lazy_gettext as _
from invenio_records_resources.proxies import current_service_registry
from invenio_vocabularies.proxies import current_service as vocabulary_service
Expand Down Expand Up @@ -432,11 +435,13 @@ def get_related_identifiers(self, obj):

# adding communities
communities = obj.get("parent", {}).get("communities", {}).get("ids", [])
service_id = current_communities.service.id
one_hour_cache = round(time.time() / 60 * 60)
for community_id in communities:
community = current_communities.service.read(
id_=community_id, identity=g.identity
slug = get_cached_community_slug(
community_id, service_id, ttl_hash=lambda x: one_hour_cache
)
url = f"{current_app.config['SITE_UI_URL']}/communities/{community.data['slug']}"
url = f"{current_app.config['SITE_UI_URL']}/communities/{slug}"
serialized_identifiers.append(
{
"relatedIdentifier": url,
Expand Down
13 changes: 9 additions & 4 deletions invenio_rdm_records/resources/serializers/dublincore/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

"""Dublin Core based Schema for Invenio RDM Records."""

import time

import bleach
import idutils
from flask import current_app, g
from flask import current_app
from flask_resources.serializers import BaseSerializerSchema
from invenio_access.permissions import system_identity
from invenio_communities import current_communities
from invenio_communities.communities.services.service import get_cached_community_slug
from invenio_vocabularies.proxies import current_service as vocabulary_service
from marshmallow import fields, missing

Expand Down Expand Up @@ -103,11 +106,13 @@ def get_relations(self, obj):

# Communities
communities = obj["parent"].get("communities", {}).get("ids", [])
service_id = current_communities.service.id
one_hour_cache = round(time.time() / 60 * 60)
for community_id in communities:
community = current_communities.service.read(
id_=community_id, identity=g.identity
slug = get_cached_community_slug(
community_id, service_id, ttl_hash=lambda x: one_hour_cache
)
url = f"{current_app.config['SITE_UI_URL']}/communities/{community.data['slug']}"
url = f"{current_app.config['SITE_UI_URL']}/communities/{slug}"
rels.append(self._transform_identifier(url, "url"))

# Parent doi
Expand Down
54 changes: 25 additions & 29 deletions invenio_rdm_records/resources/serializers/marcxml/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

"""MARCXML based Schema for Invenio RDM Records."""

import time

import bleach
from dateutil.parser import parse
from flask import current_app, g
from flask_resources.serializers import BaseSerializerSchema
from invenio_access.permissions import system_identity
from invenio_communities import current_communities
from invenio_communities.communities.services.service import get_cached_community_slug
from invenio_vocabularies.proxies import current_service as vocabulary_service
from marshmallow import fields, missing

Expand Down Expand Up @@ -42,7 +45,7 @@ class MARCXMLSchema(BaseSerializerSchema, CommonFieldsMixin):
# sources = fields.Constant(missing) # Corresponds to references in the metadata schema
formats = fields.Method("get_formats", data_key="520 1")
parent_id = fields.Method("get_parent_id", data_key="024 1")
community_ids = fields.Method("get_community_ids", data_key="980 ")
community_ids = fields.Method("get_communities", data_key="980 ")
sizes = fields.Method("get_sizes", data_key="520 2")
version = fields.Method("get_version", data_key="024 3")
funding = fields.Method(
Expand Down Expand Up @@ -127,24 +130,24 @@ def get_sizes(self, obj):
sizes = [{"a": s} for s in sizes_list]
return sizes

def get_community_ids(self, obj):
"""Get community ids."""
communities = obj["parent"].get("communities", {}).get("ids", [])

if not communities:
return missing

result = []
for community_id in communities:
community = current_communities.service.read(
id_=community_id, identity=g.identity
def _get_communities_slugs(self, ids):
"""Get communities slugs."""
service_id = current_communities.service.id
one_hour_cache = round(time.time() / 60 * 60)
return [
get_cached_community_slug(
community_id, service_id, ttl_hash=lambda x: one_hour_cache
)
slug = community.data["slug"]
# Communities are prefixed with ``user-``
comm = {"a": f"user-{slug}"}
result.append(comm)
for community_id in ids
]

return result
def get_communities(self, obj):
"""Get communities."""
ids = obj["parent"].get("communities", {}).get("ids", [])
if not ids:
return missing
# Communities are prefixed with ``user-``
return [{"a": f"user-{slug}"} for slug in self._get_communities_slugs(ids)]

def get_parent_id(self, obj):
"""Get parent id."""
Expand Down Expand Up @@ -194,18 +197,11 @@ def get_oai(self, obj):
result.update({"o": identifier})

# Communities
communities = obj["parent"].get("communities", {}).get("ids", [])
for community_id in communities:
# Resolve community to fetch its slug
community = current_communities.service.read(
id_=community_id, identity=g.identity
)
slug = community.data["slug"]

comm = f"user-{slug}"

# Add "p": [comm] or extend if there's already other communities
result.setdefault("p", []).append(comm)
ids = obj["parent"].get("communities", {}).get("ids", [])
for slug in self._get_communities_slugs(ids):
user_slug = f"user-{slug}"
# Add "p": [user_slug] or extend if there are already other communities
result.setdefault("p", []).append(user_slug)

return result or missing

Expand Down

0 comments on commit 9b2a1f1

Please sign in to comment.