from rest_framework import permissions, viewsets, filters

from utils import HeaderPagination, IsStaffOrAccountOwner

from .models import Sale, SaleComment
from .serializer import SaleSerializer, SaleCommentSerializer, CreateSaleCommentSerializer


class SaleViewSet(viewsets.ModelViewSet):
    queryset = Sale.objects.all().order_by('-id')
    serializer_class = SaleSerializer
    pagination_class = HeaderPagination
    filter_backends = [filters.SearchFilter]
    search_fields = ['title', 'content', 'author__username']

    def get_permissions(self):
        if self.request.method in permissions.SAFE_METHODS:
            self.permission_classes = [permissions.AllowAny,]
        elif self.request.method == 'POST':
            self.permission_classes = [permissions.IsAuthenticated,]
        else:
            self.permission_classes = [IsStaffOrAccountOwner,]
        return super(SaleViewSet, self).get_permissions()

    def get_queryset(self):
        username = self.request.query_params.get('profile', None)
        if username is not None:
            queryset = Sale.objects.filter(author__username=username)
            return queryset
        queryset = super(SaleViewSet, self).get_queryset()
        return queryset


class SaleCommentViewSet(viewsets.ModelViewSet):
    serializer_class = SaleCommentSerializer
    queryset = SaleComment.objects.all()
    pagination_class = HeaderPagination

    def get_permissions(self):
        if self.request.method in permissions.SAFE_METHODS:
            self.permission_classes = [permissions.AllowAny,]
        elif self.request.method == 'POST':
            self.permission_classes = [permissions.IsAuthenticated,]
        else:
            self.permission_classes = [IsStaffOrAccountOwner,]
        return super(SaleCommentViewSet, self).get_permissions()

    def get_serializer_class(self):
        if self.request.method == "POST":
            return CreateSaleCommentSerializer
        else:
            return SaleCommentSerializer


2b3c5424d91f7f50224b0f8571315231a85d76cf
<mask token>


class SaleViewSet(viewsets.ModelViewSet):
    <mask token>
    <mask token>
    <mask token>
    <mask token>
    <mask token>
    <mask token>
    <mask token>


class SaleCommentViewSet(viewsets.ModelViewSet):
    serializer_class = SaleCommentSerializer
    queryset = SaleComment.objects.all()
    pagination_class = HeaderPagination

    def get_permissions(self):
        if self.request.method in permissions.SAFE_METHODS:
            self.permission_classes = [permissions.AllowAny]
        elif self.request.method == 'POST':
            self.permission_classes = [permissions.IsAuthenticated]
        else:
            self.permission_classes = [IsStaffOrAccountOwner]
        return super(SaleCommentViewSet, self).get_permissions()

    def get_serializer_class(self):
        if self.request.method == 'POST':
            return CreateSaleCommentSerializer
        else:
            return SaleCommentSerializer

<mask token>


class SaleViewSet(viewsets.ModelViewSet):
    <mask token>
    <mask token>
    <mask token>
    <mask token>
    <mask token>
    <mask token>

    def get_queryset(self):
        username = self.request.query_params.get('profile', None)
        if username is not None:
            queryset = Sale.objects.filter(author__username=username)
            return queryset
        queryset = super(SaleViewSet, self).get_queryset()
        return queryset


class SaleCommentViewSet(viewsets.ModelViewSet):
    serializer_class = SaleCommentSerializer
    queryset = SaleComment.objects.all()
    pagination_class = HeaderPagination

    def get_permissions(self):
        if self.request.method in permissions.SAFE_METHODS:
            self.permission_classes = [permissions.AllowAny]
        elif self.request.method == 'POST':
            self.permission_classes = [permissions.IsAuthenticated]
        else:
            self.permission_classes = [IsStaffOrAccountOwner]
        return super(SaleCommentViewSet, self).get_permissions()

    def get_serializer_class(self):
        if self.request.method == 'POST':
            return CreateSaleCommentSerializer
        else:
            return SaleCommentSerializer

<mask token>


class SaleViewSet(viewsets.ModelViewSet):
    <mask token>
    <mask token>
    <mask token>
    <mask token>
    <mask token>

    def get_permissions(self):
        if self.request.method in permissions.SAFE_METHODS:
            self.permission_classes = [permissions.AllowAny]
        elif self.request.method == 'POST':
            self.permission_classes = [permissions.IsAuthenticated]
        else:
            self.permission_classes = [IsStaffOrAccountOwner]
        return super(SaleViewSet, self).get_permissions()

    def get_queryset(self):
        username = self.request.query_params.get('profile', None)
        if username is not None:
            queryset = Sale.objects.filter(author__username=username)
            return queryset
        queryset = super(SaleViewSet, self).get_queryset()
        return queryset


