Skip to content

Commit

Permalink
API to Link an EngagementQuestionnaire's unanswered Answered_Survey t…
Browse files Browse the repository at this point in the history
…o an Engagement (#11226)

* still not there

* Link 'Answered' Survey to Engagement

* initial test item

* remove the registration

* fix ruff

* re-add url and change base

* Add an actual test

* Remove extra URL entry

* Delete extra test suite

---------

Co-authored-by: Cody Maffucci <[email protected]>
  • Loading branch information
hblankenship and Maffooch authored Nov 15, 2024
1 parent 5d85275 commit 27f49b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
23 changes: 23 additions & 0 deletions dojo/api_v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3087,6 +3087,29 @@ class QuestionnaireEngagementSurveyViewSet(
def get_queryset(self):
return Engagement_Survey.objects.all().order_by("id")

@extend_schema(
request=OpenApiTypes.NONE,
parameters=[
OpenApiParameter(
"engagement_id", OpenApiTypes.INT, OpenApiParameter.PATH,
),
],
responses={status.HTTP_200_OK: serializers.QuestionnaireAnsweredSurveySerializer},
)
@action(
detail=True, methods=["post"], url_path=r"link_engagement/(?P<engagement_id>\d+)",
)
def link_engagement(self, request, pk, engagement_id):
# Get the answered survey
engagement_survey = self.get_object()
# Safely get the engagement
engagement = get_object_or_404(Engagement.objects, pk=engagement_id)
# Link the engagement
answered_survey, _ = Answered_Survey.objects.get_or_create(engagement=engagement, survey=engagement_survey)
# Send a favorable response
serialized_answered_survey = serializers.QuestionnaireAnsweredSurveySerializer(answered_survey)
return Response(serialized_answered_survey.data)


@extend_schema_view(**schema_with_prefetch())
class QuestionnaireAnsweredSurveyViewSet(
Expand Down
5 changes: 5 additions & 0 deletions unittests/test_rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -2989,6 +2989,11 @@ def __init__(self, *args, **kwargs):
self.deleted_objects = 5
BaseClass.RESTEndpointTest.__init__(self, *args, **kwargs)

def test_link_engagement_questionnaire(self):
end_url = self.url + "4/link_engagement/2/"
result = self.client.post(end_url)
self.assertEqual(result.status_code, status.HTTP_200_OK, f"Failed to link enagement survey to engagement: {result.content} on {end_url}")


class AnsweredSurveyTest(BaseClass.BaseClassTest):
fixtures = ["questionnaire_testdata.json"]
Expand Down

0 comments on commit 27f49b4

Please sign in to comment.