Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 5 roky
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from django.utils.decorators import method_decorator
  2. from django.views.decorators.cache import cache_page
  3. from django_filters.rest_framework import DjangoFilterBackend
  4. from rest_framework import viewsets, mixins, routers
  5. from rest_framework.filters import SearchFilter, OrderingFilter
  6. from rest_framework.viewsets import GenericViewSet
  7. from taggit.models import Tag
  8. from core import serializers as api
  9. from core.models import Image, Pin, Board
  10. from core.permissions import IsOwnerOrReadOnly, OwnerOnlyIfPrivate
  11. from core.serializers import filter_private_pin, filter_private_board
  12. class ImageViewSet(mixins.CreateModelMixin, GenericViewSet):
  13. queryset = Image.objects.all()
  14. serializer_class = api.ImageSerializer
  15. def create(self, request, *args, **kwargs):
  16. return super(ImageViewSet, self).create(request, *args, **kwargs)
  17. class PinViewSet(viewsets.ModelViewSet):
  18. serializer_class = api.PinSerializer
  19. filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
  20. filter_fields = ("submitter__username", 'tags__name', )
  21. ordering_fields = ('-id', )
  22. ordering = ('-id', )
  23. permission_classes = [IsOwnerOrReadOnly("submitter"), OwnerOnlyIfPrivate("submitter")]
  24. def get_queryset(self):
  25. query = Pin.objects.all()
  26. request = self.request
  27. return filter_private_pin(request, query)
  28. class BoardViewSet(viewsets.ModelViewSet):
  29. serializer_class = api.BoardSerializer
  30. filter_backends = (DjangoFilterBackend, OrderingFilter, SearchFilter)
  31. search_fields = ("name", )
  32. filter_fields = ("submitter__username", )
  33. ordering_fields = ('-id', )
  34. ordering = ('-id', )
  35. permission_classes = [IsOwnerOrReadOnly("submitter"), OwnerOnlyIfPrivate("submitter")]
  36. def get_queryset(self):
  37. return filter_private_board(self.request, Board.objects.all())
  38. class BoardAutoCompleteViewSet(
  39. mixins.ListModelMixin,
  40. viewsets.GenericViewSet,
  41. ):
  42. serializer_class = api.BoardAutoCompleteSerializer
  43. filter_backends = (DjangoFilterBackend, OrderingFilter)
  44. filter_fields = ("submitter__username", )
  45. ordering_fields = ('-id', )
  46. ordering = ('-id', )
  47. pagination_class = None
  48. permission_classes = [OwnerOnlyIfPrivate("submitter"), ]
  49. def get_queryset(self):
  50. return filter_private_board(self.request, Board.objects.all())
  51. class TagAutoCompleteViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
  52. queryset = Tag.objects.all()
  53. serializer_class = api.TagAutoCompleteSerializer
  54. pagination_class = None
  55. @method_decorator(cache_page(60 * 5))
  56. def list(self, request, *args, **kwargs):
  57. return super(TagAutoCompleteViewSet, self).list(
  58. request,
  59. *args,
  60. **kwargs
  61. )
  62. drf_router = routers.DefaultRouter()
  63. drf_router.register(r'pins', PinViewSet, basename="pin")
  64. drf_router.register(r'images', ImageViewSet)
  65. drf_router.register(r'boards', BoardViewSet, basename="board")
  66. drf_router.register(r'tags-auto-complete', TagAutoCompleteViewSet)
  67. drf_router.register(r'boards-auto-complete', BoardAutoCompleteViewSet, base_name="board")