Skip to content

Commit

Permalink
feat: command to track individual booking usage
Browse files Browse the repository at this point in the history
  • Loading branch information
tuneerroy committed Nov 5, 2023
1 parent 9bc0a2f commit c3c32a9
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions backend/gsr_booking/management/commands/individual_usage.py
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()

0 comments on commit c3c32a9

Please sign in to comment.