Skip to content

Commit

Permalink
move to nested fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sravfeyn committed Oct 17, 2024
1 parent dca64e5 commit 97c2f25
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
26 changes: 8 additions & 18 deletions commcare_connect/opportunity/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from functools import lru_cache

from django.conf import settings
from django.db.models import Sum
from rest_framework import serializers
Expand Down Expand Up @@ -94,6 +92,12 @@ class Meta:
fields = ["id", "name", "latitude", "longitude", "radius", "active"]


class OpportunityVerificationFlagsSerializer(serializers.ModelSerializer):
class Meta:
model = OpportunityVerificationFlags
fields = ["form_submission_start", "form_submission_end"]


class OpportunitySerializer(serializers.ModelSerializer):
organization = serializers.SlugRelatedField(read_only=True, slug_field="slug")
learn_app = CommCareAppSerializer()
Expand All @@ -108,8 +112,7 @@ class OpportunitySerializer(serializers.ModelSerializer):
payment_units = serializers.SerializerMethodField()
is_user_suspended = serializers.SerializerMethodField()
catchment_areas = serializers.SerializerMethodField()
start_time_threshold = serializers.SerializerMethodField()
end_time_threshold = serializers.SerializerMethodField()
verification_flags = OpportunityVerificationFlagsSerializer(source="opportunityverificationflags", read_only=True)

class Meta:
model = Opportunity
Expand Down Expand Up @@ -138,8 +141,7 @@ class Meta:
"payment_units",
"is_user_suspended",
"catchment_areas",
"start_time_threshold",
"end_time_threshold",
"verification_flags",
]

def get_claim(self, obj):
Expand Down Expand Up @@ -185,18 +187,6 @@ def get_catchment_areas(self, obj):
catchments = CatchmentArea.objects.filter(opportunity_access=opp_access)
return CatchmentAreaSerializer(catchments, many=True).data

@lru_cache
def _get_flags(self, obj):
return OpportunityVerificationFlags.objects.filter(opportunity=obj).first()

def get_start_time_threshold(self, obj):
flags = self._get_flags(obj)
return flags.form_submission_start

def get_end_time_threshold(self, obj):
flags = self._get_flags(obj)
return flags.form_submission_end


@quickcache(vary_on=["user.pk", "opportunity.pk"], timeout=60 * 60)
def _get_opp_access(user, opportunity):
Expand Down
9 changes: 5 additions & 4 deletions commcare_connect/opportunity/tests/test_api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
OpportunityAccess,
OpportunityClaim,
OpportunityClaimLimit,
OpportunityVerificationFlags,
Payment,
VisitValidationStatus,
)
Expand Down Expand Up @@ -161,9 +160,11 @@ def test_opportunity_list_endpoint(
assert response.data[0]["budget_per_visit"] == max([pu.amount for pu in payment_units])
claim_limits = OpportunityClaimLimit.objects.filter(opportunity_claim__opportunity_access__opportunity=opportunity)
assert response.data[0]["claim"]["max_payments"] == sum([cl.max_visits for cl in claim_limits])
verification_flags = OpportunityVerificationFlags.objects.get(opportunity=opportunity)
assert response.data[0]["start_time_threshold"] == verification_flags.form_submission_start
assert response.data[0]["end_time_threshold"] == verification_flags.form_submission_end
verification_flags = opportunity.opportunityverificationflags
assert response.data[0]["verification_flags"]["form_submission_start"] == str(
verification_flags.form_submission_start
)
assert response.data[0]["verification_flags"]["form_submission_end"] == str(verification_flags.form_submission_end)


def test_delivery_progress_endpoint(
Expand Down

0 comments on commit 97c2f25

Please sign in to comment.