Skip to content

Commit 3bddf3d

Browse files
authored
Refactor methods into one for getting checked in tickets (#2113)
1 parent eefc1e4 commit 3bddf3d

3 files changed

Lines changed: 77 additions & 130 deletions

File tree

src/camps/models.py

Lines changed: 28 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -357,132 +357,45 @@ def event_slots(self):
357357
EventSlot = apps.get_model("program", "EventSlot")
358358
return EventSlot.objects.filter(event_session__in=self.event_sessions.all())
359359

360-
@property
361-
def checked_in_full_week_adults(self) -> int:
362-
"""Return the count of full week adult tickets checked in"""
363-
shop_tickets = (
364-
ShopTicket.objects.filter(
365-
ticket_type=self.ticket_type_full_week_adult,
366-
).exclude(used_at=None)
367-
).count()
368-
369-
sponsor_tickets = (
370-
SponsorTicket.objects.filter(
371-
ticket_type=self.ticket_type_full_week_adult,
372-
).exclude(used_at=None)
373-
).count()
374-
375-
prize_tickets = (
376-
PrizeTicket.objects.filter(
377-
ticket_type=self.ticket_type_full_week_adult,
378-
).exclude(used_at=None)
379-
).count()
380-
381-
return shop_tickets + sponsor_tickets + prize_tickets
382-
383-
@property
384-
def checked_in_full_week_children(self) -> int:
385-
"""Return the count of full week children tickets checked in"""
386-
shop_tickets = (
387-
ShopTicket.objects.filter(
388-
ticket_type=self.ticket_type_full_week_child,
389-
).exclude(used_at=None)
390-
).count()
391-
392-
sponsor_tickets = (
393-
SponsorTicket.objects.filter(
394-
ticket_type=self.ticket_type_full_week_child,
395-
).exclude(used_at=None)
396-
).count()
397-
398-
prize_tickets = (
399-
PrizeTicket.objects.filter(
400-
ticket_type=self.ticket_type_full_week_child,
401-
).exclude(used_at=None)
402-
).count()
403-
404-
return shop_tickets + sponsor_tickets + prize_tickets
405-
406-
@property
407-
def checked_in_one_day_adults(self) -> int:
408-
"""Return the count of todays one day adult tickets checked in.
409-
410-
Count tickets with a checked in timestamp from 0600-0600 next day.
411-
Reason being early arriving participants might get checked in before 10.
360+
def checked_in_tickets(self, ticket_type, used_before=None, used_after=None) -> list:
361+
"""Get a concatenated list with all tickets of the specified type
362+
and support for filtering before/after time for when a ticket was used.
412363
"""
413-
now = timezone.localtime()
414-
today_06_hour = now.replace(hour=6, minute=0, second=0)
415-
if now < today_06_hour:
416-
start = today_06_hour - timezone.timedelta(days=1)
417-
end = today_06_hour
418-
else:
419-
start = today_06_hour
420-
end = today_06_hour + timezone.timedelta(days=1)
364+
shop_tickets = ShopTicket.objects.filter(ticket_type=ticket_type).exclude(used_at=None)
365+
sponsor_tickets = SponsorTicket.objects.filter(ticket_type=ticket_type).exclude(used_at=None)
366+
prize_tickets = PrizeTicket.objects.filter(ticket_type=ticket_type).exclude(used_at=None)
421367

422-
shop_tickets = (
423-
ShopTicket.objects.filter(
424-
ticket_type=self.ticket_type_one_day_adult,
425-
).filter(used_at__gte=start, used_at__lt=end)
426-
).count()
368+
if used_before:
369+
shop_tickets = shop_tickets.filter(used_at__lte=used_before)
370+
sponsor_tickets = sponsor_tickets.filter(used_at__lte=used_before)
371+
prize_tickets = prize_tickets.filter(used_at__lte=used_before)
372+
if used_after:
373+
shop_tickets = shop_tickets.filter(used_at__gte=used_after)
374+
sponsor_tickets = sponsor_tickets.filter(used_at__gte=used_after)
375+
prize_tickets = prize_tickets.filter(used_at__gte=used_after)
427376

428-
sponsor_tickets = (
429-
SponsorTicket.objects.filter(
430-
ticket_type=self.ticket_type_one_day_adult,
431-
).filter(used_at__gte=start, used_at__lt=end)
432-
).count()
433-
434-
prize_tickets = (
435-
PrizeTicket.objects.filter(
436-
ticket_type=self.ticket_type_one_day_adult,
437-
).filter(used_at__gte=start, used_at__lt=end)
438-
).count()
439-
440-
return shop_tickets + sponsor_tickets + prize_tickets
377+
return list(shop_tickets) + list(sponsor_tickets) + list(prize_tickets)
441378

442379
@property
443-
def checked_in_one_day_children(self) -> int:
444-
"""Return the count of todays one day children tickets checked in.
380+
def todays_participant_count(self) -> int:
381+
"""Calculate todays participant count from all used 'full week' tickets
382+
and todays used 'one day' tickets.
445383
446384
Count tickets with a checked in timestamp from 0600-0600 next day.
447385
Reason being early arriving participants might get checked in before 10.
448386
"""
449387
now = timezone.localtime()
450-
today_06_hour = now.replace(hour=6, minute=0, second=0)
451-
if now < today_06_hour:
452-
start = today_06_hour - timezone.timedelta(days=1)
453-
end = today_06_hour
388+
limit = now.replace(hour=6, minute=0, second=0)
389+
if now < limit:
390+
used_after = limit - timezone.timedelta(days=1)
391+
used_before = limit
454392
else:
455-
start = today_06_hour
456-
end = today_06_hour + timezone.timedelta(days=1)
457-
458-
shop_tickets = (
459-
ShopTicket.objects.filter(
460-
ticket_type=self.ticket_type_one_day_child,
461-
).filter(used_at__gte=start, used_at__lt=end)
462-
).count()
463-
464-
sponsor_tickets = (
465-
SponsorTicket.objects.filter(
466-
ticket_type=self.ticket_type_one_day_child,
467-
).filter(used_at__gte=start, used_at__lt=end)
468-
).count()
393+
used_after = limit
394+
used_before = limit + timezone.timedelta(days=1)
469395

470-
prize_tickets = (
471-
PrizeTicket.objects.filter(
472-
ticket_type=self.ticket_type_one_day_child,
473-
).filter(used_at__gte=start, used_at__lt=end)
474-
).count()
475-
476-
return shop_tickets + sponsor_tickets + prize_tickets
477-
478-
@property
479-
def participant_count(self) -> int:
480-
"""Retrieve the participant count for all used 'full week' tickets
481-
and todays used 'one day' tickets.
482-
"""
483396
return (
484-
self.checked_in_full_week_adults
485-
+ self.checked_in_full_week_children
486-
+ self.checked_in_one_day_adults
487-
+ self.checked_in_one_day_children
397+
len(self.checked_in_tickets(self.ticket_type_full_week_adult))
398+
+ len(self.checked_in_tickets(self.ticket_type_full_week_child))
399+
+ len(self.checked_in_tickets(self.ticket_type_one_day_adult, used_before, used_after))
400+
+ len(self.checked_in_tickets(self.ticket_type_one_day_child, used_before, used_after))
488401
)

src/camps/tests.py

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,24 @@ def test_checked_in_full_week_adults_all_ticket_types(self) -> None:
6969
self.full_week_adults["sponsor_tickets"][0],
7070
self.full_week_adults["prize_tickets"][0],
7171
]
72+
7273
for ticket in tickets:
7374
ticket.used_at = self.camp.camp.lower
7475
ticket.save()
7576

