Skip to content
Snippets Groups Projects
Commit c3645414 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[common][serializers] Implement handling of return field selection in query parameters for PUT

parent 9358585f
No related branches found
No related tags found
1 merge request!327Refactor update creation api
This commit is part of merge request !327. Comments created here will be created in the context of that merge request.
......@@ -26,6 +26,8 @@
###############################################################################
import copy
import simplejson as json
import difflib
from django.conf import settings
......@@ -44,9 +46,6 @@ from .models import Shareable, Versionable, Contribution
from .exceptions import ContributionCreationError
from .fields import JSONSerializerField, StringListField
import simplejson as json
import difflib
# ----------------------------------------------------------
......@@ -333,6 +332,31 @@ class ContributionModSerializer(serializers.ModelSerializer):
return super().update(instance, validated_data)
def filter_representation(self, representation):
"""Filter out fields if given in query parameters"""
request = self.context["request"]
fields = request.query_params.get("fields", None)
if fields is not None:
fields = fields.split(",")
to_remove = [key for key in representation.keys() if key not in fields]
for key in to_remove:
representation.pop(key)
# Retrieve the description in HTML format
if "html_description" in fields:
description = self.instance.description
if len(description) > 0:
representation["html_description"] = ensure_html(description)
else:
representation["html_description"] = ""
return representation
def to_representation(self, instance):
representation = super().to_representation(instance)
return self.filter_representation(representation)
# ----------------------------------------------------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment