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
19 changes: 18 additions & 1 deletion message_center_compassion/models/gmc_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
import traceback
from datetime import datetime

from psycopg2 import OperationalError

from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.service.model import PG_CONCURRENCY_ERRORS_TO_RETRY

from ..tools.onramp_connector import OnrampConnector

Expand Down Expand Up @@ -196,7 +199,14 @@ def _process_messages(self):
if action.direction == "in":
try:
message_update.update(self._perform_incoming_action())
except Exception:
except Exception as e:
if (
isinstance(e, OperationalError)
and e.pgcode in PG_CONCURRENCY_ERRORS_TO_RETRY
):
# Retryable concurrency error: propagate so the queue job
# is retried instead of writing in the failed transaction.
raise
# Abort pending operations
logger.error("Failure when processing message", exc_info=True)
self.env.cr.rollback()
Expand Down Expand Up @@ -410,6 +420,13 @@ def _process_single_answer(self, data_object, answer_data):
f(answer_data)
self.state = "success"
except Exception as e:
if (
isinstance(e, OperationalError)
and e.pgcode in PG_CONCURRENCY_ERRORS_TO_RETRY
):
# Retryable concurrency error: the transaction is aborted and
# cannot be written to; propagate so the queue job is retried.
raise
logger.error(traceback.format_exc())
try:
if action.failure_method:
Expand Down
11 changes: 8 additions & 3 deletions partner_communication_compassion/models/correspondence.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,19 @@ def _can_auto_send(self):
require manual validation before.
"""
self.ensure_one()
partner_langs = self.supporter_languages_ids
types = self.communication_type_ids.mapped("name")
valid = (
self.sponsorship_id.state == "active"
and "Final Letter" not in types
and "auto" in self.partner_id.letter_delivery_preference
)
if not (partner_langs & self.beneficiary_language_ids):
valid &= self.translation_language_id in partner_langs
# Only auto-send if the letter's actual content language is one the
# sponsor reads (detected from content, not the field-office stamp).
detected_lang = self._detect_letter_language()
valid = (
valid
and bool(detected_lang)
and detected_lang in self.supporter_languages_ids
)

return valid
12 changes: 12 additions & 0 deletions sbc_compassion/models/correspondence.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,18 @@ def _check_translation_language(self):
):
letter.original_language_id = detected_lang

def _detect_letter_language(self):
"""Language the letter is actually written in. Detected from its text."""
self.ensure_one()
text = self.translated_text or self.english_text or self.original_text or ""
clean = (
text.strip(" \t\n\r.")
.replace(BOX_SEPARATOR, "")
.replace(PAGE_SEPARATOR, "")
.strip()
)
return self.env["langdetect"].detect_language(clean)

@api.depends("uuid")
def _compute_read_url(self):
for letter in self:
Expand Down
9 changes: 6 additions & 3 deletions sbc_translation/models/correspondence.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,13 @@ def process_letter(self):
if not force_publish:
letter._check_translation_language()

# Can sponser read the letter?
# Can sponser read the letter? Decide from the letter's ACTUAL
# content language, not the field-office TranslationLanguage stamp.
# If it can't be determined, fail safe to translation.
detected_lang = letter._detect_letter_language()
langs_match = (
letter.beneficiary_language_ids & letter.supporter_languages_ids
) or letter.translation_language_id in letter.supporter_languages_ids
bool(detected_lang) and detected_lang in letter.supporter_languages_ids
)

# Is the letter still in the translation process?
translation_hold = (
Expand Down
Loading