76-
assert self.camp.checked_in_full_week_adults == 3
77+
result = self.camp.checked_in_tickets(self.camp.ticket_type_full_week_adult)
78+
79+
assert len(result) == len(tickets)
7780

7881
def test_checked_in_full_week_children_shop_tickets(self) -> None:
7982
"""Test the return value of checked in full week children with shop ticket"""
8083
ticket = self.full_week_children[0]
8184
ticket.used_at = self.camp.camp.lower
8285
ticket.save()
83-
assert self.camp.checked_in_full_week_children == 1
86+
87+
result = self.camp.checked_in_tickets(self.camp.ticket_type_full_week_child)
88+
89+
assert len(result) == 1
8490

8591
def test_checked_in_full_week_children_with_sponsor_ticket(self) -> None:
8692
"""Test the return value of checked in full week children with sponsor ticket"""
@@ -90,7 +96,10 @@ def test_checked_in_full_week_children_with_sponsor_ticket(self) -> None:
9096
ticket_type=self.camp.ticket_type_full_week_child,
9197
used_at=self.camp.camp.lower,
9298
)
93-
assert self.camp.checked_in_full_week_children == 1
99+
100+
result = self.camp.checked_in_tickets(self.camp.ticket_type_full_week_child)
101+
102+
assert len(result) == 1
94103

95104
def test_checked_in_full_week_children_with_prize_ticket(self) -> None:
96105
"""Test the return value of checked in full week children with prize ticket"""
@@ -100,15 +109,20 @@ def test_checked_in_full_week_children_with_prize_ticket(self) -> None:
100109
comment="Prize winner",
101110
used_at=self.camp.camp.lower,
102111
)
103-
assert self.camp.checked_in_full_week_children == 1
112+
113+
result = self.camp.checked_in_tickets(self.camp.ticket_type_full_week_child)
114+
115+
assert len(result) == 1
104116

105117
def test_checked_in_one_day_adults(self) -> None:
106118
"""Test the return value of checked in one day adults today"""
107119
for ticket in self.one_day_adults[:2]:
108120
ticket.used_at = timezone.localtime()
109121
ticket.save()
110122

111-
assert self.camp.checked_in_one_day_adults == 2
123+
result = self.camp.checked_in_tickets(self.camp.ticket_type_one_day_adult)
124+
125+
assert len(result) == 2
112126

