-
Notifications
You must be signed in to change notification settings - Fork 51
T2840 add category and description code snippet #2070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 14.0
Are you sure you want to change the base?
Changes from all commits
a8394a3
bf2189c
4ca569e
97ff839
d6eeb0f
052c330
724fe4b
24cba07
1f537b1
4333862
a5959fb
62875a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,6 +80,22 @@ class CorrespondenceS2bGenerator(models.Model): | |
| preview_pdf = fields.Binary(readonly=True) | ||
| filename = fields.Char(compute="_compute_filename") | ||
| month = fields.Selection("_get_months") | ||
| generation_status = fields.Selection( | ||
| [ | ||
| ("creating_task", "creating_task"), | ||
| ("apply_template", "apply_template"), | ||
| ("apply_text", "apply_text"), | ||
| ("apply_images", "apply_images"), | ||
| ("generate_pdf", "generate_pdf"), | ||
| ("done", "done"), | ||
| ("failed", "failed"), | ||
| ("finalizing", "finalizing"), | ||
| ], | ||
| default="creating_task", | ||
| string="Generation Status", | ||
| ) | ||
| generation_error_message = fields.Text(string="Generation Message") | ||
| MAX_PAGE_COUNT = 15 # Maximum number of pages allowed in a letter | ||
|
|
||
| def _compute_nb_letters(self): | ||
| for generator in self: | ||
|
|
@@ -130,50 +146,63 @@ def onchange_month(self): | |
|
|
||
| def preview(self): | ||
| """Generate a picture for preview.""" | ||
| pdf = self._get_pdf(self.sponsorship_ids[:1])[0] | ||
| if self.template_id.layout == "CH-A-3S01-1": | ||
| # Read page 2 | ||
| in_pdf = PdfFileReader(BytesIO(pdf)) | ||
| output_pdf = PdfFileWriter() | ||
| out_data = BytesIO() | ||
| output_pdf.addPage(in_pdf.getPage(1)) | ||
| output_pdf.write(out_data) | ||
| out_data.seek(0) | ||
| pdf = out_data.read() | ||
|
|
||
| try: | ||
| pdf = self._get_pdf(self.sponsorship_ids[:1])[0] | ||
|
|
||
| if self.template_id.layout == "CH-A-3S01-1": | ||
| in_pdf = PdfFileReader(BytesIO(pdf)) | ||
| output_pdf = PdfFileWriter() | ||
| output_pdf.addPage(in_pdf.getPage(1)) | ||
| out_data = BytesIO() | ||
| output_pdf.write(out_data) | ||
| pdf = out_data.getvalue() | ||
|
|
||
| n_pages = PdfFileReader(BytesIO(pdf)).getNumPages() | ||
| if n_pages > self.MAX_PAGE_COUNT: | ||
| msg = _("Oops your letter has %d pages. The limit is %d.") % ( | ||
| n_pages, | ||
| self.MAX_PAGE_COUNT, | ||
| ) | ||
|
|
||
| raise UserError(msg) | ||
| with Image(blob=pdf, resolution=96) as pdf_image: | ||
| preview = base64.b64encode(pdf_image.make_blob(format="jpeg")) | ||
| except PolicyError as error: | ||
| _logger.error( | ||
| "ImageMagick policy error. Please add following line to " | ||
| "/etc/Image-Magick-<version>/policy.xml: " | ||
| '<policy domain="coder" rights="read|write" ' | ||
| 'pattern="PDF" />', | ||
| ) | ||
| raise UserError( | ||
| _( | ||
| "Please allow ImageMagick to write PDF files." | ||
| " Ask an IT admin for help." | ||
|
|
||
| return self.isolated_write( | ||
| { | ||
| "state": "preview", | ||
| "generation_status": "done", | ||
| "generation_error_message": False, | ||
| "preview_image": preview, | ||
| "preview_pdf": base64.b64encode(pdf), | ||
| } | ||
| ) | ||
| ) from error | ||
| except TypeError as error: | ||
| raise UserError( | ||
|
|
||
| except (PolicyError, TypeError, UserError, Exception) as error: | ||
| error_message = ( | ||
| _( | ||
| "Unfortunately the server cannot generate PDF documents " | ||
| "at the moment. Our IT team is informed and will fix this issue " | ||
| "as soon as possible." | ||
| ) | ||
| if isinstance(error, PolicyError) | ||
| else str(error) | ||
| if isinstance(error, UserError) | ||
| else _( | ||
| "There was an error while generating the PDF of the letter. " | ||
| "Please check FPDF logs for more information." | ||
| ) | ||
| ) from error | ||
|
|
||
| pdf_image = base64.b64encode(pdf) | ||
|
|
||
| return self.write( | ||
| { | ||
| "state": "preview", | ||
| "preview_image": preview, | ||
| "preview_pdf": pdf_image, | ||
| } | ||
| ) | ||
| ) | ||
| _logger.error("Unable to generate PDF", exc_info=True) | ||
| if self.env.context.get("raise_error"): | ||
| raise UserError(error_message) from error | ||
| self.env.cr.rollback() | ||
| self.isolated_write( | ||
| { | ||
| "generation_status": "failed", | ||
| "generation_error_message": error_message, | ||
| } | ||
| ) | ||
|
|
||
| def edit(self): | ||
| """Generate a picture for preview.""" | ||
|
|
@@ -184,6 +213,7 @@ def generate_letters(self): | |
| Launch S2B Creation job | ||
| :return: True | ||
| """ | ||
| self.generation_status = "finalizing" | ||
| self.with_delay( | ||
| identity_key="s2b_generator." + str(self.ids) | ||
| ).generate_letters_job() | ||
|
|
@@ -217,7 +247,7 @@ def generate_letters_job(self): | |
| "res_model": letters._name, | ||
| }, | ||
| ) | ||
| for atchmt in self.image_ids | ||
| for atchmt in self.image_ids.sorted(reverse=True) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ] | ||
| letters += letters.create(vals) | ||
|
|
||
|
|
@@ -227,11 +257,26 @@ def generate_letters_job(self): | |
| # If the operation succeeds, notify the user | ||
| message = "Letters have been successfully generated." | ||
| self.env.user.notify_success(message=message) | ||
| return self.write({"state": "done", "date": fields.Datetime.now()}) | ||
|
|
||
| return self.isolated_write( | ||
| { | ||
| "state": "done", | ||
| "date": fields.Datetime.now(), | ||
| "generation_status": "done", | ||
| "generation_error_message": False, | ||
| } | ||
| ) | ||
|
|
||
| except Exception as error: | ||
| # If the operation fails, notify the user with the error message | ||
| self.env.cr.rollback() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| error_message = str(error) | ||
| self.isolated_write( | ||
| { | ||
| "generation_status": "failed", | ||
| "generation_error_message": error_message, | ||
| } | ||
| ) | ||
| _logger.error(error_message, exc_info=True) | ||
| self.env.user.notify_danger(message=error_message) | ||
|
|
||
|
|
@@ -257,8 +302,8 @@ def _get_text(self, sponsorship): | |
| keywords = { | ||
| "%child%": child.preferred_name, | ||
| "%age%": str(child.age), | ||
| "%firstname%": sponsor.firstname or sponsor.name, | ||
| "%lastname%": sponsor.firstname and sponsor.lastname or "", | ||
| "%firstname%": sponsor.preferred_name or sponsor.firstname or sponsor.name, | ||
| "%lastname%": sponsor.lastname or "", | ||
|
Comment on lines
+305
to
+306
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| text = self.body | ||
| for keyword, replacement in list(keywords.items()): | ||
|
|
@@ -285,7 +330,19 @@ def _get_pdf(self, sponsorship): | |
| sponsorship.display_name, | ||
| (header, ""), # Headers (front/back) | ||
| {"Original": [text]}, # Text | ||
| self.mapped("image_ids.datas"), # Images | ||
| self.mapped("image_ids").sorted(reverse=True).mapped("datas"), # Images | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| s2b_generator=self, | ||
| ), | ||
| text, | ||
| ) | ||
|
|
||
| def isolated_write(self, vals): | ||
| """Use a separate transaction to update the letter_generator.""" | ||
| if len(self) != 1: | ||
| return False | ||
|
|
||
| with self.env.registry.cursor() as new_cr: | ||
| new_env = self.env(cr=new_cr) | ||
| new_s2b_generator = new_env[self._name].browse(self.id) | ||
| new_s2b_generator.write(vals) | ||
| new_cr.commit() | ||
|
Comment on lines
+339
to
+348
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
ifstatement checks the hold type again, which seems redundant since the code block is already within thefor hold in self.filtered(lambda h: h.no_money_extension < 3)loop andif hold.type == HoldType.NO_MONEY_HOLD.value:block. Consider removing this redundant check to simplify the code.