Skip to content

Commit 45a4978

Browse files
committed
removed active Manager in lieu of adding an active method to the objects
1 parent 68d326f commit 45a4978

File tree

8 files changed

+29
-29
lines changed

8 files changed

+29
-29
lines changed

Justfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ upload:
119119
# Run linter and code formatter tools
120120
lint:
121121
@echo Linting...
122-
-{{BIN}}/flake8 picker tests demo
122+
-{{BIN}}/ruff check picker tests demo
123123

124124
@echo Format checks...
125-
-{{BIN}}/black --check --diff -l 100 picker tests demo
125+
-{{BIN}}/ruff format --diff --line-length 100 picker tests demo
126126

127127
# Build the demo Docker container
128128
docker-build *args='':

picker/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "2.3.0"
1+
__version__ = "2.4.0"
22
VERSION = tuple(int(i) for i in __version__.split("."))
33

44
default_app_config = "picker.apps.PickerConfig"

picker/models/picks.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ def should_autopick(self):
4747
return self.autopick != self.Autopick.NONE
4848

4949

50-
class ActiveStatusManager(models.Manager):
51-
def get_queryset(self):
52-
return super().get_queryset().filter(status=self.model.Status.ACTIVE)
50+
class PickerGroupingManager(models.Manager):
51+
def active(self):
52+
return self.filter(
53+
status=self.model.Status.ACTIVE, leagues__is_active=True
54+
).prefetch_related("leagues", "members")
5355

5456

5557
class PickerGrouping(models.Model):
@@ -67,8 +69,7 @@ class Status(models.TextChoices):
6769
status = models.CharField(max_length=4, choices=Status.choices, default=Status.ACTIVE)
6870
category = models.CharField(max_length=3, choices=Category.choices, default=Category.PRIVATE)
6971

70-
objects = models.Manager()
71-
active = ActiveStatusManager()
72+
objects = PickerGroupingManager()
7273

7374
def __str__(self):
7475
return self.name
@@ -89,15 +90,17 @@ def save(self, *args, **kws):
8990
return super().save(*args, **kws)
9091

9192

92-
class ActiveMembershipManager(models.Manager):
93-
def get_queryset(self):
93+
class MembershipManager(models.Manager):
94+
def active(self, **kwargs):
9495
return (
95-
super()
96-
.get_queryset()
97-
.filter(status=self.model.Status.ACTIVE, group__status=PickerGrouping.Status.ACTIVE)
96+
self.filter(
97+
status=self.model.Status.ACTIVE,
98+
group__status=PickerGrouping.Status.ACTIVE,
99+
**kwargs,
100+
)
98101
.select_related("group")
99102
.prefetch_related(
100-
models.Prefetch("group__leagues", queryset=sports.League.active.all())
103+
models.Prefetch("group__leagues", queryset=sports.League.objects.active())
101104
)
102105
)
103106

@@ -106,7 +109,7 @@ def for_user(self, user, league=None):
106109
if league:
107110
kwargs["group__leagues"] = league
108111

109-
return self.filter(**kwargs)
112+
return self.active(**kwargs)
110113

111114

112115
class PickerMembership(models.Model):
@@ -129,8 +132,7 @@ class Status(models.TextChoices):
129132
status = models.CharField(max_length=4, choices=Status.choices, default=Status.ACTIVE)
130133
autopick = models.CharField(max_length=4, choices=Autopick.choices, default=Autopick.RANDOM)
131134

132-
objects = models.Manager()
133-
active = ActiveMembershipManager()
135+
objects = MembershipManager()
134136

135137
def __str__(self):
136138
return f"{self.user}@{self.group}"

picker/models/sports.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def temp_slug():
3737
return "{:10.0f}".format(random.random() * 10000000000)
3838

3939

40-
class ActiveManager(models.Manager):
41-
def get_queryset(self):
42-
return super().get_queryset().filter(is_active=True)
40+
class LeagueManager(models.Manager):
41+
def active(self):
42+
return self.filter(is_active=True)
4343

4444

4545
class League(models.Model):
@@ -51,8 +51,7 @@ class League(models.Model):
5151
avg_game_duration = models.PositiveIntegerField(default=240)
5252
is_active = models.BooleanField(default=True)
5353

54-
objects = models.Manager()
55-
active = ActiveManager()
54+
objects = LeagueManager()
5655

5756
class Meta:
5857
permissions = (("can_update_score", "Can update scores"),)
@@ -426,7 +425,7 @@ def import_games(self, data, teams=None):
426425
@cached_property
427426
def next_gameset(self):
428427
try:
429-
return self.league.gamesets.get(season=self.season, sequence = self.sequence + 1)
428+
return self.league.gamesets.get(season=self.season, sequence=self.sequence + 1)
430429
except GameSet.DoesNotExist:
431430
return None
432431

picker/templatetags/picker_tags.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from django.template import Library
2-
from django.core.exceptions import ObjectDoesNotExist
32

43
from ..models import PickerFavorite
54
from ..utils import get_templates

picker/views/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ def season(self):
5454

5555
@cached_property
5656
def leagues(self):
57-
return League.active.all()
57+
return League.objects.active()
5858

5959
@cached_property
6060
def league(self):
61-
return get_object_or_404(League.active, slug=self.kwargs["league"])
61+
return get_object_or_404(League.objects.active(), slug=self.kwargs["league"])
6262

6363
def get_template_names(self, template_override=None):
6464
if template_override is None and self.template_name is None:
@@ -114,4 +114,4 @@ def get_context_data(self, **kwargs):
114114

115115
@cached_property
116116
def memberships(self):
117-
return list(PickerMembership.active.for_user(self.request.user, league=self.league))
117+
return list(PickerMembership.objects.for_user(self.request.user, league=self.league))

picker/views/picks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def get_context_data(self, **kwargs):
179179
def show_picks(self, gameset, **kwargs):
180180
return self.render_to_response(
181181
self.get_context_data(picks=gameset.pick_for_user(self.request.user), **kwargs),
182-
template_override="@picks/show.html"
182+
template_override="@picks/show.html",
183183
)
184184

185185
def post(self, request, *args, **kwargs):

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ requires = ["setuptools>=61.0.0"]
33
build-backend = "setuptools.build_meta"
44

55
[project]
6-
name = "django-picker"
6+
name = "picker"
77
dynamic = ["version"]
88
description = "A Django sports picker app"
99
license = { file = "LICENSE" }

0 commit comments

Comments
 (0)