Package expedient :: Package common :: Package permissions :: Module utils
[hide private]
[frames] | no frames]

Module utils

source code

Created on Jun 1, 2010


Author: jnaous

Functions [hide private]
 
get_user_from_req(request, *args, **kwargs)
Get the user from the request.
source code
 
get_queryset(klass, index, filter='pk')
Returns a function that can be used for the require_*_permission_for_view decorators to get a queryset from some argument.
source code
 
get_leaf_queryset(parent_klass, index, filter='pk')
Same as get_queryset but also calls the as_leaf_class function on the first element in the queryset and returns a queryset with the returned object's class.
source code
 
get_queryset_from_class(klass)
Returns a function usable as the target_func of the require_objs_permissions_for_view decorator.
source code
 
get_queryset_from_id(klass, id)
Returns a function usable as a target_func parameter.
source code
 
get_object_from_ids(ct_id, id)
Get an object from the ContentType id and from the object's id.
source code
a callable.
permissions_save_override(permittee_kw, model_func, create_perm, edit_perm, delete_perm)
Get a save function that can be used to enforce create, edit, and delete permissions.
source code
 
permissions_delete_override(permittee_kw, model_func, delete_perm)
Get a delete function that can be used to enforce delete permissions.
source code
Variables [hide private]
  __package__ = 'expedient.common.permissions'
Function Details [hide private]

get_user_from_req(request, *args, **kwargs)

source code 

Get the user from the request. This function is helpful when using the require_*_permission_for_view decorators.

For example:

   @require_objs_permissions_for_view(
       ["can_view_obj_detail"],
       get_user_from_req,
       get_objects_from_filter_func(Obj, 1),
       ["GET"],
   )
   def view_obj_detail(request, obj_id):
       ...
Parameters:
  • request (HttpRequest) - the request object

get_queryset(klass, index, filter='pk')

source code 

Returns a function that can be used for the require_*_permission_for_view decorators to get a queryset from some argument.

The returned function has a signature (*args, **kwargs) and mainly does the following:

   klass.objects.filter(**{filter: arg})

where arg is obtained from the arguments. If index is an int, arg is assumed to be positional. Otherwise, it is assumed to be a keyword.

For example:

   @require_obj_permission_for_view(
       ["can_view_obj_detail"],
       get_user_from_req,
       get_queryset(Obj, 1),
       ["GET"],
   )
   def view_obj_detail(request, obj_id):
       ...
Parameters:
  • klass (class) - The class of the object to be returned.
  • index (int for positional, hashable for keyword.) - location of the id in the arguments when the arguments are given as (*args, **kwargs).
  • filter (str) - a filter to be used for obtaining the object.
Returns:
A callable that returns an object from (*args, **kwargs)

get_queryset_from_class(klass)

source code 

Returns a function usable as the target_func of the require_objs_permissions_for_view decorator. The returned function returns the ContentType queryset for a class. This can be used to enforce class level permissions on views.

Parameters:
  • klass - the model class for which we want the queryset.

get_queryset_from_id(klass, id)

source code 

Returns a function usable as a target_func parameter. The returned function returns a QuerySet containing one object with the given id.

Parameters:
  • klass - the class of the queryset's model.
  • id - the object's id.

get_object_from_ids(ct_id, id)

source code 

Get an object from the ContentType id and from the object's id.

Parameters:
  • ct_id - ContentType's id for the object class.
  • id - object's id.

permissions_save_override(permittee_kw, model_func, create_perm, edit_perm, delete_perm)

source code 

Get a save function that can be used to enforce create, edit, and delete permissions.

For example:

   class ModelX(models.Model):
       ...
       save = permissions_save_override(
           "user", lambda: ModelX, "can_create", "can_edit", "can_delete")
Parameters:
  • permittee_kw (str.) - the keyword used to store the permittee in threadlocals
  • model_func (Model subclass) - A callable that returns the the class.
  • create_perm (str) - The name of the creation permission for the class.
  • edit_perm (str) - The name of the edit permission for the instance.
  • delete_perm (str) - the name of the delete permission for the instance.
Returns: a callable.
a save function that can be used to enforce permissions.

permissions_delete_override(permittee_kw, model_func, delete_perm)

source code 

Get a delete function that can be used to enforce delete permissions.

For example:

   class ModelX(models.Model):
       ...
       delete = permissions_delete_override(
           "user", lambda: ModelX, "can_delete")
Parameters:
  • permittee_kw (str.) - the keyword used to store the permittee in threadlocals
  • model_func (Model subclass) - A callable that returns the class.
  • delete_perm (str) - the name of the delete permission for the instance.