Refactor Assignment model to enforce single rota assignment per worker and update related view logic to prevent multiple assignments
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db.models import Count
|
||||
|
||||
from rota.models import Worker
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "List workers assigned to more than one rota (useful before migrating to OneToOne Assignment)"
|
||||
|
||||
def handle(self, *args, **options):
|
||||
qs = Worker.objects.annotate(num_rotas=Count('rotas')).filter(num_rotas__gt=1)
|
||||
if not qs.exists():
|
||||
self.stdout.write(self.style.SUCCESS('No workers assigned to more than one rota.'))
|
||||
return
|
||||
for w in qs:
|
||||
self.stdout.write(f"Worker id={w.id} name={w.name!r} assigned_rotas={w.num_rotas}")
|
||||
Reference in New Issue
Block a user