diff --git a/boxes/management/commands/list_things.py b/boxes/management/commands/list_things.py new file mode 100644 index 0000000..f85e874 --- /dev/null +++ b/boxes/management/commands/list_things.py @@ -0,0 +1,25 @@ +from django.core.management.base import BaseCommand +from boxes.models import Thing + + +class Command(BaseCommand): + help = 'List all things with their full thing type path and box ID' + + def get_thing_type_path(self, thing_type): + """Get the full path of a thing type with underscores instead of spaces.""" + ancestors = list(thing_type.get_ancestors(include_self=True)) + path_parts = [ancestor.name.replace(' ', '_') for ancestor in ancestors] + return '/'.join(path_parts) + + def handle(self, *args, **options): + things = Thing.objects.select_related('thing_type', 'box').all() + + if not things.exists(): + self.stdout.write(self.style.WARNING('No things found')) + return + + for thing in things: + type_path = self.get_thing_type_path(thing.thing_type) + self.stdout.write(f'{thing.name}: {type_path}, box {thing.box.id}') + + self.stdout.write(self.style.SUCCESS(f'\nTotal: {things.count()} things'))