-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* components: add internal notes cmp
- Loading branch information
Showing
13 changed files
with
379 additions
and
46 deletions.
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
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,64 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2024 CERN. | ||
# | ||
# Invenio-RDM-Records is free software; you can redistribute it and/or modify | ||
# it under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""RDM service component for custom fields.""" | ||
from copy import copy, deepcopy | ||
from datetime import datetime, timezone | ||
|
||
from invenio_pidstore.providers.recordid_v2 import RecordIdProviderV2 | ||
|
||
from .metadata import MetadataComponent | ||
|
||
|
||
class InternalNotesComponent(MetadataComponent): | ||
"""Service component for custom fields.""" | ||
|
||
field = "internal_notes" | ||
new_version_skip_fields = [] | ||
|
||
def create(self, identity, data=None, record=None, **kwargs): | ||
"""Inject note to the record.""" | ||
notes = data.get(self.field, []) | ||
for note in notes: | ||
note.setdefault("added_by", {"user": identity.id}) | ||
note.setdefault("timestamp", datetime.now(timezone.utc).isoformat()) | ||
note.setdefault("id", RecordIdProviderV2.generate_id()) | ||
record.update({"internal_notes": notes}) | ||
|
||
def update_draft(self, identity, data=None, record=None, **kwargs): | ||
"""Inject parsed metadata to the record.""" | ||
notes_updated = data.get(self.field, []) | ||
notes = deepcopy(record.get(self.field, [])) | ||
existing_ids = set([note["id"] for note in notes]) | ||
new_ids = set([note["id"] for note in notes_updated]) | ||
to_delete = existing_ids - new_ids | ||
|
||
if sorted(existing_ids) != sorted(new_ids): | ||
for note in notes_updated: | ||
if note["id"] in existing_ids: | ||
notes_updated.remove(note) | ||
continue | ||
note.setdefault("added_by", {"user": identity.id}) | ||
note.setdefault("timestamp", datetime.now(timezone.utc).isoformat()) | ||
note.setdefault("id", RecordIdProviderV2.generate_id()) | ||
for note in notes: | ||
if note["id"] in to_delete: | ||
notes.remove(note) | ||
|
||
record.update({"internal_notes": notes + notes_updated}) | ||
|
||
def publish(self, identity, draft=None, record=None, **kwargs): | ||
"""Update draft metadata.""" | ||
record.update({"internal_notes": draft.get(self.field, [])}) | ||
|
||
def edit(self, identity, draft=None, record=None, **kwargs): | ||
"""Update draft metadata.""" | ||
draft.update({"internal_notes": record.get(self.field, [])}) | ||
|
||
def new_version(self, identity, draft=None, record=None, **kwargs): | ||
"""Update draft metadata.""" | ||
draft.update({"internal_notes": copy(record.get(self.field, []))}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2024 CERN. | ||
# | ||
# Invenio-RDM-Records is free software; you can redistribute it and/or modify | ||
# it under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Query Parser module for InvenioRdmRecords.""" | ||
from luqum.tree import Word | ||
|
||
|
||
def word_internal_notes(node): | ||
"""Quote DOIs.""" | ||
if not node.value.startswith("internal_notes"): | ||
return node | ||
return Word(" ") |
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
Oops, something went wrong.