How to create a generic Audit callback?
How to create a generic Audit callback?
I'm writing an extension that will listen to several AuditActions (possibly the 141 Audit Actions that are marked as `IsRiskAction=true` in the `Q2_AuditAction` table). All actions will invoke the same generic code.
From the tutorial, it seems I need to create 141 individual methods, one per AuditAction, but instead of doing that, I was thinking to override the method `__getattr__` to return a reference to the generic method for any of the 141 audit actions I have registered in the `AUDIT_ACTIONS` list. This seems to be compatible with the implementation I see in `Q2AuditActionRequestHandler`. And it's working ok:
```
async def generic_audit_handler(self):
self.logger.info('Auditing action %s:', self.audit_details.audit_action)
def __getattr__(self, item):
if item in self.AUDIT_ACTIONS:
return self.generic_audit_handler
return super().__getattr__(item)
```
Another way I thought of doing this was to invoke `setattr(cls, audit_name, method_ref)` for each of the 141 AuditActions (in a loop on `AUDIT_ACTIONS`), this would actually create the methods in the class, although the `__getattr__` approach seems simpler.
Is there another, more proper, way to achieve this in Q2? Like a default action handler?
Thanks.
I'm writing an extension that will listen to several AuditActions (possibly the 141 Audit Actions that are marked as `IsRiskAction=true` in the `Q2_AuditAction` table). All actions will invoke the same generic code.
From the tutorial, it seems I need to create 141 individual methods, one per AuditAction, but instead of doing that, I was thinking to override the method `__getattr__` to return a reference to the generic method for any of the 141 audit actions I have registered in the `AUDIT_ACTIONS` list. This seems to be compatible with the implementation I see in `Q2AuditActionRequestHandler`. And seems to work ok:
```
async def generic_audit_handler(self):
self.logger.info('Auditing action %s:', self.audit_details.audit_action)
def __getattr__(self, item):
if item in self.AUDIT_ACTIONS:
return self.generic_audit_handler
return super().__getattr__(item)
```
Another way I thought of doing this was to invoke `setattr(cls, audit_name, method_ref)` for each of the 141 AuditActions (in a loop on `AUDIT_ACTIONS`), this would actually create the methods in the class, although the `__getattr__` approach seems simpler.
Is there another, more proper, way to achieve this in Q2? Like a default action handler?
Thanks.
#
How to create a generic Audit callback?
I'm writing an extension that will listen to several AuditActions (possibly the 141 Audit Actions that are marked as `IsRiskAction=true` in the `Q2_AuditAction` table). All actions will invoke the same generic code.
From the tutorial, it seems I need to create 141 individual methods, one per AuditAction, but instead of doing that, I was thinking to override the method `__getattr__` to return a reference to the generic method for any of the 141 audit actions I have registered in the `AUDIT_ACTIONS` list. This seems to be compatible with the implementation I see in `Q2AuditActionRequestHandler`. And seems to work ok:
```
async def generic_audit_handler(self):
self.logger.info('Auditing action %s:', self.audit_details.audit_action)
def __getattr__(self, item):
if item in self.AUDIT_ACTIONS:
return self.generic_audit_handler
return super().__getattr__(item)
```
Another way I thought of doing this was to invoke `setattr(cls, audit_name, method_ref)` for each of the 141 AuditActions (in a loop on `AUDIT_ACTIONS`), this would actually create the methods in the class, although the `__getattr__` approach seems simpler.
Is there another, more proper, way to achieve this in Q2? Like a default action handler?
Thanks.