Skip to content
Merged
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
49 changes: 10 additions & 39 deletions src/jobs/publication_plans_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import logging
from typing import Callable, List

from ..focalboard.focalboard_client import FocalboardClient
from ..planka.planka_client import PlankaClient

from ..app_context import AppContext
from ..consts import BoardCardColor, BoardListAlias, TrelloCardColor
from ..consts import BoardListAlias, TrelloCardColor
from ..strings import load
from ..tg.sender import pretty_send
from .base_job import BaseJob
from .utils import check_trello_card, format_errors, format_possibly_plural
from .utils import format_possibly_plural

logger = logging.getLogger(__name__)

Expand All @@ -20,36 +20,29 @@ def _execute(
app_context: AppContext, send: Callable[[str], None], called_from_handler=False
):
validation_errors = {}
errors = {}

paragraphs = [load("publication_plans_job__intro")]

paragraphs += PublicationPlansJob._retrieve_cards_for_paragraph(
focalboard_client=app_context.focalboard_client,
planka_client=app_context.planka_client,
title=load("publication_plans_job__title_publish_this_week"),
list_aliases=(
BoardListAlias.PUBLISH_BACKLOG_9,
BoardListAlias.PUBLISH_IN_PROGRESS_10,
),
errors=errors,
validation_errors=validation_errors,
show_due=True,
need_illustrators=True,
need_date=True,
)

paragraphs += PublicationPlansJob._retrieve_cards_for_paragraph(
focalboard_client=app_context.focalboard_client,
planka_client=app_context.planka_client,
title=load("common_report__section_title_editorial_board"),
list_aliases=(
BoardListAlias.PUBLISH_BACKLOG_9,
BoardListAlias.PUBLISH_IN_PROGRESS_10,
),
errors=errors,
validation_errors=validation_errors,
show_due=False,
need_illustrators=False,
need_date=False,
)

if len(validation_errors) > 0:
Expand All @@ -61,9 +54,6 @@ def _execute(

paragraphs.append(load("publication_plans_job__outro"))

if len(errors) > 0:
paragraphs += format_errors(errors)

pretty_send(paragraphs, send)

@staticmethod
Expand All @@ -73,26 +63,22 @@ def _format_validation_errors(validation_errors: dict) -> str:
]
for card_name, missing_fields in validation_errors.items():
fields_text = ", ".join(missing_fields)

message_lines.append(
f"– В карточке <b>{card_name}</b> не заполнено: {fields_text}"
)
return "\n".join(message_lines)

@staticmethod
def _retrieve_cards_for_paragraph(
focalboard_client: FocalboardClient,
planka_client: PlankaClient,
title: str,
list_aliases: List[BoardListAlias],
errors: dict,
validation_errors: dict,
show_due=True,
need_illustrators=True,
need_date=False,
) -> List[str]:
logger.info(f'Started counting: "{title}"')
list_ids = focalboard_client.get_list_id_from_aliases(list_aliases)
cards = focalboard_client.get_cards(list_ids)
list_ids = planka_client.get_list_id_from_aliases(list_aliases)
cards = planka_client.get_cards(list_ids)
if show_due:
cards.sort(key=lambda card: card.due or datetime.datetime.min)
parse_failure_counter = 0
Expand All @@ -106,12 +92,12 @@ def _retrieve_cards_for_paragraph(
parse_failure_counter += 1
continue

card_fields = focalboard_client.get_custom_fields(card.id)
card_fields = planka_client.get_custom_fields(card.id)
display_name = card_fields.title or card.name
label_names = [
label.name
for label in card.labels
if label.color not in [TrelloCardColor.BLACK, BoardCardColor.BLACK]
if label.color != TrelloCardColor.BLACK
]

is_archive_card = load("common_trello_label__archive") in label_names
Expand Down Expand Up @@ -160,24 +146,9 @@ def _retrieve_cards_for_paragraph(
missing_fields.append("дата")

if missing_fields:
# validation_errors[f"[{error_display_name}]({card.url})"] = (
# missing_fields
# )
validation_errors[f'<a href="{card.url}">{error_display_name}</a>'] = (
missing_fields
)

continue

card_is_ok = check_trello_card(
card,
errors,
is_bad_title=False,
is_bad_illustrators=False,
is_bad_due_date=False,
)

if not card_is_ok:
continue

date = (
Expand Down
11 changes: 11 additions & 0 deletions src/planka/planka_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ def get_cards(self, list_ids=None, board_id=None):
logger.debug(f"get_cards: {cards}")
return cards

def get_labels(self, board_id=None) -> List[objects.TrelloCardLabel]:
board_id = board_id or self.board_id
data = self._get_board_data(board_id)
labels = [
self._label_from_planka_dict(label)
for label in data.get("included", {}).get("labels", [])
if "id" in label
]
logger.debug(f"get_labels: {labels}")
return labels

def get_list_id_from_aliases(self, list_aliases) -> list:
lists = self.get_lists()
result = []
Expand Down
12 changes: 5 additions & 7 deletions src/tg/handlers/flow_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ def handle(self) -> Optional[PlainTextUserAction]:
def _generate_rubric_summary(update, rubric_name: str) -> None:
try:
app_context = AppContext()
fc = app_context.focalboard_client
labels = fc._get_labels()
pc = app_context.planka_client
labels = pc.get_labels()
rubric_label = next(
(
lbl
Expand All @@ -138,9 +138,8 @@ def _generate_rubric_summary(update, rubric_name: str) -> None:
)
return

# Get all lists
try:
lists = fc.get_lists(board_id=fc.board_id, sorted=False)
lists = pc.get_lists()
except Exception as e:
logger.error(
f"_generate_rubric_summary: не удалось получить lists: {e}",
Expand All @@ -161,7 +160,6 @@ def _generate_rubric_summary(update, rubric_name: str) -> None:
for column_name, alias, *meta_flag in SECTIONS:
need_meta = bool(meta_flag and meta_flag[0])
heading = load(alias.value)
# Find column
target_list = next(
(lst for lst in lists if lst.name.strip().startswith(column_name)), None
)
Expand All @@ -172,7 +170,7 @@ def _generate_rubric_summary(update, rubric_name: str) -> None:
continue

try:
cards = fc.get_cards(list_ids=[target_list.id], board_id=fc.board_id)
cards = pc.get_cards(list_ids=[target_list.id])
except Exception:
had_errors = True
message_parts.append(f"<b>{heading}</b> (0)")
Expand Down Expand Up @@ -200,7 +198,7 @@ def _generate_rubric_summary(update, rubric_name: str) -> None:
card.due.strftime("%d.%m.%Y") if card.due else "без срока"
)
try:
fields = fc.get_custom_fields(card.id)
fields = pc.get_custom_fields(card.id)
authors = (
", ".join(fields.authors)
if fields.authors
Expand Down
2 changes: 1 addition & 1 deletion src/tg/handlers/get_rubrics_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_rubrics(update: Update, tg_context: CallbackContext) -> None:

excluded_rubrics = {load(key) for key in EXCLUDED_LOAD_KEYS}
try:
labels = app_context.focalboard_client._get_labels()
labels = app_context.planka_client.get_labels()
filtered = [
lbl.name
for lbl in labels
Expand Down
Loading