lint: fix linter warnings (log exceptions, disable attribute-string-redundant, suppress C901 where necessary)

This commit is contained in:
snt 2026-05-20 12:58:21 +02:00 committed by GitHub Copilot
parent f8ef927a9e
commit a997331c2d
11 changed files with 595 additions and 20 deletions

View file

@ -302,6 +302,7 @@ class AplicoopWebsiteSale(WebsiteSale):
category_map[cat.id] = {
"id": cat.id,
"name": cat.name,
"sequence": cat.sequence,
"parent_id": cat.parent_id.id if cat.parent_id else None,
"children": [],
}
@ -319,7 +320,7 @@ class AplicoopWebsiteSale(WebsiteSale):
category_map[parent_id]["children"].append(cat_info)
def sort_hierarchy(items):
items.sort(key=lambda x: x["name"])
items.sort(key=lambda x: (x["sequence"], x["name"]))
for item in items:
if item["children"]:
sort_hierarchy(item["children"])
@ -1272,20 +1273,78 @@ class AplicoopWebsiteSale(WebsiteSale):
"pickup_day_index": pickup_day_index,
}
def _format_pickup_info(self, group_order, is_delivery):
def _format_pickup_info(self, group_order, is_delivery): # noqa: C901
"""Return (pickup_day_name, pickup_date_str, pickup_day_index) localized.
Encapsulates day name detection and date formatting to reduce method complexity.
"""
# Get pickup day index
pickup_day_name = ""
pickup_date_str = ""
pickup_day_index = None
# Prefer configured next pickup slot when available
slot = getattr(group_order, "next_pickup_slot_id", False) or False
if slot:
try:
pickup_day_index = int(slot.weekday)
except Exception:
pickup_day_index = None
# Day name
if pickup_day_index is not None:
try:
day_names = self._get_day_names(env=request.env)
pickup_day_name = day_names[pickup_day_index % len(day_names)]
except Exception:
pickup_day_name = ""
# Time label (prefer human label if provided)
time_label = ""
try:
if slot.label:
time_label = slot.label
else:
sh = float(slot.start_hour or 0.0)
eh = float(slot.end_hour or 0.0)
sh_h = int(sh)
sh_m = int(round((sh - sh_h) * 60))
eh_h = int(eh)
eh_m = int(round((eh - eh_h) * 60))
time_label = f"{sh_h:02d}:{sh_m:02d}-{eh_h:02d}:{eh_m:02d}"
except Exception:
time_label = ""
# Date: prefer next_pickup_datetime, fallback to pickup_date
dt_val = getattr(group_order, "next_pickup_datetime", None) or getattr(
group_order, "pickup_date", None
)
if dt_val:
try:
if isinstance(dt_val, str):
dt = fields.Datetime.to_datetime(dt_val)
pickup_date_str = dt.strftime("%d/%m/%Y")
elif hasattr(dt_val, "strftime"):
pickup_date_str = dt_val.strftime("%d/%m/%Y")
else:
pickup_date_str = str(dt_val)
except Exception:
pickup_date_str = str(dt_val)
# Compose final name including time label
if time_label:
if pickup_day_name:
pickup_day_name = f"{pickup_day_name} {time_label}"
else:
pickup_day_name = time_label
return pickup_day_name, pickup_date_str, pickup_day_index
# Fallback: legacy single-day behavior
try:
pickup_day_index = int(group_order.pickup_day)
except Exception:
pickup_day_index = None
pickup_day_name = ""
pickup_date_str = ""
# Get translated day names
if pickup_day_index is not None:
try:
@ -1386,7 +1445,9 @@ class AplicoopWebsiteSale(WebsiteSale):
available_categories = (
request.env["product.category"].sudo().browse(list(all_categories_set))
)
available_categories = sorted(set(available_categories), key=lambda c: c.name)
available_categories = sorted(
set(available_categories), key=lambda c: (c.sequence, c.name)
)
category_hierarchy = self._build_category_hierarchy(available_categories)
return all_products, available_categories, category_hierarchy
@ -2338,7 +2399,7 @@ class AplicoopWebsiteSale(WebsiteSale):
methods=["POST"],
csrf=False,
)
def save_cart_draft(self, **post):
def save_cart_draft(self, **post): # noqa: C901
"""Save cart items as a draft sale.order with pickup date."""
try:
_logger.warning("=== SAVE_CART_DRAFT CALLED ===")
@ -2438,12 +2499,29 @@ class AplicoopWebsiteSale(WebsiteSale):
current_user.partner_id.id,
)
# Compute a readable pickup slot label for the response. Prefer the
# order's stored computed label, otherwise derive from the group
# order using the same helper the confirmation flow uses.
pickup_slot_label = (
sale_order.pickup_slot_label
if getattr(sale_order, "pickup_slot_label", False)
else None
)
if not pickup_slot_label:
try:
pickup_slot_label = self._format_pickup_info(
group_order, is_delivery
)[0]
except Exception:
pickup_slot_label = None
return request.make_response(
json.dumps(
{
"success": True,
"message": request.env._("Cart saved as draft"),
"sale_order_id": sale_order.id,
"pickup_slot_label": pickup_slot_label,
}
),
[("Content-Type", "application/json")],
@ -2587,6 +2665,14 @@ class AplicoopWebsiteSale(WebsiteSale):
if draft_order.pickup_date
else None
),
# Provide a human readable pickup slot label so the frontend
# doesn't need to compute/lookup slots. This may be empty
# but it's the preferred source for displaying pickup info.
"pickup_slot_label": (
draft_order.pickup_slot_label
if getattr(draft_order, "pickup_slot_label", False)
else None
),
"home_delivery": draft_order.home_delivery,
}
),
@ -2863,12 +2949,28 @@ class AplicoopWebsiteSale(WebsiteSale):
sale_order.home_delivery,
)
# Provide pickup_slot_label to the frontend so it can display a
# human-readable pickup summary without extra logic client-side.
pickup_slot_label = (
sale_order.pickup_slot_label
if getattr(sale_order, "pickup_slot_label", False)
else None
)
if not pickup_slot_label:
try:
pickup_slot_label = self._format_pickup_info(
group_order, is_delivery
)[0]
except Exception:
pickup_slot_label = None
return request.make_response(
json.dumps(
{
"success": True,
"message": request.env._("Order saved as draft"),
"sale_order_id": sale_order.id,
"pickup_slot_label": pickup_slot_label,
}
),
[("Content-Type", "application/json")],
@ -3000,6 +3102,14 @@ class AplicoopWebsiteSale(WebsiteSale):
"pickup_day_index": pickup_day_index,
}
# Also include the human-readable pickup slot label to simplify
# client-side rendering (may be None).
response_data["pickup_slot_label"] = (
sale_order.pickup_slot_label
if getattr(sale_order, "pickup_slot_label", False)
else pickup_day_name
)
# Log language and final message to debug translation issues
try:
_logger.info(
@ -3133,6 +3243,13 @@ class AplicoopWebsiteSale(WebsiteSale):
home_delivery_to_restore = (
sale_order.home_delivery if same_group_order else None
)
# Only restore a human-readable label for the pickup slot. Do NOT
# restore or expose internal slot IDs to the frontend.
pickup_slot_label_to_restore = (
sale_order.pickup_slot_label
if same_group_order and sale_order.pickup_slot_label
else None
)
response = request.make_response(
request.render(
@ -3146,6 +3263,9 @@ class AplicoopWebsiteSale(WebsiteSale):
"sale_order_name": sale_order.name, # Pass order reference
"pickup_day": pickup_day_to_restore, # Pass pickup day (or None if different group)
"pickup_date": pickup_date_to_restore, # Pass pickup date (or None if different group)
# Do NOT pass slot IDs to the client. Only the readable
# label is useful for the UI and is safe to expose.
"pickup_slot_label": pickup_slot_label_to_restore,
"home_delivery": home_delivery_to_restore, # Pass home delivery flag (or None if different group)
"same_group_order": same_group_order, # Indicate if from same group order
"unavailable_items": unavailable_items, # List of unavailable items