diff --git a/pii-ner-exclude.txt b/pii-ner-exclude.txt index ee637fdfba..48224d583b 100644 --- a/pii-ner-exclude.txt +++ b/pii-ner-exclude.txt @@ -1141,3 +1141,4 @@ self.linked_model WorkBasketOutputFormat param kwargs: Enum +WorkBasketOutputFormat Enum diff --git a/workbaskets/management/commands/list_workbaskets.py b/workbaskets/management/commands/list_workbaskets.py index 75dbaf3057..8364309b46 100644 --- a/workbaskets/management/commands/list_workbaskets.py +++ b/workbaskets/management/commands/list_workbaskets.py @@ -1,6 +1,8 @@ +import ast import logging from typing import Any from typing import Optional +from typing import Sequence from django.core.management import BaseCommand from django.core.management.base import CommandParser @@ -53,6 +55,13 @@ def add_arguments(self, parser: CommandParser) -> None: help="Output first / last transactions.", ) + parser.add_argument( + "workbasket_ids", + help=("Comma-separated list of workbasket ids to filter to"), + nargs="?", + type=ast.literal_eval, + ) + def handle(self, *args: Any, **options: Any) -> Optional[str]: workbaskets = WorkBasket.objects.order_by("updated_at").all() @@ -66,6 +75,14 @@ def handle(self, *args: Any, **options: Any) -> Optional[str]: if workbasket_statuses: workbaskets = workbaskets.filter(status__in=options["status"]) + if options.get("workbasket_ids"): + # Filter by id + ids = options["workbasket_ids"] + if isinstance(ids, int): + workbaskets = workbaskets.filter(pk=ids) + elif isinstance(ids, Sequence): + workbaskets = workbaskets.filter(pk__in=ids) + output_format = ( WorkBasketOutputFormat.COMPACT if options["compact"] diff --git a/workbaskets/management/util.py b/workbaskets/management/util.py index 3c6be99c34..f1af68f78e 100644 --- a/workbaskets/management/util.py +++ b/workbaskets/management/util.py @@ -12,6 +12,16 @@ def first_line_of(s: str) -> str: class WorkBasketCommandMixin: + def get_transaction_span(self, transactions): + """ + Return a string representing the span from first to last transaction. + + Hyphens are output if there are no transactions. + """ + first_tx = transactions.first().pk if transactions else None + last_tx = transactions.last().pk if transactions else None + return (f", {first_tx or '-'} -'" + f"' {last_tx or '-'}",) + def _output_workbasket_readable( self, workbasket, show_transaction_info, indent=4, **kwargs ): @@ -23,7 +33,8 @@ def _output_workbasket_readable( self.stdout.write(f"{spaces}status: {workbasket.status}") if show_transaction_info: self.stdout.write( - f"{spaces}transactions: {workbasket.transactions.first().pk} - {workbasket.transactions.last().pk}", + f"{spaces}transactions: " + + self.get_transaction_span(workbasket.transactions.all()), ) def _output_workbasket_compact(self, workbasket, show_transaction_info, **kwargs): @@ -33,7 +44,7 @@ def _output_workbasket_compact(self, workbasket, show_transaction_info, **kwargs ) if show_transaction_info: self.stdout.write( - f", {workbasket.transactions.first().pk} - {workbasket.transactions.last().pk}", + self.get_transaction_span(workbasket.transactions.all()), ) def output_workbasket(