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

Upsert Application Submissions #603

Merged
merged 3 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions backend/clubs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,9 @@ class ApplicationSubmission(models.Model):
def __str__(self):
return f"{self.user.first_name}: {self.application.name}"

class Meta:
unique_together = (("user", "application", "committee"),)


class ApplicationQuestionResponse(models.Model):
"""
Expand Down Expand Up @@ -1723,6 +1726,9 @@ class ApplicationQuestionResponse(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

class Meta:
unique_together = (("question", "submission"),)


class QuestionResponse(models.Model):
"""
Expand Down
131 changes: 21 additions & 110 deletions backend/clubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
TextField,
Value,
)
from django.db.models.expressions import RawSQL
from django.db.models.functions import SHA1, Concat, Lower, Trunc
from django.db.models.query import prefetch_related_objects
from django.http import HttpResponse
Expand Down Expand Up @@ -4479,8 +4478,11 @@ def question_response(self, *args, **kwargs):
submissions page""",
}
)
submission = ApplicationSubmission.objects.create(
user=self.request.user, application=application, committee=committee,
submission, _ = ApplicationSubmission.objects.get_or_create(
user=self.request.user,
application=application,
committee=committee,
archived=False,
)

key = f"applicationsubmissions:{application.id}"
Expand All @@ -4504,23 +4506,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.update_or_create(
question=question,
submission=submission,
defaults={"text": text},
)
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,
obj, _ = ApplicationQuestionResponse.objects.update_or_create(
question=question,
submission=submission,
).save()
defaults={"multiple_choice": multiple_choice_obj},
)
response = Response(ApplicationQuestionResponseSerializer(obj).data)
submission.save()
return response

@action(detail=False, methods=["get"])
Expand Down Expand Up @@ -4588,10 +4591,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 Expand Up @@ -4703,21 +4706,7 @@ def send_emails(self, *args, **kwargs):

# Query for recent submissions with user and committee joined
submissions = ApplicationSubmission.objects.filter(
application=app,
created_at__in=RawSQL(
"""SELECT recent_time
FROM
(SELECT user_id,
committee_id,
application_id,
max(created_at) recent_time
FROM clubs_applicationsubmission
WHERE NOT archived
GROUP BY user_id,
committee_id, application_id) recent_subs""",
(),
),
archived=False,
application=app, archived=False,
).select_related("user", "committee")

dry_run = self.request.data.get("dry_run")
Expand Down Expand Up @@ -4924,21 +4913,7 @@ def get_operation_id(self, **kwargs):
def get_queryset(self):
return (
ApplicationSubmission.objects.filter(
application__is_wharton_council=True,
created_at__in=RawSQL(
"""SELECT recent_time
FROM
(SELECT user_id,
committee_id,
application_id,
max(created_at) recent_time
FROM clubs_applicationsubmission
WHERE NOT archived
GROUP BY user_id,
committee_id, application_id) recent_subs""",
(),
),
archived=False,
application__is_wharton_council=True, archived=False,
)
.annotate(
annotated_name=F("application__name"),
Expand Down Expand Up @@ -4969,30 +4944,9 @@ class ApplicationSubmissionViewSet(viewsets.ModelViewSet):
http_method_names = ["get", "post"]

def get_queryset(self):
# Use a raw SQL query to obtain the most recent (user, committee) pairs
# of application submissions for a specific application.
# Done by grouping by (user_id, commitee_id) and returning the most
# recent instance in each group, then selecting those instances

app_id = self.kwargs["application_pk"]
submissions = (
ApplicationSubmission.objects.filter(
application=app_id,
created_at__in=RawSQL(
"""SELECT recent_time
FROM
(SELECT user_id,
committee_id,
application_id,
max(created_at) recent_time
FROM clubs_applicationsubmission
WHERE NOT archived
GROUP BY user_id,
committee_id, application_id) recent_subs""",
(),
),
archived=False,
)
ApplicationSubmission.objects.filter(application=app_id, archived=False,)
.select_related("user__profile", "committee", "application__club")
.prefetch_related(
Prefetch(
Expand Down Expand Up @@ -5057,23 +5011,7 @@ def export(self, *args, **kwargs):
"""
app_id = int(self.kwargs["application_pk"])
data = (
ApplicationSubmission.objects.filter(
application=app_id,
created_at__in=RawSQL(
"""SELECT recent_time
FROM
(SELECT user_id,
committee_id,
application_id,
max(created_at) recent_time
FROM clubs_applicationsubmission
WHERE NOT archived
GROUP BY user_id,
committee_id, application_id) recent_subs""",
(),
),
archived=False,
)
ApplicationSubmission.objects.filter(application=app_id, archived=False,)
.select_related("user__profile", "committee", "application__club")
.prefetch_related(
Prefetch(
Expand Down Expand Up @@ -5118,19 +5056,6 @@ def exportall(self, *args, **kwargs):
ApplicationSubmission.objects.filter(
application__is_wharton_council=True,
application__application_cycle=cycle,
created_at__in=RawSQL(
"""SELECT recent_time
FROM
(SELECT user_id,
committee_id,
application_id,
max(created_at) recent_time
FROM clubs_applicationsubmission
WHERE NOT archived
GROUP BY user_id,
committee_id, application_id) recent_subs""",
(),
),
archived=False,
)
.select_related("application", "application__application_cycle")
Expand Down Expand Up @@ -5285,21 +5210,7 @@ class ApplicationSubmissionUserViewSet(viewsets.ModelViewSet):
def get_queryset(self):
submissions = (
ApplicationSubmission.objects.filter(
user=self.request.user,
created_at__in=RawSQL(
"""SELECT recent_time
FROM
(SELECT user_id,
committee_id,
application_id,
max(created_at) recent_time
FROM clubs_applicationsubmission
WHERE NOT archived
GROUP BY user_id,
committee_id, application_id) recent_subs""",
(),
),
archived=False,
user=self.request.user, archived=False,
)
.select_related("user__profile", "committee", "application__club")
.prefetch_related(
Expand Down
Loading