# coding=utf-8

from rest_framework import generics
from utils.permission import IsCustomerUser
from utils.exceptions import CustomError
from utils import response_ok
from .serializers import ActivitySerializer
from apps.activity.models import Activity
from apps.activity.filters import ActivityFilter



class ActivityListViewSet(generics.ListAPIView):
    # permission_classes = [IsCustomerUser, ]
    queryset = Activity.objects.filter(delete=False)
    serializer_class = ActivitySerializer

    def filter_queryset(self, queryset):
        f = ActivityFilter(self.request.GET, queryset=queryset)
        return f.qs

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        serializer = self.get_serializer(queryset, many=True)
        return response_ok(serializer.data)


class ActivityDetailView(generics.RetrieveAPIView):
    # permission_classes = [IsCustomerUser, ]
    queryset = Activity.objects.filter(delete=False)
    serializer_class = ActivitySerializer

    def retrieve(self, request, *args, **kwargs):
        id = request.GET.get('id')
        instance = self.queryset.filter(id=id).first()
        if not instance:
            raise CustomError(u'未找到相应的活动！')
        serializer = self.get_serializer(instance)
        return response_ok(serializer.data)

