/main/decorators.py

https://github.com/guerrillagrafters/GuerrillaGrafters_Web
Python | 27 lines | 16 code | 8 blank | 3 comment | 2 complexity | de87c871bc9865363e56166cc46d4fff MD5 | raw file
  1. import logging
  2. from django.core.handlers.wsgi import WSGIRequest
  3. from django.http import HttpResponseForbidden, HttpResponse
  4. # This is a decorator that can be used to create a custom admin action that
  5. # doesn't require selecting any objects. Specify a list of actions that can be used
  6. # that don't need any objects passed to it.
  7. class allowEmptyQuerySetForActions():
  8. def __init__(self, actions):
  9. self.actions = actions
  10. def __call__(self, original_class):
  11. def changelist_view(self, request, **kwargs):
  12. if( '_selected_action' not in request.POST and
  13. 'action' in request.POST and request.POST['action'] in self.actions ):
  14. new_query_dict = request.POST.copy()
  15. new_query_dict.__setitem__('_selected_action', '0')
  16. request._post = new_query_dict
  17. return super(type(self), self).changelist_view(request, **kwargs)
  18. original_class.changelist_view = changelist_view
  19. return original_class