-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* closes zenodo/zenodo-rdm#696
- Loading branch information
Showing
7 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
invenio_communities/communities/records/systemfields/parent_community.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters