diff --git a/beat/web/code/serializers.py b/beat/web/code/serializers.py index 8519433ab1264ab2f2e99a7b7d13db3224325899..1b585461f67bb48bc3f9584bbf6c06afe7ed114d 100755 --- a/beat/web/code/serializers.py +++ b/beat/web/code/serializers.py @@ -66,6 +66,23 @@ class CodeModSerializer(ContributionModSerializer): return super().save(**kwargs) + def filter_representation(self, representation): + def add_code(representation): + # This is inspired from the source of Django REST Framework + field = self._declared_fields["code"] + representation["code"] = field.to_representation(self.instance.source_code) + + request = self.context["request"] + fields = request.query_params.get("fields", None) + if fields is not None: + fields = fields.split(",") + if "code" in fields: + add_code(representation) + else: + add_code(representation) + + return super().filter_representation(representation) + # ----------------------------------------------------------