Skip to content

Commit

Permalink
upsert application submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
rm03 committed Nov 28, 2023
1 parent 2814025 commit 95ea040
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions backend/clubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4479,8 +4479,11 @@ def question_response(self, *args, **kwargs):
submissions page""",
}
)
submission = ApplicationSubmission.objects.create(
user=self.request.user, application=application, committee=committee,
submission, _ = ApplicationSubmission.objects.update_or_create(
user=self.request.user,
application=application,
committee=committee,
archived=False,
)

key = f"applicationsubmissions:{application.id}"
Expand All @@ -4504,23 +4507,24 @@ def question_response(self, *args, **kwargs):
):
text = question_data.get("text", None)
if text is not None and text != "":
obj = ApplicationQuestionResponse.objects.create(
text=text, question=question, submission=submission,
).save()
obj, _ = ApplicationQuestionResponse.objects.get_or_create(
question=question, submission=submission,
)
obj.text = text
obj.save()
response = Response(ApplicationQuestionResponseSerializer(obj).data)
elif question_type == ApplicationQuestion.MULTIPLE_CHOICE:
multiple_choice_value = question_data.get("multipleChoice", None)
if multiple_choice_value is not None and multiple_choice_value != "":
multiple_choice_obj = ApplicationMultipleChoice.objects.filter(
question=question, value=multiple_choice_value
).first()
obj = ApplicationQuestionResponse.objects.create(
multiple_choice=multiple_choice_obj,
question=question,
submission=submission,
).save()
obj, _ = ApplicationQuestionResponse.objects.get_or_create(
question=question, submission=submission,
)
obj.multiple_choice = multiple_choice_obj
obj.save()
response = Response(ApplicationQuestionResponseSerializer(obj).data)
submission.save()
return response

@action(detail=False, methods=["get"])
Expand Down Expand Up @@ -4588,10 +4592,10 @@ def questions(self, *args, **kwargs):

response = (
ApplicationQuestionResponse.objects.filter(
submission__user=self.request.user, submission__archived=False
question=question,
submission__user=self.request.user,
submission__archived=False,
)
.filter(question__prompt=question.prompt)
.order_by("-updated_at")
.select_related("submission", "multiple_choice", "question")
.prefetch_related("question__committees", "question__multiple_choice")
.first()
Expand Down

0 comments on commit 95ea040

Please sign in to comment.