-
Notifications
You must be signed in to change notification settings - Fork 7
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
1 parent
78a1519
commit f80e89a
Showing
1 changed file
with
28 additions
and
44 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,50 +1,34 @@ | ||
from django.core.management.base import BaseCommand | ||
from django.db import IntegrityError | ||
from django.conf import settings | ||
from core.models import ( | ||
StaffMember, | ||
User, | ||
) # Update 'your_app' with the actual name of your app | ||
from core.models import (StaffMember, User, ) # Update 'your_app' with the actual name of your app | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Populate staff members based on METROPOLIS_STAFFS and bio settings." | ||
|
||
def handle(self, *args, **options): | ||
self.stdout.write(self.style.INFO(StaffMember.objects.all().count())) | ||
try: | ||
for position, user_ids in settings.METROPOLIS_STAFFS.items(): | ||
for user_id in user_ids: | ||
try: | ||
user = User.objects.get(pk=user_id) | ||
except User.DoesNotExist: | ||
self.stdout.write( | ||
self.style.ERROR(f"User {user_id} does not exist") | ||
) | ||
continue | ||
try: | ||
bio = settings.METROPOLIS_STAFF_BIO.get(user_id, "") | ||
if not bio: | ||
print( | ||
f"Bio for user {user.username} ID:{user.id} is empty" | ||
) | ||
staff_member, created = StaffMember.objects.get_or_create( | ||
user=user, bio=bio | ||
) | ||
staff_member.positions = list(staff_member.positions) + [ | ||
position | ||
] | ||
staff_member.save() | ||
except IntegrityError as e: | ||
self.stdout.write( | ||
self.style.WARNING( | ||
f"StaffMember for user {user_id} already exists: " + str(e) | ||
) | ||
) | ||
|
||
except AttributeError: | ||
self.stdout.write( | ||
self.style.SUCCESS("METROPOLIS_STAFFS does not exist anymore") | ||
) | ||
|
||
self.stdout.write(self.style.SUCCESS("Command completed successfully")) | ||
help = "Populate staff members based on METROPOLIS_STAFFS and bio settings." | ||
|
||
def handle(self, *args, **options): | ||
self.stdout.write(self.style.INFO(StaffMember.objects.all().count())) | ||
try: | ||
for position, user_ids in settings.METROPOLIS_STAFFS.items(): | ||
for user_id in user_ids: | ||
try: | ||
user = User.objects.get(pk=user_id) | ||
except User.DoesNotExist: | ||
self.stdout.write(self.style.ERROR(f"User {user_id} does not exist")) | ||
continue | ||
try: | ||
bio = settings.METROPOLIS_STAFF_BIO.get(user_id, "") | ||
if not bio: | ||
print(f"Bio for user {user.username} ID:{user.id} is empty") | ||
staff_member, created = StaffMember.objects.get_or_create(user=user, bio=bio) | ||
staff_member.positions = list(staff_member.positions) + [position] | ||
staff_member.save() | ||
except IntegrityError as e: | ||
self.stdout.write(f"StaffMember for user {user_id} already exists: " + str(e) | ||
) | ||
|
||
except AttributeError: | ||
self.stdout.write(self.style.SUCCESS("METROPOLIS_STAFFS does not exist anymore")) | ||
|
||
self.stdout.write(self.style.SUCCESS("Command completed successfully")) |