<s> from __future__ import unicode_literals <EOL> from rest_framework import serializers <EOL> from rest_framework . exceptions import ValidationError <EOL> from rest_framework . reverse import reverse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from documents . models import Document <EOL> from documents . serializers import DocumentSerializer <EOL> from user_management . serializers import UserSerializer <EOL> from . models import Folder <EOL> class FolderSerializer ( serializers . HyperlinkedModelSerializer ) : <EOL> documents = serializers . HyperlinkedIdentityField ( <EOL> view_name = '<STR_LIT>' <EOL> ) <EOL> documents_count = serializers . SerializerMethodField ( ) <EOL> user = UserSerializer ( read_only = True ) <EOL> class Meta : <EOL> extra_kwargs = { <EOL> '<STR_LIT:url>' : { '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT:user>' : { '<STR_LIT>' : '<STR_LIT>' } <EOL> } <EOL> fields = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT:label>' , <EOL> '<STR_LIT:url>' , '<STR_LIT:user>' <EOL> ) <EOL> model = Folder <EOL> def get_documents_count ( self , obj ) : <EOL> return obj . documents . count ( ) <EOL> class NewFolderSerializer ( serializers . Serializer ) : <EOL> label = serializers . CharField ( ) <EOL> def create ( self , validated_data ) : <EOL> try : <EOL> data = validated_data . copy ( ) <EOL> data . update ( { '<STR_LIT:user>' : self . context [ '<STR_LIT>' ] . user } ) <EOL> return Folder . objects . create ( ** data ) <EOL> except Exception as exception : <EOL> raise ValidationError ( exception ) <EOL> class FolderDocumentSerializer ( DocumentSerializer ) : <EOL> remove = serializers . SerializerMethodField ( ) <EOL> def get_remove ( self , instance ) : <EOL> return reverse ( <EOL> '<STR_LIT>' , args = ( <EOL> self . context [ '<STR_LIT>' ] . pk , instance . pk <EOL> ) , request = self . context [ '<STR_LIT>' ] , format = self . context [ '<STR_LIT>' ] <EOL> ) <EOL> class Meta ( DocumentSerializer . Meta ) : <EOL> fields = DocumentSerializer . Meta . fields + ( '<STR_LIT>' , ) <EOL> read_only_fields = DocumentSerializer . Meta . fields <EOL> class NewFolderDocumentSerializer ( serializers . Serializer ) : <EOL> document = serializers . IntegerField ( <EOL> help_text = _ ( '<STR_LIT>' ) <EOL> ) <EOL> def create ( self , validated_data ) : <EOL> try : <EOL> document = Document . objects . get ( pk = validated_data [ '<STR_LIT>' ] ) <EOL> validated_data [ '<STR_LIT>' ] . documents . add ( document ) <EOL> except Exception as exception : <EOL> raise ValidationError ( exception ) <EOL> return { '<STR_LIT>' : document . pk } </s>
