-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f34b8b
commit b97974e
Showing
5 changed files
with
78 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters