-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: command to track individual booking usage
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
backend/gsr_booking/management/commands/individual_usage.py
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,30 @@ | ||
from django.core.management.base import BaseCommand | ||
from django.utils import timezone | ||
|
||
from gsr_booking.models import Group, GSRBooking | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Provides usage stats for a given user." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("pennkey", type=str, help="Pennkey of user to check") | ||
|
||
def handle(self, *args, **kwargs): | ||
pennkey = kwargs["pennkey"] | ||
groups = Group.objects.filter(memberships__user__username=pennkey) | ||
bookings = GSRBooking.objects.filter( | ||
reservation__creator__username=pennkey, | ||
reservation__is_cancelled=False, | ||
reservation__end__lte=timezone.now(), | ||
is_cancelled=False, | ||
) | ||
|
||
for group in groups: | ||
print(f'Usage for group "{group.name}":') | ||
group_bookings = bookings.filter(reservation__group=group) | ||
total_time = sum( | ||
(booking.end - booking.start).total_seconds() / 60 for booking in group_bookings | ||
) | ||
print(f"Total Credits Used: {int(total_time)}") | ||
print() |