-
Notifications
You must be signed in to change notification settings - Fork 8
Base Resources
Serrano comes with a base resource that provide structure for the exposed resources. This is described here to make it easier to define other Avocado-backed resources for the most common use cases.
- Every request is checked for authentication if required. This also includes token-based access if enabled.
- If cross-origin resource sharing is enabled, every response gets two additional headers
Access-Control-Allow-Origin
andAccess-Control-Allow-Methods
.
Takes the request
object and returns a cleaned dict of GET parameters. This uses a restlib2 Parametizer class to clean the parameters.
resource = BaseResource()
params = resource.get_params(request)
To customize the clean of parameters and for setting defaults, create a subclass of restlib2.params.Parametizer
and set it to the parametizer
on the resource class. For example:
from restlib2.params import Parametizer, param_cleaners
from serrano.resources.base import BaseResource
class MyParametizer(Parametizer):
query = ''
page = 1
per_page = 10
def clean_page(self, value):
return param_cleaners.clean_int(value)
def clean_per_page(self, value):
return param_cleaners.clean_int(value)
class MyResource(BaseResource):
parametizer = MyParametizer
Takes the request
object and returns a DataContext
object that is currently applicable to the request. For convenience, the method also takes an attrs
keyword argument that would be the context nodes themselves. A series of checks are performed:
- If
attrs
are not supplied, deriveattrs
fromrequest
object using thecontext
key- For instance, if a POST request was being handled and the POST data contained a key
context
, that would be processed
- For instance, if a POST request was being handled and the POST data contained a key
- If
attrs
is adict
, validate and return - If
attrs
is an integer, lookup the context object for the requesting user and return - Attempt to current the session context for the user if one exists
- Fallback to an empty context
resource = BaseResource()
context = resource.get_context(request)
# Supply explicit context data
context = resource.get_context(request, attrs={'field': 1, 'operator': 'exact', 'value': 5})
This method is identical to the above get_context
regarding the processing steps. The difference, of course, is that is assumes valid DataView
data and it returns a DataView
instance.
resource = BaseResource()
view = resource.get_view(request)
# Supply explicit view data
view = resource.get_view(request, attrs={'columns': [3, 2, 1]})
Docs
Resource Docs
Development