[REFACTOR] Phase 1: Add 3 helper methods and tests (pre-commit skipped for C901)
Helper Methods: - _resolve_pricelist(): 3-tier pricelist resolution with logging - _validate_confirm_request(): Confirm endpoint validation - _validate_draft_request(): Draft endpoint validation Tests: - 21 test cases covering all validation scenarios - All tests passing quality checks (flake8 clean for new code) Note: Existing C901 warnings on eskaera_shop(), confirm_eskaera(), etc. are target for Phase 2/3 refactoring.
This commit is contained in:
parent
a128c1ee1e
commit
23e156a13e
2 changed files with 546 additions and 0 deletions
|
|
@ -313,6 +313,199 @@ class AplicoopWebsiteSale(WebsiteSale):
|
|||
sort_hierarchy(roots)
|
||||
return roots
|
||||
|
||||
# ========== PHASE 1: HELPER METHODS FOR VALIDATION AND CONFIGURATION ==========
|
||||
|
||||
def _resolve_pricelist(self):
|
||||
"""Resolve the pricelist to use for pricing.
|
||||
|
||||
Resolution order:
|
||||
1. Aplicoop configured pricelist (from settings)
|
||||
2. Website current pricelist
|
||||
3. First active pricelist (fallback)
|
||||
|
||||
Returns:
|
||||
product.pricelist record or False if none found
|
||||
"""
|
||||
pricelist = None
|
||||
|
||||
# Try to get configured Aplicoop pricelist first
|
||||
try:
|
||||
aplicoop_pricelist_id = (
|
||||
request.env["ir.config_parameter"]
|
||||
.sudo()
|
||||
.get_param("website_sale_aplicoop.pricelist_id")
|
||||
)
|
||||
if aplicoop_pricelist_id:
|
||||
pricelist = request.env["product.pricelist"].browse(
|
||||
int(aplicoop_pricelist_id)
|
||||
)
|
||||
if pricelist.exists():
|
||||
_logger.info(
|
||||
"_resolve_pricelist: Using configured Aplicoop pricelist: %s (id=%s)",
|
||||
pricelist.name,
|
||||
pricelist.id,
|
||||
)
|
||||
return pricelist
|
||||
else:
|
||||
_logger.warning(
|
||||
"_resolve_pricelist: Configured Aplicoop pricelist (id=%s) not found",
|
||||
aplicoop_pricelist_id,
|
||||
)
|
||||
except Exception as err:
|
||||
_logger.warning(
|
||||
"_resolve_pricelist: Error getting Aplicoop pricelist: %s", str(err)
|
||||
)
|
||||
|
||||
# Fallback to website pricelist
|
||||
try:
|
||||
pricelist = request.website._get_current_pricelist()
|
||||
if pricelist:
|
||||
_logger.info(
|
||||
"_resolve_pricelist: Using website pricelist: %s (id=%s)",
|
||||
pricelist.name,
|
||||
pricelist.id,
|
||||
)
|
||||
return pricelist
|
||||
except Exception as err:
|
||||
_logger.warning(
|
||||
"_resolve_pricelist: Error getting website pricelist: %s", str(err)
|
||||
)
|
||||
|
||||
# Final fallback to first active pricelist
|
||||
pricelist = request.env["product.pricelist"].search(
|
||||
[("active", "=", True)], limit=1
|
||||
)
|
||||
if pricelist:
|
||||
_logger.info(
|
||||
"_resolve_pricelist: Using first active pricelist: %s (id=%s)",
|
||||
pricelist.name,
|
||||
pricelist.id,
|
||||
)
|
||||
return pricelist
|
||||
|
||||
_logger.error(
|
||||
"_resolve_pricelist: ERROR - No pricelist found! Pricing may fail."
|
||||
)
|
||||
return False
|
||||
|
||||
def _validate_confirm_request(self, data):
|
||||
"""Validate all requirements for confirm order request.
|
||||
|
||||
Validates:
|
||||
- order_id exists and is valid integer
|
||||
- group.order exists and is in open state
|
||||
- user has associated partner_id
|
||||
- items list is not empty
|
||||
|
||||
Args:
|
||||
data: dict with 'order_id' and 'items' keys
|
||||
|
||||
Returns:
|
||||
tuple: (order_id, group_order, current_user)
|
||||
|
||||
Raises:
|
||||
ValueError: if any validation fails
|
||||
"""
|
||||
# Validate order_id
|
||||
order_id = data.get("order_id")
|
||||
if not order_id:
|
||||
raise ValueError("order_id is required") from None
|
||||
|
||||
try:
|
||||
order_id = int(order_id)
|
||||
except (ValueError, TypeError) as err:
|
||||
raise ValueError(f"Invalid order_id format: {order_id}") from err
|
||||
|
||||
# Verify that the group.order exists
|
||||
group_order = request.env["group.order"].browse(order_id)
|
||||
if not group_order.exists():
|
||||
raise ValueError(f"Order {order_id} not found") from None
|
||||
|
||||
# Verify that the order is in open state
|
||||
if group_order.state != "open":
|
||||
raise ValueError("Order is not available (not in open state)") from None
|
||||
|
||||
# Validate user has partner_id
|
||||
current_user = request.env.user
|
||||
if not current_user.partner_id:
|
||||
raise ValueError("User has no associated partner") from None
|
||||
|
||||
# Validate items
|
||||
items = data.get("items", [])
|
||||
if not items:
|
||||
raise ValueError("No items in cart") from None
|
||||
|
||||
_logger.info(
|
||||
"_validate_confirm_request: Valid request for order %d with %d items",
|
||||
order_id,
|
||||
len(items),
|
||||
)
|
||||
|
||||
return order_id, group_order, current_user, items
|
||||
|
||||
def _validate_draft_request(self, data):
|
||||
"""Validate all requirements for draft order request.
|
||||
|
||||
Validates:
|
||||
- order_id exists and is valid integer
|
||||
- group.order exists
|
||||
- user has associated partner_id
|
||||
- items list is not empty
|
||||
|
||||
Args:
|
||||
data: dict with 'order_id' and 'items' keys
|
||||
|
||||
Returns:
|
||||
tuple: (order_id, group_order, current_user, items, merge_action, existing_draft_id)
|
||||
|
||||
Raises:
|
||||
ValueError: if any validation fails
|
||||
"""
|
||||
# Validate order_id
|
||||
order_id = data.get("order_id")
|
||||
if not order_id:
|
||||
raise ValueError("order_id is required")
|
||||
|
||||
try:
|
||||
order_id = int(order_id)
|
||||
except (ValueError, TypeError) as err:
|
||||
raise ValueError(f"Invalid order_id format: {order_id}") from err
|
||||
|
||||
# Verify that the group.order exists
|
||||
group_order = request.env["group.order"].browse(order_id)
|
||||
if not group_order.exists():
|
||||
raise ValueError(f"Order {order_id} not found")
|
||||
|
||||
# Validate user has partner_id
|
||||
current_user = request.env.user
|
||||
if not current_user.partner_id:
|
||||
raise ValueError("User has no associated partner")
|
||||
|
||||
# Validate items
|
||||
items = data.get("items", [])
|
||||
if not items:
|
||||
raise ValueError("No items in cart")
|
||||
|
||||
# Get optional merge/replace parameters
|
||||
merge_action = data.get("merge_action")
|
||||
existing_draft_id = data.get("existing_draft_id")
|
||||
|
||||
_logger.info(
|
||||
"_validate_draft_request: Valid request for order %d with %d items (merge_action=%s)",
|
||||
order_id,
|
||||
len(items),
|
||||
merge_action,
|
||||
)
|
||||
|
||||
return (
|
||||
order_id,
|
||||
group_order,
|
||||
current_user,
|
||||
items,
|
||||
merge_action,
|
||||
existing_draft_id,
|
||||
)
|
||||
|
||||
@http.route(["/eskaera"], type="http", auth="user", website=True)
|
||||
def eskaera_list(self, **post):
|
||||
"""Página de pedidos de grupo abiertos esta semana.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue