from django_filters import rest_framework as filters
from rest_framework import serializers, viewsets
from ..views import MunityViewSet

from .models import Record

class RecordSerializer(serializers.ModelSerializer):
    class Meta:
        fields = [
            "id",
            "dif_value",
            "next_value",
            "created",
            "user",
            "product_object_id",
            "product_content_type",
            "action",
        ]
        model = Record

class RecordsFilter(filters.FilterSet):
    class Meta:
        fields = {
            "created": ["gt", "gte", "lt", "lte"],
            "user": ["exact"],
        }
        model = Record


class RecordsViewSet(viewsets.ModelViewSet):

    def get_queryset(self):
        model = self.serializer_class.Meta.model

        # filter request to current model workspace
        if "workspace_pk" in self.kwargs:
            return model.objects.filter(workspace=self.kwargs["workspace_pk"])
        return model.objects.all()

    serializer_class = RecordSerializer
    filterset_class = RecordsFilter

