import re
from rest_framework import serializers

from .models import Collection, CollectionIcon


class CollectionSerializer(serializers.ModelSerializer):

    """Collections's serializer"""

    class Meta:
        model = Collection
        read_only = ('token', )


class CollectionIconSerializer(serializers.ModelSerializer):

    """CollectionIcon's Serializer. """

    class Meta:
        model = CollectionIcon

    def validate_width(self, attrs, source):
        width = attrs[source]
        if width < 1.0:
            raise serializers.ValidationError('Width should be greater than 1.0')
        return attrs

    def validate_name(self, attrs, source):
        name = attrs[source].lower()
        name = re.sub(r'[^a-z0-9\-]', '-', name).strip('-')
        name = re.sub(r'-+', '-', name)
        if name:
            attrs[source] = name
        else:
            raise serializers.ValidationError('Invalid name')
        return attrs

    def validate(self, attrs):
        packicon = attrs.get('packicon')
        svg_d = attrs.get('svg_d')
        width = attrs.get('width')
        if packicon or (svg_d and width): return attrs
        raise serializers.ValidationError(
            'Either a packicon or the shape of icon should be given'
        )

[((32, 15, 32, 39), 're.sub', 're.sub', ({(32, 22, 32, 27): '"""-+"""', (32, 29, 32, 32): '"""-"""', (32, 34, 32, 38): 'name'}, {}), "('-+', '-', name)", False, 'import re\n'), ((44, 14, 46, 9), 'rest_framework.serializers.ValidationError', 'serializers.ValidationError', ({(45, 12, 45, 68): '"""Either a packicon or the shape of icon should be given"""'}, {}), "(\n    'Either a packicon or the shape of icon should be given')", False, 'from rest_framework import serializers\n'), ((26, 18, 26, 81), 'rest_framework.serializers.ValidationError', 'serializers.ValidationError', ({(26, 46, 26, 80): '"""Width should be greater than 1.0"""'}, {}), "('Width should be greater than 1.0')", False, 'from rest_framework import serializers\n'), ((36, 18, 36, 61), 'rest_framework.serializers.ValidationError', 'serializers.ValidationError', ({(36, 46, 36, 60): '"""Invalid name"""'}, {}), "('Invalid name')", False, 'from rest_framework import serializers\n'), ((31, 15, 31, 48), 're.sub', 're.sub', ({(31, 22, 31, 36): '"""[^a-z0-9\\\\-]"""', (31, 38, 31, 41): '"""-"""', (31, 43, 31, 47): 'name'}, {}), "('[^a-z0-9\\\\-]', '-', name)", False, 'import re\n')]