Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added modifications to polls and stories API #851

Merged
merged 10 commits into from
Jan 12, 2022
51 changes: 51 additions & 0 deletions ureport/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,32 @@ def get_images(self, obj):
for image in obj.get_featured_images()
]

# Function to use ?fields and ?exclude API calls for specific attributes in stories
# this is for which we can exclude any field
def __init__(self, *args, **kwargs):
# Don't pass the 'fields' arg up to the superclass
request = kwargs.get('context', {}).get('request')
str_fields = request.GET.get('exclude', '') if request else None
str_exclude_fields = request.GET.get('fields', '') if request else None
alviriseup marked this conversation as resolved.
Show resolved Hide resolved
exclude_fields = str_exclude_fields.split(',') if str_exclude_fields else None
fields = str_fields.split(',') if str_fields else None

# Instantiate the superclass normally
super(StoryReadSerializer, self).__init__(*args, **kwargs)

if fields is not None:
# Drop any fields that are not specified in the `fields` argument.
allowed = set(fields)
existing = set(self.fields)
for field_name in allowed:
self.fields.pop(field_name)
elif exclude_fields is not None:
allowed_fields = set(exclude_fields)
existing_data = set(self.fields)
for field_names in existing_data - allowed_fields:
self.fields.pop(field_names)


class Meta:
model = Story
fields = (
Expand All @@ -122,6 +148,31 @@ class PollReadSerializer(serializers.ModelSerializer):
category = CategoryReadSerializer()
questions = SerializerMethodField()

# Function to use ?fields and ?exclude API calls for specific attributes in polls
def __init__(self, *args, **kwargs):
# Don't pass the 'fields' arg up to the superclass
request = kwargs.get('context', {}).get('request')
str_fields = request.GET.get('exclude', '') if request else None
str_exclude_fields = request.GET.get('fields', '') if request else None
exclude_fields = str_exclude_fields.split(',') if str_exclude_fields else None
fields = str_fields.split(',') if str_fields else None

# Instantiate the superclass normally
super(PollReadSerializer, self).__init__(*args, **kwargs)

if fields is not None:
# Drop any fields that are not specified in the `fields` argument.
allowed = set(fields)
print('allowed :',allowed)
existing = set(self.fields)
for field_name in allowed:
self.fields.pop(field_name)
elif exclude_fields is not None:
allowed_fields = set(exclude_fields)
existing_data = set(self.fields)
for field_names in existing_data - allowed_fields:
self.fields.pop(field_names)

def get_questions(self, obj):
questions = []
for question in obj.get_questions():
Expand Down
99 changes: 99 additions & 0 deletions ureport/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,26 @@ class PollList(BaseListAPIView):
...
]
}


If you want to get polls with only specific attributes:

Example:

GET /api/v1/polls/org/{org}/?fields=title,flow_uuid

Response is polls with only title and flow_uuid attributes.


If you want to get polls without specific attributes:

Example:

GET /api/v1/polls/org/{org}/?exclude=title,flow_uuid

Response is polls without title and flow_uuid attributes.


"""

serializer_class = PollReadSerializer
Expand Down Expand Up @@ -395,6 +415,26 @@ class PollDetails(RetrieveAPIView):
},
"created_on": "2015-09-02T08:53:30.313251Z"
}


If you want to get a poll with only specific attributes:

Example:

GET /api/v1/polls/{id}/?fields=title,flow_uuid

Response is a poll with only title and flow_uuid attributes.


If you want to get a poll without specific attributes:

Example:

GET /api/v1/polls/{id}/?exclude=title,flow_uuid

Response is a poll without title and flow_uuid attributes.


"""

serializer_class = PollReadSerializer
Expand Down Expand Up @@ -532,6 +572,26 @@ class FeaturedPollList(BaseListAPIView):
...
]
}


If you want to get the featured poll with only specific attributes:

Example:

GET /api/v1/polls/org/{org}/featured/?fields=title,flow_uuid

Response is the featured poll with only title and flow_uuid attributes.


If you want to get the featured poll without specific attributes:

Example:

GET /api/v1/polls/org/{org}/featured/?exclude=title,flow_uuid

Response is the featured poll without title and flow_uuid attributes.


"""

serializer_class = PollReadSerializer
Expand Down Expand Up @@ -829,6 +889,26 @@ class StoryList(BaseListAPIView):
},
...
}


If you want to get stories with only specific attributes:

Example:

GET /api/v1/stories/org/{org}/?fields=title,content

Response is stories with only title and content attributes.


If you want to get stories without specific attributes:

Example:

GET /api/v1/stories/org/{org}/?exclude=title,content

Response is stories without title and content attributes.


"""

serializer_class = StoryReadSerializer
Expand Down Expand Up @@ -866,6 +946,25 @@ class StoryDetails(RetrieveAPIView):
"name": "tests"
}
}

If you want to get a story with only specific attributes:

Example:

GET /api/v1/stories/{id}/?fields=title,content

Response is a story with only title and content attributes.


If you want to get a story without specific attributes:

Example:

GET /api/v1/stories/{id}/?exclude=title,content

Response is a story without title and content attributes.


"""

serializer_class = StoryReadSerializer
Expand Down