decorators.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Copyright (C) 2014 Andrey Antukh <niwi@niwi.be>
  2. # Copyright (C) 2014 Jesús Espino <jespinog@gmail.com>
  3. # Copyright (C) 2014 David Barragán <bameda@dbarragan.com>
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import warnings
  17. # Rest Framework 2.4 backport some decorators.
  18. def detail_route(methods=['get'], **kwargs):
  19. """
  20. Used to mark a method on a ViewSet that should be routed for detail requests.
  21. """
  22. def decorator(func):
  23. func.bind_to_methods = methods
  24. func.detail = True
  25. func.permission_classes = kwargs.get('permission_classes', [])
  26. func.kwargs = kwargs
  27. return func
  28. return decorator
  29. def list_route(methods=['get'], **kwargs):
  30. """
  31. Used to mark a method on a ViewSet that should be routed for list requests.
  32. """
  33. def decorator(func):
  34. func.bind_to_methods = methods
  35. func.detail = False
  36. func.permission_classes = kwargs.get('permission_classes', [])
  37. func.kwargs = kwargs
  38. return func
  39. return decorator
  40. def link(**kwargs):
  41. """
  42. Used to mark a method on a ViewSet that should be routed for detail GET requests.
  43. """
  44. msg = 'link is pending deprecation. Use detail_route instead.'
  45. warnings.warn(msg, PendingDeprecationWarning, stacklevel=2)
  46. def decorator(func):
  47. func.bind_to_methods = ['get']
  48. func.detail = True
  49. func.permission_classes = kwargs.get('permission_classes', [])
  50. func.kwargs = kwargs
  51. return func
  52. return decorator
  53. def action(methods=['post'], **kwargs):
  54. """
  55. Used to mark a method on a ViewSet that should be routed for detail POST requests.
  56. """
  57. msg = 'action is pending deprecation. Use detail_route instead.'
  58. warnings.warn(msg, PendingDeprecationWarning, stacklevel=2)
  59. def decorator(func):
  60. func.bind_to_methods = methods
  61. func.detail = True
  62. func.permission_classes = kwargs.get('permission_classes', [])
  63. func.kwargs = kwargs
  64. return func
  65. return decorator