class SaleCommentViewSet(viewsets.ModelViewSet):
    serializer_class = SaleCommentSerializer
    queryset = SaleComment.objects.all()
    pagination_class = HeaderPagination

    def get_permissions(self):
        if self.request.method in permissions.SAFE_METHODS:
            self.permission_classes = [permissions.AllowAny]
        elif self.request.method == 'POST':
            self.permission_classes = [permissions.IsAuthenticated]
        else:
            self.permission_classes = [IsStaffOrAccountOwner]
        return super(SaleCommentViewSet, self).get_permissions()

    def get_serializer_class(self):
        if self.request.method == 'POST':
            return CreateSaleCommentSerializer
        else:
            return SaleCommentSerializer

<mask token>


class SaleViewSet(viewsets.ModelViewSet):
    queryset = Sale.objects.all().order_by('-id')
    serializer_class = SaleSerializer
    pagination_class = HeaderPagination
    filter_backends = [filters.SearchFilter]
    search_fields = ['title', 'content', 'author__username']

    def get_permissions(self):
        if self.request.method in permissions.SAFE_METHODS:
            self.permission_classes = [permissions.AllowAny]
        elif self.request.method == 'POST':
            self.permission_classes = [permissions.IsAuthenticated]
        else:
            self.permission_classes = [IsStaffOrAccountOwner]
        return super(SaleViewSet, self).get_permissions()

    def get_queryset(self):
        username = self.request.query_params.get('profile', None)
        if username is not None:
            queryset = Sale.objects.filter(author__username=username)
            return queryset
        queryset = super(SaleViewSet, self).get_queryset()
        return queryset


class SaleCommentViewSet(viewsets.ModelViewSet):
    serializer_class = SaleCommentSerializer
    queryset = SaleComment.objects.all()
    pagination_class = HeaderPagination

    def get_permissions(self):
        if self.request.method in permissions.SAFE_METHODS:
            self.permission_classes = [permissions.AllowAny]
        elif self.request.method == 'POST':
            self.permission_classes = [permissions.IsAuthenticated]
        else:
            self.permission_classes = [IsStaffOrAccountOwner]
        return super(SaleCommentViewSet, self).get_permissions()

    def get_serializer_class(self):
        if self.request.method == 'POST':
            return CreateSaleCommentSerializer
        else:
            return SaleCommentSerializer

from rest_framework import permissions, viewsets, filters

from utils import HeaderPagination, IsStaffOrAccountOwner

from .models import Sale, SaleComment
from .serializer import SaleSerializer, SaleCommentSerializer, CreateSaleCommentSerializer


class SaleViewSet(viewsets.ModelViewSet):
    queryset = Sale.objects.all().order_by('-id')
    serializer_class = SaleSerializer
    pagination_class = HeaderPagination
    filter_backends = [filters.SearchFilter]
    search_fields = ['title', 'content', 'author__username']

    def get_permissions(self):
        if self.request.method in permissions.SAFE_METHODS:
            self.permission_classes = [permissions.AllowAny,]
        elif self.request.method == 'POST':
            self.permission_classes = [permissions.IsAuthenticated,]
        else:
            self.permission_classes = [IsStaffOrAccountOwner,]
        return super(SaleViewSet, self).get_permissions()

    def get_queryset(self):
        username = self.request.query_params.get('profile', None)
        if username is not None:
            queryset = Sale.objects.filter(author__username=username)
            return queryset
        queryset = super(SaleViewSet, self).get_queryset()
        return queryset


class SaleCommentViewSet(viewsets.ModelViewSet):
    serializer_class = SaleCommentSerializer
    queryset = SaleComment.objects.all()
    pagination_class = HeaderPagination

    def get_permissions(self):
        if self.request.method in permissions.SAFE_METHODS:
            self.permission_classes = [permissions.AllowAny,]
        elif self.request.method == 'POST':
            self.permission_classes = [permissions.IsAuthenticated,]
        else:
            self.permission_classes = [IsStaffOrAccountOwner,]
        return super(SaleCommentViewSet, self).get_permissions()

    def get_serializer_class(self):
        if self.request.method == "POST":
            return CreateSaleCommentSerializer
        else:
            return SaleCommentSerializer