113127
def test_checked_in_one_day_adults_with_sponsor_ticket(self) -> None:
114128
"""Test the return value of checked in one day adults
@@ -121,7 +135,9 @@ def test_checked_in_one_day_adults_with_sponsor_ticket(self) -> None:
121135
used_at=timezone.localtime(),
122136
)
123137

124-
assert self.camp.checked_in_one_day_adults == 1
138+
result = self.camp.checked_in_tickets(self.camp.ticket_type_one_day_adult)
139+
140+
assert len(result) == 1
125141

126142
def test_checked_in_one_day_adults_with_prize_ticket(self) -> None:
127143
"""Test the return value of checked in one day adults
@@ -134,7 +150,9 @@ def test_checked_in_one_day_adults_with_prize_ticket(self) -> None:
134150
used_at=timezone.localtime(),
135151
)
136152

137-
assert self.camp.checked_in_one_day_adults == 1
153+
result = self.camp.checked_in_tickets(self.camp.ticket_type_one_day_adult)
154+
155+
assert len(result) == 1
138156

139157
def test_checked_in_one_day_adults_timing(self) -> None:
140158
"""Test check in before 06 yesterday don't count"""
@@ -146,15 +164,22 @@ def test_checked_in_one_day_adults_timing(self) -> None:
146164
not_valid.used_at = timezone.localtime() - timezone.timedelta(days=2)
147165
not_valid.save()
148166

149-
assert self.camp.checked_in_one_day_adults == 1
167+
result = self.camp.checked_in_tickets(
168+
self.camp.ticket_type_one_day_adult,
169+
used_after=timezone.localtime().replace(hour=6, minute=0, second=0)
170+
)
171+
172+
assert len(result) == 1
150173

151174
def test_checked_in_one_day_children(self) -> None:
152175
"""Test the return value of checked in one day children today"""
153176
for ticket in self.one_day_children[:2]:
154177
ticket.used_at = timezone.localtime()
155178
ticket.save()
156179

157-
assert self.camp.checked_in_one_day_children == 2
180+
result = self.camp.checked_in_tickets(self.camp.ticket_type_one_day_child)
181+
182+
assert len(result) == 2
158183

159184
def test_checked_in_one_day_children_with_sponsor_ticket(self) -> None:
160185
"""Test the return value of checked in one day children
@@ -167,7 +192,9 @@ def test_checked_in_one_day_children_with_sponsor_ticket(self) -> None:
167192
used_at=timezone.localtime(),
168193
)
169194

170-
assert self.camp.checked_in_one_day_children == 1
195+
result = self.camp.checked_in_tickets(self.camp.ticket_type_one_day_child)
196+
197+
assert len(result) == 1
171198

172199
def test_checked_in_one_day_children_with_prize_ticket(self) -> None:
173200
"""Test the return value of checked in one day children
@@ -180,7 +207,9 @@ def test_checked_in_one_day_children_with_prize_ticket(self) -> None:
180207
used_at=timezone.localtime(),
181208
)
182209

183-
assert self.camp.checked_in_one_day_children == 1
210+
result = self.camp.checked_in_tickets(self.camp.ticket_type_one_day_child)
211+
212+
assert len(result) == 1
184213

185214
def test_checked_in_one_day_children_timing(self) -> None:
186215
"""Test check in before 06 yesterday don't count"""
@@ -192,9 +221,14 @@ def test_checked_in_one_day_children_timing(self) -> None:
192221
not_valid.used_at = timezone.localtime() - timezone.timedelta(days=2)
193222
not_valid.save()
194223

195-
assert self.camp.checked_in_one_day_children == 1
224+
result = self.camp.checked_in_tickets(
225+
self.camp.ticket_type_one_day_child,
226+
used_after=timezone.localtime().replace(hour=6, minute=0, second=0)
227+
)
228+
229+
assert len(result) == 1
196230

197-
def test_participant_count(self) -> None:
231+
def test_todays_participant_count(self) -> None:
198232
"""Test the count of all participants"""
199233
adult_full_week = self.full_week_adults["shop_tickets"][0]
200234
adult_full_week.used_at = self.camp.camp.lower
@@ -212,7 +246,7 @@ def test_participant_count(self) -> None:
212246
child_one_day.used_at = timezone.localtime()
213247
child_one_day.save()
214248

215-
assert self.camp.participant_count == 4
249+
assert self.camp.todays_participant_count == 4
216250

217251
def test_year_of_camp(self) -> None:
218252
"""Test the property `year` return current year of camp."""

src/tokens/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def get_total_players_metrics(self, camp_finds: QuerySet) -> dict:
9999
.last()
100100
)
101101
unique_player_count = camp_finds.distinct("user").count()
102-
non_player_count = self.request.camp.participant_count
102+
non_player_count = self.request.camp.todays_participant_count
103103

104104
if non_player_count: # Avoid ZeroDivisionError
105105
players_pct = unique_player_count / (unique_player_count + non_player_count) * 100

0 commit comments

Comments
 (0)