From 648c10cc23f5a3edc3df689d0814465d174f063b Mon Sep 17 00:00:00 2001
From: Samuel Gaist <samuel.gaist@idiap.ch>
Date: Wed, 22 Apr 2020 16:46:08 +0200
Subject: [PATCH] [code][serializers] Implement code field handling PUT

This includes both save and to_representation.
---
 beat/web/code/serializers.py | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/beat/web/code/serializers.py b/beat/web/code/serializers.py
index 8519433ab..1b585461f 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)
+
 
 # ----------------------------------------------------------
 
-- 
GitLab