@@ -0,0 +1,1944 @@
+from django.conf import settings%0Afrom django.core.management import BaseCommand, CommandError%0Afrom django.utils import timezone%0A%0Afrom legalaid.models import Case%0A%0A%0Aclass Command(BaseCommand):%0A    help = %22Recalculate case.assigned_out_of_hours since a given date%22%0A%0A    def handle(self, *args, **options):%0A        try:%0A            date_string = args%5B0%5D%0A        except IndexError:%0A            raise CommandError(%22A start date is required%22)%0A%0A        try:%0A            dt = timezone.datetime.strptime(date_string, %22%25Y-%25m-%25d%22)%0A        except ValueError:%0A            raise CommandError(%22The start date should be a valid datetime in yyyy-mm-dd format%22)%0A%0A        cases = Case.objects.filter(provider_assigned_at__gte=dt)%0A        count = cases.count()%0A%0A        self.stdout.write(%22%7Bcount%7D cases assigned since %7Bdt%7D.%22.format(count=count, dt=dt))%0A        if not count:%0A            return%0A%0A        unchanged = %5B%5D%0A        changed_to_true = %5B%5D%0A        changed_to_false = %5B%5D%0A%0A        for case in cases:%0A            current_value = case.assigned_out_of_hours%0A            new_value = self.recalculate_field(case)%0A            if new_value == current_value:%0A                unchanged.append(case.pk)%0A            elif new_value:%0A                changed_to_true.append(case.pk)%0A            else:%0A                changed_to_false.append(case.pk)%0A%0A        self.stdout.write(%22%7Bcount%7D cases already had the correct value.%22.format(count=len(unchanged)))%0A        self.stdout.write(%22%7Bcount%7D cases will be changed to %60True%60.%22.format(count=len(changed_to_true)))%0A        self.stdout.write(%22%7Bcount%7D cases will be changed to %60False%60.%22.format(count=len(changed_to_false)))%0A%0A    def recalculate_field(self, case):%0A        case_category = getattr(case.eligibility_check.category, %22code%22) if case.eligibility_check else None%0A        non_rota_hours = settings.NON_ROTA_OPENING_HOURS%5Bcase_category%5D%0A        return not non_rota_hours.available(case.provider_assigned_at, tz_aware=True)%0A

Add management command to recalculate the field