-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
2 changed files
with
68 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() |
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,38 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.core.management.base import BaseCommand | ||
|
||
|
||
User = get_user_model() | ||
|
||
|
||
class Command(BaseCommand): | ||
help = """ | ||
Shows all user information given a pennkey or an email. | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("--pennkey", type=str, help="pennkey") | ||
parser.add_argument("--email", type=str, help="email") | ||
|
||
def handle(self, *args, **kwargs): | ||
if kwargs["pennkey"] is None and kwargs["email"] is None: | ||
self.stdout.write("Please provide a pennkey or an email.") | ||
return | ||
|
||
if kwargs["pennkey"] is not None: | ||
users = User.objects.filter(username=kwargs["pennkey"]) | ||
else: | ||
users = User.objects.filter(email=kwargs["email"]) | ||
|
||
if len(users) == 0: | ||
self.stdout.write("User not found.") | ||
return | ||
if len(users) > 1: | ||
self.stdout.write("Multiple users found? Huh?") | ||
return | ||
|
||
user = users[0] | ||
self.stdout.write(f"User: {user.username}") | ||
self.stdout.write(f"Email: {user.email}") | ||
self.stdout.write(f"First name: {user.first_name}") | ||
self.stdout.write(f"Last name: {user.last_name}") |