Skip to content

Commit

Permalink
systemfields: add parent community
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastro2 committed Feb 6, 2024
1 parent f94fb10 commit 712a438
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 1 deletion.
2 changes: 2 additions & 0 deletions invenio_communities/communities/records/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from invenio_communities.communities.records.systemfields.is_verified import (
IsVerifiedField,
)
from .systemfields.parent_community import ParentCommunityField

from ..dumpers.community_theme import CommunityThemeDumperExt
from ..dumpers.featured import FeaturedDumperExt
Expand All @@ -52,6 +53,7 @@ class Community(Record):
id = ModelField()
slug = ModelField()
pid = PIDSlugField("id", "slug")
parent = ParentCommunityField()

schema = ConstantField("$schema", "local://communities/communities-v1.0.0.json")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@
}
}
},
"parent": {
"type": "string",
"description": "Identifier of the parent community."
},
"theme": {
"type": "object",
"description": "Community theme settings.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
"is_deleted": {
"type": "boolean"
},
"parent": {
"type": "keyword"
},
"tombstone": {
"properties": {
"removal_reason": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
"is_deleted": {
"type": "boolean"
},
"parent": {
"type": "keyword"
},
"tombstone": {
"properties": {
"removal_reason": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
"is_deleted": {
"type": "boolean"
},
"parent": {
"type": "keyword"
},
"tombstone": {
"properties": {
"removal_reason": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2022 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Community PID slug field."""
from uuid import UUID

from invenio_records.systemfields import SystemField

from invenio_communities.proxies import current_communities


def is_valid_uuid(value):
"""Check if the provided value is a valid UUID."""
try:
UUID(str(value))
return True
except (ValueError, AttributeError, TypeError):
return False


class ParentCommunityField(SystemField):
"""System field for parent community."""

def __init__(self, key="parent"):
"""Create a new ParentCommunityField instance."""
super().__init__(key=key)

def obj(self, instance):
"""Get the access object."""
obj = self._get_cache(instance)
if obj is not None:
return obj

value = self.get_dictkey(instance)
if value:
obj = value
else:
obj = None

self._set_cache(instance, obj)
return obj

def set_obj(self, record, obj):
"""Set the access object."""
if is_valid_uuid(obj) or obj is None:
pass
elif isinstance(obj, current_communities.service.record_cls):
obj = str(obj.id)
else:
raise ValueError("Invalid parent community.")
record["parent"] = obj

def __get__(self, record, owner=None):
"""Get the record's access object."""
if record is None:
# access by class
return self

# access by object
return self.obj(record)

def __set__(self, record, obj):
"""Set the records access object."""
self.set_obj(record, obj)


25 changes: 24 additions & 1 deletion invenio_communities/communities/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from invenio_records_resources.services.uow import (
RecordCommitOp,
RecordIndexOp,
unit_of_work,
unit_of_work, RecordBulkIndexOp,
)
from invenio_requests import current_requests_service
from invenio_requests.services.results import EntityResolverExpandableField
Expand Down Expand Up @@ -474,6 +474,29 @@ def delete_community(
if len(requests) > 0:
raise OpenRequestsForCommunityDeletionError(len(requests))

# We want to fetch all the child communities and update them.
# task.apply_async(update_child_comm(parent_slug))
# import pdb
# pdb.set_trace()
child_communities = self._search(
"search",
identity,
{},
None,
extra_filter=dsl.query.Bool(
"must",
must=[
dsl.Q("match", **{"parent": record.slug}),
],
),
).execute()
child_communities = child_communities # Create a list with the slug/id
for child_community in child_communities:
child_record = self.record_cls.pid.resolve(child_community["slug"])
child_record.parent_community = None

uow.register(RecordBulkIndexOp(child_communities, indexer=self.indexer))

# Load tombstone data with the schema
data, errors = self.schema_tombstone.load(
data or {},
Expand Down

0 comments on commit 712a438

Please sign in to comment.