Skip to content

Commit 35e06d7

Browse files
committed
Move Resource.takes_child() and Reosurce.takes_parent() exclusively to Action
Refs #2563 (comment)
1 parent 2ec8995 commit 35e06d7

3 files changed

Lines changed: 13 additions & 29 deletions

File tree

datasette/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1609,7 +1609,9 @@ def _actions(self):
16091609
"description": action.description,
16101610
"takes_parent": action.takes_parent,
16111611
"takes_child": action.takes_child,
1612-
"resource_class": action.resource_class.__name__ if action.resource_class else None,
1612+
"resource_class": (
1613+
action.resource_class.__name__ if action.resource_class else None
1614+
),
16131615
"also_requires": action.also_requires,
16141616
}
16151617
for action in sorted(self.actions.values(), key=lambda a: a.name)

datasette/permissions.py

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,6 @@ def private(self) -> bool:
5454
def private(self, value: bool):
5555
self._private = value
5656

57-
@classmethod
58-
def takes_parent(cls) -> bool:
59-
"""
60-
Whether actions on this resource can work with a parent.
61-
62-
Returns True for parent-level and child-level resources.
63-
Returns False for top-level resources (where parent_class is None).
64-
"""
65-
return cls.parent_class is not None
66-
67-
@classmethod
68-
def takes_child(cls) -> bool:
69-
"""
70-
Whether actions on this resource can work with a child.
71-
72-
- Top-level resources (no parent): False
73-
- Child-level resources (has parent): True
74-
"""
75-
return cls.parent_class is not None
7657

7758
@classmethod
7859
def __init_subclass__(cls):
@@ -126,24 +107,25 @@ class Action:
126107
@property
127108
def takes_parent(self) -> bool:
128109
"""
129-
Whether this action requires a parent identifier.
110+
Whether this action requires a parent identifier when instantiating its resource.
130111
131-
Returns False for global-only actions (no resource_class), otherwise delegates to resource_class.
112+
Returns False for global-only actions (no resource_class).
113+
Returns True for all actions with a resource_class (all resources require a parent identifier).
132114
"""
133-
if self.resource_class is None:
134-
return False
135-
return self.resource_class.takes_parent()
115+
return self.resource_class is not None
136116

137117
@property
138118
def takes_child(self) -> bool:
139119
"""
140-
Whether this action requires a child identifier.
120+
Whether this action requires a child identifier when instantiating its resource.
141121
142-
Returns False for global-only actions (no resource_class), otherwise delegates to resource_class.
122+
Returns False for global actions (no resource_class).
123+
Returns False for parent-level resources (DatabaseResource - parent_class is None).
124+
Returns True for child-level resources (TableResource, QueryResource - have a parent_class).
143125
"""
144126
if self.resource_class is None:
145127
return False
146-
return self.resource_class.takes_child()
128+
return self.resource_class.parent_class is not None
147129

148130

149131
_reason_id = 1

docs/upgrade-1.0a20.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def register_actions(datasette):
8686
]
8787
```
8888

89-
The hierarchy information (whether an action takes parent/child parameters) is now determined by the `Resource` class, not the `Action`. For backward compatibility, `Action` still has `takes_parent` and `takes_child` properties that delegate to the resource class.
89+
The hierarchy information (whether an action takes parent/child parameters) is now derived from the `Resource` class hierarchy. `Action` has `takes_parent` and `takes_child` properties that are computed based on the `resource_class` and its `parent_class` attribute.
9090

9191
## permission_allowed() hook is replaced by permission_resources_sql()
9292

0 commit comments

Comments
 (0)