<commit_before>from django.contrib.auth.models import User, Group
from rest_framework import serializers, exceptions
from rest_framework.reverse import reverse
from media_service.models import MediaStore, Course, Collection, CollectionResource, Resource
from media_service.mediastore import MediaStoreUpload
import json

def resource_to_representation(resource):
    return resource.get_representation()

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'id', 'username', 'email', 'groups')

class CollectionResourceSerializer(serializers.HyperlinkedModelSerializer):
    collection_id = serializers.PrimaryKeyRelatedField(queryset=Collection.objects.all())
    collection_url = serializers.HyperlinkedIdentityField(view_name="collection-detail", lookup_field="pk")
    course_image_id = serializers.PrimaryKeyRelatedField(queryset=Resource.objects.all(), source='resource_id')

    class Meta:
        model = CollectionResource
        fields = ('id', 'collection_url', 'collection_id', 'course_image_id', 'sort_order', 'created', 'updated')

    def __init__(self, *args, **kwargs):
        super(CollectionResourceSerializer, self).__init__(*args, **kwargs)

    def create(self, validated_data):
        collection_resource = CollectionResource(
            collection=validated_data['collection_id'],
            resource=validated_data['resource_id'],
        )
        collection_resource.save()
        return collection_resource

    def to_representation(self, instance):
        data =  super(CollectionResourceSerializer, self).to_representation(instance)
        resource = instance.resource
        data.update({
            "type": 'collectionimages',
            "url": reverse('collectionimages-detail', kwargs={'pk': instance.pk}, request=self.context['request']),
            "course_image_id": resource.id,
            "title": resource.title,
            "description": resource.description,
            "upload_file_name": resource.upload_file_name,
            "is_upload": resource.is_upload,
        })
        data.update(resource_to_representation(resource))
        return data

class CollectionResourceIdsField(serializers.Field):
    def to_representation(self, obj):
        course_image_ids = [item.resource_id for item in obj.resources.all()]
        return course_image_ids

    def to_internal_value(self, data):
        valid_resource_ids = Resource.objects.filter(pk__in=data).distinct().values_list('pk', flat=True)
        diff = list(set(data).difference(valid_resource_ids))
        if len(diff) != 0:
            raise serializers.ValidationError("Invalid course_image_ids for course %s. Invalid: %s Valid: %s." % (course_pk, diff, valid_resource_ids))
        return data

    def get_attribute(self, obj):
        return obj

class CollectionSerializer(serializers.HyperlinkedModelSerializer):
    course_id = serializers.PrimaryKeyRelatedField(queryset=Course.objects.all())
    images_url = serializers.HyperlinkedIdentityField(view_name="collectionimages-list", lookup_field="pk")
    description = serializers.CharField(max_length=None, required=False)
    course_image_ids = CollectionResourceIdsField(read_only=False, required=False)

    class Meta:
        model = Collection
        fields = ('url', 'id', 'title', 'description', 'sort_order', 'course_id', 'course_image_ids', 'images_url', 'created', 'updated')

    def __init__(self, *args, **kwargs):
        include = kwargs.pop('include', [])
        super(CollectionSerializer, self).__init__(*args, **kwargs)
        if 'images' in include:
            self.fields['images'] = CollectionResourceSerializer(source="resources", many=True, read_only=True)

    def create(self, validated_data):
        collection = Collection(
            title=validated_data['title'],
            course=validated_data['course_id'],
            description=validated_data.get('description', ''),
        )<commit_msg>making sure collections can have blank descriptions<commit_after>from django.contrib.auth.models import User, Group
from rest_framework import serializers, exceptions
from rest_framework.reverse import reverse
from media_service.models import MediaStore, Course, Collection, CollectionResource, Resource
from media_service.mediastore import MediaStoreUpload
import json

def resource_to_representation(resource):
    return resource.get_representation()

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'id', 'username', 'email', 'groups')

class CollectionResourceSerializer(serializers.HyperlinkedModelSerializer):
    collection_id = serializers.PrimaryKeyRelatedField(queryset=Collection.objects.all())
    collection_url = serializers.HyperlinkedIdentityField(view_name="collection-detail", lookup_field="pk")
    course_image_id = serializers.PrimaryKeyRelatedField(queryset=Resource.objects.all(), source='resource_id')

    class Meta:
        model = CollectionResource
        fields = ('id', 'collection_url', 'collection_id', 'course_image_id', 'sort_order', 'created', 'updated')

    def __init__(self, *args, **kwargs):
        super(CollectionResourceSerializer, self).__init__(*args, **kwargs)

    def create(self, validated_data):
        collection_resource = CollectionResource(
            collection=validated_data['collection_id'],
            resource=validated_data['resource_id'],
        )
        collection_resource.save()
        return collection_resource

    def to_representation(self, instance):
        data =  super(CollectionResourceSerializer, self).to_representation(instance)
        resource = instance.resource
        data.update({
            "type": 'collectionimages',
            "url": reverse('collectionimages-detail', kwargs={'pk': instance.pk}, request=self.context['request']),
            "course_image_id": resource.id,
            "title": resource.title,
            "description": resource.description,
            "upload_file_name": resource.upload_file_name,
            "is_upload": resource.is_upload,
        })
        data.update(resource_to_representation(resource))
        return data

class CollectionResourceIdsField(serializers.Field):
    def to_representation(self, obj):
        course_image_ids = [item.resource_id for item in obj.resources.all()]
        return course_image_ids

    def to_internal_value(self, data):
        valid_resource_ids = Resource.objects.filter(pk__in=data).distinct().values_list('pk', flat=True)
        diff = list(set(data).difference(valid_resource_ids))
        if len(diff) != 0:
            raise serializers.ValidationError("Invalid course_image_ids for course %s. Invalid: %s Valid: %s." % (course_pk, diff, valid_resource_ids))
        return data

    def get_attribute(self, obj):
        return obj

class CollectionSerializer(serializers.HyperlinkedModelSerializer):
    course_id = serializers.PrimaryKeyRelatedField(queryset=Course.objects.all())
    images_url = serializers.HyperlinkedIdentityField(view_name="collectionimages-list", lookup_field="pk")
    description = serializers.CharField(max_length=None, required=False, allow_blank=True)
    course_image_ids = CollectionResourceIdsField(read_only=False, required=False)

    class Meta:
        model = Collection
        fields = ('url', 'id', 'title', 'description', 'sort_order', 'course_id', 'course_image_ids', 'images_url', 'created', 'updated')

    def __init__(self, *args, **kwargs):
        include = kwargs.pop('include', [])
        super(CollectionSerializer, self).__init__(*args, **kwargs)
        if 'images' in include:
            self.fields['images'] = CollectionResourceSerializer(source="resources", many=True, read_only=True)

    def create(self, validated_data):
        collection = Collection(
            title=validated_data['title'],
            course=validated_data['course_id'],
            description=validated_data.get('description', ''),
        )
{'fraction_non_alphanumeric': 0.11008717310087172, 'fraction_numerical': 0.00024906600249066, 'mean_word_length': 4.108778625954199, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3074728', 'n_tokens_mistral': 2133, 'n_tokens_neox': 2083, 'n_words': 425}