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

API to Link an EngagementQuestionnaire's unanswered Answered_Survey to an Engagement #11226

Merged
merged 9 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions dojo/api_v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3057,6 +3057,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
2 changes: 2 additions & 0 deletions dojo/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="engagement_survey")
v2_api.register(r"questionnaire_general_questionnaires", QuestionnaireGeneralSurveyViewSet, basename="general_survey")
v2_api.register(r"questionnaire_questions", QuestionnaireQuestionViewSet, basename="question")
v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="link_engagement")

ur = []
ur += dev_env_urls
ur += endpoint_urls
Expand Down
18 changes: 18 additions & 0 deletions unittests/test_rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -3003,6 +3003,24 @@ def __init__(self, *args, **kwargs):
BaseClass.RESTEndpointTest.__init__(self, *args, **kwargs)


class QuestionnaireEngagementQuestionnairesLinkEngagementTest(BaseClass.RESTEndpointTest):
fixtures = ["questionnaire_testdata.json"]

def __init__(self, *args, **kwargs):
self.endpoint_model = Answered_Survey
self.endpoint_path = "questionnaire_engagement_questionnaires"
self.viewname = "link_engagement"
self.viewset = QuestionnaireAnsweredSurveyViewSet
self.test_type = TestType.STANDARD
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 AnnouncementTest(BaseClass.BaseClassTest):
fixtures = ["dojo_testdata.json"]

Expand Down
Loading