Skip to content

Commit

Permalink
... this works locally v3
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLovesDoggo committed Jan 4, 2024
1 parent 78a1519 commit f80e89a
Showing 1 changed file with 28 additions and 44 deletions.
72 changes: 28 additions & 44 deletions core/management/commands/migrate_staff.py
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"))

0 comments on commit f80e89a

Please sign in to comment.