import logging from django.core.management.base import BaseCommand, CommandError from doorcontrol.models import Door from doorcontrol.tasks.update_doors import logger, update_door class Command(BaseCommand): def add_arguments(self, parser): parser.add_argument("door_names", nargs="*") parser.add_argument("--dry-run", action="store_true") def handle( self, *args, door_names: list[str], dry_run: bool, verbosity: int, **options ): verbosity_levels = { 0: logging.ERROR, 1: logging.WARNING, 2: logging.INFO, 3: logging.DEBUG, } logger.setLevel(verbosity_levels.get(verbosity, logging.WARNING)) doors = Door.objects.all() if door_names: doors = doors.filter(name__in=door_names) if len(doors) != len(door_names): raise CommandError("Not all door names matched doors in database") for door in doors: update_door(door, dry_run=dry_run)