-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(growth): prompts activities endpoint #28008
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import calendar | ||
|
|
||
| from django.db import IntegrityError, transaction | ||
| from django.db.models import Q | ||
| from django.http import HttpResponse | ||
| from django.utils import timezone | ||
| from rest_framework import serializers | ||
|
|
@@ -15,6 +16,7 @@ | |
| VALID_STATUSES = frozenset(("snoozed", "dismissed")) | ||
|
|
||
|
|
||
| # Endpoint to retrieve multiple PromptsActivity at once | ||
| class PromptsActivitySerializer(serializers.Serializer): | ||
| feature = serializers.CharField(required=True) | ||
| status = serializers.ChoiceField(choices=zip(VALID_STATUSES, VALID_STATUSES), required=True) | ||
|
|
@@ -33,24 +35,31 @@ class PromptsActivityEndpoint(Endpoint): | |
| def get(self, request): | ||
| """Return feature prompt status if dismissed or in snoozed period""" | ||
|
|
||
| feature = request.GET.get("feature") | ||
|
|
||
| if not prompt_config.has(feature): | ||
| return Response({"detail": "Invalid feature name"}, status=400) | ||
|
|
||
| required_fields = prompt_config.required_fields(feature) | ||
| for field in required_fields: | ||
| if field not in request.GET: | ||
| return Response({"detail": 'Missing required field "%s"' % field}, status=400) | ||
|
|
||
| filters = {k: request.GET.get(k) for k in required_fields} | ||
|
|
||
| try: | ||
| result = PromptsActivity.objects.get(user=request.user, feature=feature, **filters) | ||
| except PromptsActivity.DoesNotExist: | ||
| return Response({}) | ||
|
|
||
| return Response({"data": result.data}) | ||
| features = request.GET.getlist("feature") | ||
| if len(features) == 0: | ||
| return Response({"details": "No feature specified"}, status=400) | ||
|
|
||
| conditions = None | ||
| for feature in features: | ||
| if not prompt_config.has(feature): | ||
| return Response({"detail": "Invalid feature name " + feature}, status=400) | ||
|
|
||
| required_fields = prompt_config.required_fields(feature) | ||
| for field in required_fields: | ||
| if field not in request.GET: | ||
| return Response({"detail": 'Missing required field "%s"' % field}, status=400) | ||
| filters = {k: request.GET.get(k) for k in required_fields} | ||
| condition = Q(feature=feature, **filters) | ||
| conditions = condition if conditions is None else (conditions | condition) | ||
|
|
||
| result = PromptsActivity.objects.filter(conditions, user=request.user) | ||
| featuredata = {k.feature: k.data for k in result} | ||
| if len(features) == 1: | ||
| result = result.first() | ||
| data = None if result is None else result.data | ||
| return Response({"data": data, "features": featuredata}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Neo-Zhixing excellent idea also setting |
||
| else: | ||
| return Response({"features": featuredata}) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to maintain backward compatibility with a bunch of tests that mocks the endpoint with |
||
|
|
||
| def put(self, request): | ||
| serializer = PromptsActivitySerializer(data=request.data) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Neo-Zhixing Nicely done using
getlisthere :)