# Path: script_builder/serializers.py
# class MungerSerializer(PartialAllowed):
#     data_fields = DataFieldSerializer(many=True, read_only=True)
#     pivot_fields = PivotFieldSerializer(many=True, read_only=True)
#     field_types = FieldTypeSerializer(many=True, read_only=True)
# 
#     class Meta:
#         model = MungerBuilder
#         fields = ('munger_name', 'munger_template', 'input_path', 'output_path', 'rows_to_delete_top',
#                   'rows_to_delete_bottom', 'data_fields', 'pivot_fields', 'field_types',
#                   'default_aggregate_field_type', 'is_sample')
# 
# class DataFieldSerializer(PartialAllowed):
#     class Meta:
#         model = DataField
#         fields = ('id', 'munger_builder', 'current_name', 'new_name', 'active_name')
# 
# class PivotFieldSerializer(PartialAllowed):
#     class Meta:
#         model = PivotField
# 
# class FieldTypeSerializer(PartialAllowed):
#     class Meta:
#         model = FieldType
#         fields = '__all__'


from guardian.shortcuts import get_objects_for_user
from .serializers import MungerSerializer, DataFieldSerializer, PivotFieldSerializer, FieldTypeSerializer
from rest_framework.response import Response
from rest_framework import status, filters, mixins, generics, permissions
    def get(self, request, *args, **kwargs):
        if 'pk' in kwargs:
            return self.retrieve(request, *args, **kwargs)
        else:
            return self.list(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        user = request.user
        if self.under_limit(user):
            return self.create(request, *args, **kwargs)
        else:
            error_string = 'Cannot Create more {} - Delete some to make space'.format(
                self.__class__.__name__
            )
            return Response(error_string, status=status.HTTP_403_FORBIDDEN)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)

    def under_limit(self, user):
        meta_name = self.serializer_class.Meta.model._meta.model_name
        permission_name = 'script_builder.change_{}'.format(meta_name)
        current_objects = get_objects_for_user(user, permission_name)
        return len(current_objects) <= self.USER_OBJECT_LIMIT or user.is_superuser

class Mungers(MungerBuilderAPIView):
    USER_OBJECT_LIMIT = 5

# Path: script_builder/serializers.py
# class MungerSerializer(PartialAllowed):
#     data_fields = DataFieldSerializer(many=True, read_only=True)
#     pivot_fields = PivotFieldSerializer(many=True, read_only=True)
#     field_types = FieldTypeSerializer(many=True, read_only=True)
# 
#     class Meta:
#         model = MungerBuilder
#         fields = ('munger_name', 'munger_template', 'input_path', 'output_path', 'rows_to_delete_top',
#                   'rows_to_delete_bottom', 'data_fields', 'pivot_fields', 'field_types',
#                   'default_aggregate_field_type', 'is_sample')
# 
# class DataFieldSerializer(PartialAllowed):
#     class Meta:
#         model = DataField
#         fields = ('id', 'munger_builder', 'current_name', 'new_name', 'active_name')
# 
# class PivotFieldSerializer(PartialAllowed):
#     class Meta:
#         model = PivotField
# 
# class FieldTypeSerializer(PartialAllowed):
#     class Meta:
#         model = FieldType
#         fields = '__all__'


# Path: script_builder/api_views.py
from guardian.shortcuts import get_objects_for_user
from .serializers import MungerSerializer, DataFieldSerializer, PivotFieldSerializer, FieldTypeSerializer
from rest_framework.response import Response
from rest_framework import status, filters, mixins, generics, permissions

    def get(self, request, *args, **kwargs):
        if 'pk' in kwargs:
            return self.retrieve(request, *args, **kwargs)
        else:
            return self.list(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        user = request.user
        if self.under_limit(user):
            return self.create(request, *args, **kwargs)
        else:
            error_string = 'Cannot Create more {} - Delete some to make space'.format(
                self.__class__.__name__
            )
            return Response(error_string, status=status.HTTP_403_FORBIDDEN)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)

    def under_limit(self, user):
        meta_name = self.serializer_class.Meta.model._meta.model_name
        permission_name = 'script_builder.change_{}'.format(meta_name)
        current_objects = get_objects_for_user(user, permission_name)
        return len(current_objects) <= self.USER_OBJECT_LIMIT or user.is_superuser

class Mungers(MungerBuilderAPIView):
    USER_OBJECT_LIMIT = 5

    serializer_class = MungerSerializer