|
10 | 10 |
|
11 | 11 | from enum import Enum |
12 | 12 | from functools import partial |
| 13 | +from werkzeug.utils import cached_property |
13 | 14 |
|
14 | 15 | from invenio_records.dumpers import SearchDumper |
15 | 16 | from invenio_records.systemfields import ConstantField, DictField, ModelField |
16 | 17 | from invenio_records_resources.records.api import Record |
17 | 18 | from invenio_records_resources.records.systemfields import IndexField |
18 | 19 |
|
19 | 20 | from ..customizations import RequestState as State |
20 | | -from .dumpers import CalculatedFieldDumperExt, GrantTokensDumperExt |
| 21 | +from .dumpers import ( |
| 22 | + CalculatedFieldDumperExt, |
| 23 | + GrantTokensDumperExt, |
| 24 | + ParentChildDumperExt, |
| 25 | +) |
21 | 26 | from .models import RequestEventModel, RequestMetadata |
22 | 27 | from .systemfields import ( |
23 | 28 | EntityReferenceField, |
@@ -51,6 +56,13 @@ class RequestEvent(Record): |
51 | 56 |
|
52 | 57 | model_cls = RequestEventModel |
53 | 58 |
|
| 59 | + dumper = SearchDumper( |
| 60 | + extensions=[ |
| 61 | + ParentChildDumperExt(), |
| 62 | + ] |
| 63 | + ) |
| 64 | + """Search dumper with parent-child relationship extension.""" |
| 65 | + |
54 | 66 | # Systemfields |
55 | 67 | metadata = None |
56 | 68 |
|
@@ -83,6 +95,36 @@ class RequestEvent(Record): |
83 | 95 | created_by = EntityReferenceField("created_by", check_referenced) |
84 | 96 | """Who created the event.""" |
85 | 97 |
|
| 98 | + parent_id = ModelField("parent_id") |
| 99 | + """The parent event ID for parent-child relationships.""" |
| 100 | + |
| 101 | + parent_child = DictField("parent_child") |
| 102 | + """OpenSearch join relationship for parent-child queries.""" |
| 103 | + |
| 104 | + @cached_property |
| 105 | + def parent(self): |
| 106 | + """Get the parent event as a RequestEvent instance. |
| 107 | +
|
| 108 | + Returns None if this is a top-level event or if the event type |
| 109 | + doesn't support children. |
| 110 | + """ |
| 111 | + if not self.model or not self.model.parent: |
| 112 | + return None |
| 113 | + |
| 114 | + # Use the SQLAlchemy relationship to get parent (efficient) |
| 115 | + parent_model = self.model.parent |
| 116 | + return type(self)(parent_model.data, model=parent_model) |
| 117 | + |
| 118 | + def pre_commit(self): |
| 119 | + """Hook called before committing the record. |
| 120 | +
|
| 121 | + Validates that children are allowed for this event type. |
| 122 | + """ |
| 123 | + from .validators import validate_children_allowed |
| 124 | + |
| 125 | + validate_children_allowed(self) |
| 126 | + super().pre_commit() |
| 127 | + |
86 | 128 |
|
87 | 129 | class Request(Record): |
88 | 130 | """A generic request record.""" |
|
0 commit comments