Skip to content

Commit

Permalink
ebill_postfinance: reduce payload size saved in the db
Browse files Browse the repository at this point in the history
  • Loading branch information
TDu committed Jan 9, 2024
1 parent 8d57dbd commit 68de205
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions ebill_postfinance/models/ebill_postfinance_invoice_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ class EbillPostfinanceInvoiceMessage(models.Model):
ref = fields.Char("Reference No.", size=35)
ebill_account_number = fields.Char("Payer Id", size=20)
payload = fields.Text("Payload sent")
payload_size = fields.Float(
"Payload Size (MB)", digits=(6, 3), compute="_compute_payload_size"
)
payload_size = fields.Float("Payload Size (MB)", digits=(6, 3), readonly=True)
response = fields.Text()
payment_type = fields.Selection(
selection=[
Expand All @@ -90,13 +88,12 @@ class EbillPostfinanceInvoiceMessage(models.Model):
readonly=True,
)

@api.depends("payload")
def _compute_payload_size(self):
for message in self:
size_in_bytes = len(message.payload)
if size_in_bytes > 0:
size_in_bytes = size_in_bytes / 1000000
message.payload_size = size_in_bytes
@api.model
def _get_payload_size(self, payload):
size_in_bytes = len(payload)

Check warning on line 93 in ebill_postfinance/models/ebill_postfinance_invoice_message.py

View check run for this annotation

Codecov / codecov/patch

ebill_postfinance/models/ebill_postfinance_invoice_message.py#L93

Added line #L93 was not covered by tests
if size_in_bytes > 0:
size_in_bytes = size_in_bytes / 1000000
return size_in_bytes

Check warning on line 96 in ebill_postfinance/models/ebill_postfinance_invoice_message.py

View check run for this annotation

Codecov / codecov/patch

ebill_postfinance/models/ebill_postfinance_invoice_message.py#L95-L96

Added lines #L95 - L96 were not covered by tests

def set_transaction_id(self):
self.ensure_one()
Expand Down Expand Up @@ -144,15 +141,32 @@ def set_as_paid(self, data):
record.state = "done"
record.invoice_id.message_post(body=_("Invoice paid through eBilling"))

Check warning on line 142 in ebill_postfinance/models/ebill_postfinance_invoice_message.py

View check run for this annotation

Codecov / codecov/patch

ebill_postfinance/models/ebill_postfinance_invoice_message.py#L141-L142

Added lines #L141 - L142 were not covered by tests

@api.model
def _remove_pdf_data_from_payload(self, data):
"""Minimize payload size to be kept.
Remove the node containing the pdf data from the xml.
"""
start_node = "<Appendix>"
end_node = "</Appendix>"
start = data.find(start_node)

Check warning on line 153 in ebill_postfinance/models/ebill_postfinance_invoice_message.py

View check run for this annotation

Codecov / codecov/patch

ebill_postfinance/models/ebill_postfinance_invoice_message.py#L151-L153

Added lines #L151 - L153 were not covered by tests
if start < 0:
return data
end = data.find(end_node, start)
return data[0:start] + data[end + len(end_node) :]

Check warning on line 157 in ebill_postfinance/models/ebill_postfinance_invoice_message.py

View check run for this annotation

Codecov / codecov/patch

ebill_postfinance/models/ebill_postfinance_invoice_message.py#L155-L157

Added lines #L155 - L157 were not covered by tests

def send_to_postfinance(self):
# TODO: Could sent multiple with one call
for message in self:
message.file_type_used = message.service_id.file_type_to_use
message.set_transaction_id()
message.payload = message._generate_payload()
payload = message._generate_payload()
data = payload.encode("utf-8")
message.payload = self._remove_pdf_data_from_payload(payload)
message.payload_size = self._get_payload_size(payload)
try:

Check warning on line 168 in ebill_postfinance/models/ebill_postfinance_invoice_message.py

View check run for this annotation

Codecov / codecov/patch

ebill_postfinance/models/ebill_postfinance_invoice_message.py#L162-L168

Added lines #L162 - L168 were not covered by tests
# TODO: Handle file type from service configuation
data = message.payload.encode("utf-8")
res = message.service_id.upload_file(

Check warning on line 170 in ebill_postfinance/models/ebill_postfinance_invoice_message.py

View check run for this annotation

Codecov / codecov/patch

ebill_postfinance/models/ebill_postfinance_invoice_message.py#L170

Added line #L170 was not covered by tests
message.transaction_id, message.file_type_used, data
)
Expand Down

0 comments on commit 68de205

Please sign in to comment.