Skip to content

Commit

Permalink
remove raw SQL queries + add uniqueness constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
rm03 committed Jan 6, 2024
1 parent 7878693 commit 6e0288e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 98 deletions.
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
103 changes: 5 additions & 98 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 @@ -4707,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 @@ -4928,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 @@ -4973,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 @@ -5061,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 @@ -5122,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 @@ -5289,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

0 comments on commit 6e0288e

Please sign in to comment.