Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
julianweng committed Feb 3, 2024
1 parent 1f34b8b commit b97974e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM pennlabs/django-base:a142aa6975ee293bbc8a09ef0b81998ce7063dd3
FROM pennlabs/django-base:281d2f6446bb5381e1a2ecf39f6689b648d92ee6-3.8.5

LABEL maintainer="Penn Labs"

Expand Down
36 changes: 36 additions & 0 deletions backend/clubs/management/commands/update_club_counts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.core.management.base import BaseCommand
from django.db.models import Count, Q

from clubs.models import Club


class Command(BaseCommand):
help = "Update stored favorite and membership counts."

def handle(self, *args, **kwargs):
try:
queryset = Club.objects.all().annotate(
temp_favorite_count=Count("favorite", distinct=True),
temp_membership_count=Count(
"membership", distinct=True, filter=Q(active=True)
),
)

for club in queryset:
club.favorite_count = club.temp_favorite_count
club.membership_count = club.temp_membership_count
Club.objects.bulk_update(queryset, ["favorite_count", "membership_count"])

self.stdout.write(
self.style.SUCCESS(
"Successfully updated all club favorite and membership counts!"
)
)
except Exception as e:
self.stdout.write(
self.style.ERROR(
"An error was encountered while updating"
+ "club favorite and membership counts!"
)
)
self.stdout.write(e)
33 changes: 33 additions & 0 deletions backend/clubs/migrations/0095_auto_20240202_2013.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 3.2.23 on 2024-02-03 01:13

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("clubs", "0094_applicationcycle_release_date"),
]

operations = [
migrations.AddField(
model_name="club",
name="favorite_count",
field=models.IntegerField(default=0),
),
migrations.AddField(
model_name="club",
name="membership_count",
field=models.IntegerField(default=0),
),
migrations.AddField(
model_name="historicalclub",
name="favorite_count",
field=models.IntegerField(default=0),
),
migrations.AddField(
model_name="historicalclub",
name="membership_count",
field=models.IntegerField(default=0),
),
]
7 changes: 7 additions & 0 deletions backend/clubs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,13 @@ class Club(models.Model):
appointment_needed = models.BooleanField(default=False)
signature_events = models.TextField(blank=True) # html

# cache club aggregation counts
favorite_count = models.IntegerField(default=0)
membership_count = models.IntegerField(default=0)

# cache club rankings
rank = models.IntegerField(default=0)

# cache club rankings
rank = models.IntegerField(default=0)

Expand Down
8 changes: 1 addition & 7 deletions backend/clubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,13 +1014,7 @@ class ClubViewSet(XLSXFormatterMixin, viewsets.ModelViewSet):
"""

queryset = (
Club.objects.all()
.annotate(
favorite_count=Count("favorite", distinct=True),
membership_count=Count("membership", distinct=True, filter=Q(active=True)),
)
.prefetch_related("tags")
.order_by("-favorite_count", "name")
Club.objects.all().prefetch_related("tags").order_by("-favorite_count", "name")
)
permission_classes = [ClubPermission | IsSuperuser]
filter_backends = [filters.SearchFilter, ClubsSearchFilter, ClubsOrderingFilter]
Expand Down

0 comments on commit b97974e

Please sign in to comment.