Skip to content

Commit

Permalink
Merge PR #35 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by max3903
  • Loading branch information
OCA-git-bot committed Oct 11, 2023
2 parents 40710e7 + e04808c commit 9f5f417
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
20 changes: 11 additions & 9 deletions l10n_mx_res_partner_csf/tests/test_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,10 @@ def setUp(self):
}
)

def test_import_cfs(self):
action = self.partner.action_upload_csf()
def test_import_csf_pdf_file(self):
generated_file = os.path.join("l10n_mx_res_partner_csf", "tests", "demo.pdf")
generated_file = tools.misc.file_open(generated_file, "rb")
data = base64.encodebytes(generated_file.read())
record_csf = (
self.env[action.get("res_model")]
.with_context(active_id=self.partner)
.create({"file": data, "file_name": "demo.txt"})
)
with self.assertRaises(UserError):
record_csf.upload_csf()

record1 = self.import_obj.with_context(active_id=self.partner.id).create(
{"file": data, "file_name": "demo.pdf"}
Expand All @@ -45,3 +37,13 @@ def test_import_cfs(self):
self.assertEqual(" JURICA", self.partner.street2)
self.assertEqual("QUERETARO", self.partner.city)
self.assertEqual(self.country_id.id, self.partner.country_id.id)

def test_import_csf_txt_file(self):
action = self.partner.action_upload_csf()
record_csf = (
self.env[action.get("res_model")]
.with_context(active_id=self.partner.id)
.create({"file": "fake_data", "file_name": "demo.txt"})
)
with self.assertRaises(UserError):
record_csf.upload_csf()
11 changes: 6 additions & 5 deletions l10n_mx_res_partner_csf/wizard/import_csf.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ def attach_csf(self):

def upload_csf(self):
partner_obj = self.env["res.partner"]
if self.file_name.split(".")[-1] != "pdf":
raise UserError(_("Upload file is not in PDF format"))
temp_path = tempfile.gettempdir()
file_data = base64.decodebytes(self.file)
fp = open(temp_path + "/csf.pdf", "wb+")
fp.write(file_data)
fp.close()
text = extract_text(temp_path + "/csf.pdf")
try:
text = extract_text(temp_path + "/csf.pdf")
except Exception as e:
raise UserError(_("Uploaded file is not in PDF format (%s).") % e) from e
vals = self.prepare_res_partner_values(text)
partner_obj.browse(self._context.get("active_id")).write(vals)
self.attach_csf()
Expand Down Expand Up @@ -75,7 +76,7 @@ def prepare_res_partner_values(self, text):

country_id = self.env.ref("base.mx")
state_id = state_obj.search(
[("country_id", "=", country_id.id), ("name", "ilike", state)]
[("country_id", "=", country_id.id), ("name", "ilike", state)], limit=1
)

return {
Expand All @@ -85,6 +86,6 @@ def prepare_res_partner_values(self, text):
"city": city,
"street": street,
"street2": street2,
"state_id": state_id.id,
"state_id": state_id.id or False,
"country_id": country_id.id,
}

0 comments on commit 9f5f417

Please sign in to comment.