Skip to content

Serializers

hypha.apply.api.v1.review.serializers

ReviewOpinionReadSerializer

Bases: ModelSerializer

author_id class-attribute instance-attribute

author_id = ReadOnlyField(source='author.id')

opinion class-attribute instance-attribute

opinion = ReadOnlyField(source='get_opinion_display')

Meta

model class-attribute instance-attribute
model = ReviewOpinion
fields class-attribute instance-attribute
fields = ('author_id', 'opinion')

ReviewOpinionWriteSerializer

Bases: ModelSerializer

Meta

model class-attribute instance-attribute
model = ReviewOpinion
fields class-attribute instance-attribute
fields = ('opinion')
extra_kwargs class-attribute instance-attribute
extra_kwargs = {'opinion': {'write_only': True}}

SubmissionReviewSerializer

Bases: ModelSerializer

opinions class-attribute instance-attribute

opinions = ReviewOpinionReadSerializer(read_only=True, many=True)

Meta

model class-attribute instance-attribute
model = Review
fields class-attribute instance-attribute
fields = ['id', 'score', 'is_draft', 'opinions']
extra_kwargs class-attribute instance-attribute
extra_kwargs = {'score': {'read_only': True}, 'is_draft': {'required': False}}

get_recommendation

get_recommendation(obj)
Source code in hypha/apply/api/v1/review/serializers.py
def get_recommendation(self, obj):
    return {
        "value": obj.recommendation,
        "display": obj.get_recommendation_display(),
    }

validate

validate(data)
Source code in hypha/apply/api/v1/review/serializers.py
def validate(self, data):
    validated_data = super().validate(data)
    validated_data["form_data"] = dict(validated_data.items())
    return validated_data

update

update(instance, validated_data)
Source code in hypha/apply/api/v1/review/serializers.py
def update(self, instance, validated_data):
    instance = super().update(instance, validated_data)
    instance.score = self.calculate_score(instance, self.validated_data)
    recommendation = self.validated_data.get(instance.recommendation_field.id, NO)
    instance.recommendation = int(
        recommendation if recommendation is not None else NO
    )
    instance.is_draft = self.validated_data.get("is_draft", False)

    # Old review forms do not have the requred visability field.
    # This will set visibility to PRIVATE by default.
    try:
        instance.visibility = self.validated_data[instance.visibility_field.id]
    except AttributeError:
        instance.visibility = PRIVATE

    if not instance.is_draft:
        # Capture the revision against which the user was reviewing
        instance.revision = instance.submission.live_revision

    instance.save()
    return instance

calculate_score

calculate_score(instance, data)
Source code in hypha/apply/api/v1/review/serializers.py
def calculate_score(self, instance, data):
    scores = []
    for field in instance.score_fields:
        score = data.get(field.id, ["", NA])
        # Include NA answers as 0.
        score = score[1] if score is not None else NA
        if score == NA:
            score = 0
        scores.append(score)
    # Check if there are score_fields_without_text and also
    # append scores from them.
    for field in instance.score_fields_without_text:
        score = data.get(field.id, "")
        # Include '' answers as 0.
        if score is None or score == "":
            score = 0
        scores.append(int(score))

    try:
        return sum(scores) / len(scores)
    except ZeroDivisionError:
        return NA

FieldSerializer

Bases: Serializer

id class-attribute instance-attribute

id = SerializerMethodField()

type class-attribute instance-attribute

type = SerializerMethodField()

kwargs class-attribute instance-attribute

kwargs = SerializerMethodField()

widget class-attribute instance-attribute

widget = SerializerMethodField()

get_id

get_id(obj)
Source code in hypha/apply/api/v1/review/serializers.py
def get_id(self, obj):
    return obj[0]

get_type

get_type(obj)
Source code in hypha/apply/api/v1/review/serializers.py
def get_type(self, obj):
    if isinstance(obj[1], BlockFieldWrapper):
        return "LoadHTML"
    return obj[1].__class__.__name__

get_kwargs

get_kwargs(obj)
Source code in hypha/apply/api/v1/review/serializers.py
def get_kwargs(self, obj):
    return get_field_kwargs(obj[1])

get_widget

get_widget(obj)
Source code in hypha/apply/api/v1/review/serializers.py
def get_widget(self, obj):
    return get_field_widget(obj[1])