-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
33 lines (28 loc) · 1.4 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
This module is used to create a standard view that could be extended in order
to handle the interaction with the REST API.
Interaction with main HTTP methods are intercepted and an event is generated
for each action (function called).
The interception of events is executed associating as a decorator to each function.
"""
from rest_framework.views import APIView
from core.plugins.decorators import generate_event
import inspect
class EventView(APIView):
"""
This class could be used as a mixin (multiple inheritance) in order to extend the base APIView
methods defined in the Django REST framework which are converted in HTTP methods.
Extending this class it is possible to generate an event every time that one of the main methods
are called and executed without any thrown (unhandled) exception.
"""
def __init__(self):
"""
Constructor used to associate the generate_event decorator to any overridden function.
"""
super(EventView, self).__init__()
for x in inspect.getmembers(self, (inspect.ismethod)):
if x[0] in ('get', 'post', 'put', 'delete', 'as_view', 'options'):
setattr(self, x[0], generate_event(getattr(self, x[0])))