Skip to content

Commit

Permalink
list_workbaskets - transaction output, handle empty workbaskets.
Browse files Browse the repository at this point in the history
  • Loading branch information
stuaxo committed Jul 1, 2022
1 parent 33d6ca8 commit 34f2bfa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions pii-ner-exclude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1141,3 +1141,4 @@ self.linked_model
WorkBasketOutputFormat
param kwargs:
Enum
WorkBasketOutputFormat Enum
17 changes: 17 additions & 0 deletions workbaskets/management/commands/list_workbaskets.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()

Expand 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"]
Expand Down
15 changes: 13 additions & 2 deletions workbaskets/management/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand All @@ -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):
Expand All @@ -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(
Expand Down

0 comments on commit 34f2bfa

Please sign in to comment.