You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to add additional behaviour to a CRUDView, without having to repeat all the niceties of Neapolitan. Imagine a LeadCRUDView where I would like to add a POST /lead/<uuid:pk>/mark_qualified action.
Currently, we are not able to extend the Neapolitan Roles because they are Enum values. To make it work anyways, I created a CustomRole base class (see below), which replicates to Role Enums behaviour.
Observations and Challenges
Consistency: This approach allows arbitrary functionality to be added in a structured manner.
Complexity in CRUDViews: Adding many methods through roles can make CRUDViews unwieldy. Delegating method definitions to the roles themselves would improve legibility but complicate cross-method interactions.
Fragmentation: Role-related logic is currently scattered across:
(1) Role handlers
(2) The role itself
(3) Context data.
Neapolitan should provide Roles, which are extendable by default.
Test ensuring the Role doesn't break in obvious ways during Neapolitan upgrades.
fromunittestimportTestCasefromneapolitan.viewsimportRolefromdjango_backoffice.roleimportCustomRoleclassTestCustomRole(TestCase):
deftest_has_role_methods(self):
""" The CustomRole is a way of providing non-standard behaviour to Neapolitan views. If one of the methods is outright missing, things will go most likely wrong somewhere. This test is mainly a safeguard during upgrades of Neapolitan. """role_method_names= [
method_nameformethod_nameinRole.__dict__ifnotmethod_name.startswith("_") # No private members and magic methodsandnotmethod_name.isupper() # no enum members
]
formethod_nameinrole_method_names:
withself.subTest(f"Has method {method_name}"):
self.assertTrue(
hasattr(CustomRole, method_name),
f"{CustomRole.__name__} is missing method {method_name} of {Role.__name__}",
)
The text was updated successfully, but these errors were encountered:
Hi @dennisstritzke — thanks for opening the issue here. Quick summary is that my WIP is not dissimilar.
The reason for 80090e5 was that I allow custom roles to proxy for the default roles, and so override those. Using the identity is check didn't allow that.
I'm slowly swinging towards extracting all this from the WORK project, and then I'll be opening a PR here. I'll be sure to mention that here. (But you shouldn't have too much to worry about in terms of breakages I'd think.)
Version 24.8 added a Role equality test, stating ‘[…] preparatory to custom roles.’ As I utilise custom roles already, I want to share my take on them.
How are you imagining custom roles to work?
The Use Case
I want to add additional behaviour to a CRUDView, without having to repeat all the niceties of Neapolitan. Imagine a
LeadCRUDView
where I would like to add aPOST /lead/<uuid:pk>/mark_qualified
action.Currently, we are not able to extend the Neapolitan Roles because they are Enum values. To make it work anyways, I created a
CustomRole
base class (see below), which replicates to Role Enums behaviour.Observations and Challenges
(1) Role handlers
(2) The role itself
(3) Context data.
An Example
The Role
The view
Base Class and Test
Test ensuring the Role doesn't break in obvious ways during Neapolitan upgrades.
The text was updated successfully, but these errors were encountered: