generics.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from __future__ import unicode_literals, print_function
  2. from rest_framework import mixins
  3. from rest_framework.generics import GenericAPIView
  4. from rest_framework.viewsets import ModelViewSet
  5. from . import mixins as bulk_mixins
  6. __all__ = [
  7. 'BulkCreateAPIView',
  8. 'BulkDestroyAPIView',
  9. 'BulkModelViewSet',
  10. 'BulkUpdateAPIView',
  11. 'ListBulkCreateAPIView',
  12. 'ListBulkCreateDestroyAPIView',
  13. 'ListBulkCreateUpdateAPIView',
  14. 'ListBulkCreateUpdateDestroyAPIView',
  15. 'ListCreateBulkUpdateAPIView',
  16. 'ListCreateBulkUpdateDestroyAPIView',
  17. ]
  18. # ################################################## #
  19. # Concrete view classes that provide method handlers #
  20. # by composing the mixin classes with the base view. #
  21. # ################################################## #
  22. class BulkCreateAPIView(bulk_mixins.BulkCreateModelMixin,
  23. GenericAPIView):
  24. def post(self, request, *args, **kwargs):
  25. return self.create(request, *args, **kwargs)
  26. class BulkUpdateAPIView(bulk_mixins.BulkUpdateModelMixin,
  27. GenericAPIView):
  28. def put(self, request, *args, **kwargs):
  29. return self.bulk_update(request, *args, **kwargs)
  30. def patch(self, request, *args, **kwargs):
  31. return self.partial_bulk_update(request, *args, **kwargs)
  32. class BulkDestroyAPIView(bulk_mixins.BulkDestroyModelMixin,
  33. GenericAPIView):
  34. def delete(self, request, *args, **kwargs):
  35. return self.bulk_destroy(request, *args, **kwargs)
  36. class ListBulkCreateAPIView(mixins.ListModelMixin,
  37. bulk_mixins.BulkCreateModelMixin,
  38. GenericAPIView):
  39. def get(self, request, *args, **kwargs):
  40. return self.list(request, *args, **kwargs)
  41. def post(self, request, *args, **kwargs):
  42. return self.create(request, *args, **kwargs)
  43. class ListCreateBulkUpdateAPIView(mixins.ListModelMixin,
  44. mixins.CreateModelMixin,
  45. bulk_mixins.BulkUpdateModelMixin,
  46. GenericAPIView):
  47. def get(self, request, *args, **kwargs):
  48. return self.list(request, *args, **kwargs)
  49. def post(self, request, *args, **kwargs):
  50. return self.create(request, *args, **kwargs)
  51. def put(self, request, *args, **kwargs):
  52. return self.bulk_update(request, *args, **kwargs)
  53. def patch(self, request, *args, **kwargs):
  54. return self.partial_bulk_update(request, *args, **kwargs)
  55. class ListCreateBulkUpdateDestroyAPIView(mixins.ListModelMixin,
  56. mixins.CreateModelMixin,
  57. bulk_mixins.BulkUpdateModelMixin,
  58. bulk_mixins.BulkDestroyModelMixin,
  59. GenericAPIView):
  60. def get(self, request, *args, **kwargs):
  61. return self.list(request, *args, **kwargs)
  62. def post(self, request, *args, **kwargs):
  63. return self.create(request, *args, **kwargs)
  64. def put(self, request, *args, **kwargs):
  65. return self.bulk_update(request, *args, **kwargs)
  66. def patch(self, request, *args, **kwargs):
  67. return self.partial_bulk_update(request, *args, **kwargs)
  68. def delete(self, request, *args, **kwargs):
  69. return self.bulk_destroy(request, *args, **kwargs)
  70. class ListBulkCreateUpdateAPIView(mixins.ListModelMixin,
  71. bulk_mixins.BulkCreateModelMixin,
  72. bulk_mixins.BulkUpdateModelMixin,
  73. GenericAPIView):
  74. def get(self, request, *args, **kwargs):
  75. return self.list(request, *args, **kwargs)
  76. def post(self, request, *args, **kwargs):
  77. return self.create(request, *args, **kwargs)
  78. def put(self, request, *args, **kwargs):
  79. return self.bulk_update(request, *args, **kwargs)
  80. def patch(self, request, *args, **kwargs):
  81. return self.partial_bulk_update(request, *args, **kwargs)
  82. class ListBulkCreateDestroyAPIView(mixins.ListModelMixin,
  83. bulk_mixins.BulkCreateModelMixin,
  84. bulk_mixins.BulkDestroyModelMixin,
  85. GenericAPIView):
  86. def get(self, request, *args, **kwargs):
  87. return self.list(request, *args, **kwargs)
  88. def post(self, request, *args, **kwargs):
  89. return self.create(request, *args, **kwargs)
  90. def delete(self, request, *args, **kwargs):
  91. return self.bulk_destroy(request, *args, **kwargs)
  92. class ListBulkCreateUpdateDestroyAPIView(mixins.ListModelMixin,
  93. bulk_mixins.BulkCreateModelMixin,
  94. bulk_mixins.BulkUpdateModelMixin,
  95. bulk_mixins.BulkDestroyModelMixin,
  96. GenericAPIView):
  97. def get(self, request, *args, **kwargs):
  98. return self.list(request, *args, **kwargs)
  99. def post(self, request, *args, **kwargs):
  100. return self.create(request, *args, **kwargs)
  101. def put(self, request, *args, **kwargs):
  102. return self.bulk_update(request, *args, **kwargs)
  103. def patch(self, request, *args, **kwargs):
  104. return self.partial_bulk_update(request, *args, **kwargs)
  105. def delete(self, request, *args, **kwargs):
  106. return self.bulk_destroy(request, *args, **kwargs)
  107. # ########################################################## #
  108. # Concrete viewset classes that provide method handlers #
  109. # by composing the bulk mixin classes with the base viewset. #
  110. # ########################################################## #
  111. class BulkModelViewSet(bulk_mixins.BulkCreateModelMixin,
  112. bulk_mixins.BulkUpdateModelMixin,
  113. bulk_mixins.BulkDestroyModelMixin,
  114. ModelViewSet):
  115. pass