Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list_workbaskets - transaction output, handle empty workbaskets. #615

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pii-ner-exclude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1141,3 +1141,5 @@ self.linked_model
WorkBasketOutputFormat
param kwargs:
Enum
WorkBasketOutputFormat Enum
f"{first_tx
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 '-'} - {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),
)

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),
)

def output_workbasket(
Expand Down