Skip to content
Snippets Groups Projects
serializers.py 1.14 KiB
Newer Older
  • Learn to ignore specific revisions
  • import collections
    
    
    from rest_framework import serializers
    
    from .models import ApplicationSubmission
    
    
    class SubmissionListSerializer(serializers.ModelSerializer):
        class Meta:
            model = ApplicationSubmission
    
    Tomasz Knapik's avatar
    Tomasz Knapik committed
            fields = ('id', 'title', 'status')
    
    
    
    class SubmissionDetailSerializer(serializers.ModelSerializer):
    
        questions = serializers.SerializerMethodField()
        meta_questions = serializers.SerializerMethodField()
    
    
        class Meta:
            model = ApplicationSubmission
    
            fields = ('id', 'title', 'questions', 'meta_questions')
    
        def get_all_questions(self, obj, filter_func=None):
            for field_id in obj.question_field_ids:
                if filter_func is not None:
    
    Tomasz Knapik's avatar
    Tomasz Knapik committed
                    if not filter_func(field_id):
    
                yield obj.serialize(field_id)
    
    
        def get_meta_questions(self, obj):
            return self.get_all_questions(
                obj,
    
    Tomasz Knapik's avatar
    Tomasz Knapik committed
                filter_func=lambda field_id: field_id in obj.named_blocks
    
            )
    
        def get_questions(self, obj):
            return self.get_all_questions(
                obj,
    
    Tomasz Knapik's avatar
    Tomasz Knapik committed
                filter_func=lambda field_id: field_id not in obj.named_blocks