33
44import hashlib
55import json
6+ import logging
67from base64 import b64decode , b64encode
78from hashlib import sha256
89from io import BytesIO
1415from reportlab .pdfgen import canvas
1516from reportlab .platypus import Image , Paragraph
1617
17- from odoo import _ , api , fields , models
18+ from odoo import api , fields , models
1819from odoo .exceptions import UserError , ValidationError
1920from odoo .http import request
2021from odoo .tools import float_repr
2122
23+ _logger = logging .getLogger (__name__ )
24+
2225
2326class SignOcaRequest (models .Model ):
2427 _name = "sign.oca.request"
@@ -159,10 +162,14 @@ def _ensure_draft(self):
159162 self .ensure_one ()
160163 if not self .signer_ids :
161164 raise ValidationError (
162- _ ("There are no signers, please fill them before configuring it" )
165+ self .env ._ (
166+ "There are no signers, please fill them before configuring it"
167+ )
163168 )
164169 if not self .state == "draft" :
165- raise ValidationError (_ ("You can only configure requests in draft state" ))
170+ raise ValidationError (
171+ self .env ._ ("You can only configure requests in draft state" )
172+ )
166173
167174 def configure (self ):
168175 self ._ensure_draft ()
@@ -253,7 +260,7 @@ def action_send(self, sign_now=False, message=""):
253260 self .env ["mail.thread" ].message_notify (
254261 body = render_result ,
255262 partner_ids = signer .partner_id .ids ,
256- subject = _ ("New document to sign" ),
263+ subject = self . env . _ ("New document to sign" ),
257264 subtype_id = self .env .ref ("mail.mt_comment" ).id ,
258265 mail_auto_delete = False ,
259266 email_layout_xmlid = "mail.mail_notification_light" ,
@@ -281,13 +288,13 @@ def action_send_signed_request(self):
281288 # The message will not be linked to the record because we do not want
282289 # it happen.
283290 self .env ["mail.thread" ].message_notify (
284- body = _ (
291+ body = self . env . _ (
285292 "%(name)s (%(email)s) has sent the signed document." ,
286293 name = self .create_uid .name ,
287294 email = self .create_uid .email ,
288295 ),
289296 partner_ids = signer .partner_id .ids ,
290- subject = _ ("Signed document" ),
297+ subject = self . env . _ ("Signed document" ),
291298 subtype_id = self .env .ref ("mail.mt_comment" ).id ,
292299 mail_auto_delete = False ,
293300 attachment_ids = attachments .ids ,
@@ -413,7 +420,9 @@ def get_info(self, access_token=False):
413420 def sign (self ):
414421 self .ensure_one ()
415422 if not self .is_allow_signature :
416- raise ValidationError (_ ("You are not allowed to sign this document." ))
423+ raise ValidationError (
424+ self .env ._ ("You are not allowed to sign this document." )
425+ )
417426 return {
418427 "target" : "new" ,
419428 "type" : "ir.actions.act_url" ,
@@ -424,10 +433,11 @@ def action_sign(self, items, access_token=False, latitude=False, longitude=False
424433 self .ensure_one ()
425434 if self .signed_on :
426435 raise ValidationError (
427- _ ("Users %s has already signed the document" ) % self .partner_id .name
436+ self .env ._ ("Users %s has already signed the document" )
437+ % self .partner_id .name
428438 )
429439 if self .request_id .state != "sent" :
430- raise ValidationError (_ ("Request cannot be signed" ))
440+ raise ValidationError (self . env . _ ("Request cannot be signed" ))
431441 self .signed_on = fields .Datetime .now ()
432442 # current_hash = self.request_id.current_hash
433443 signatory_data = self .request_id .signatory_data
@@ -488,7 +498,7 @@ def _check_signable(self, item):
488498 if not item ["required" ]:
489499 return
490500 if not item ["value" ]:
491- raise ValidationError (_ ("Field %s is not filled" ) % item ["name" ])
501+ raise ValidationError (self . env . _ ("Field %s is not filled" ) % item ["name" ])
492502
493503 def _get_pdf_page_text (self , item , box ):
494504 packet = BytesIO ()
@@ -548,23 +558,35 @@ def _get_pdf_page_signature(self, item, box):
548558 can = canvas .Canvas (packet , pagesize = (box .getWidth (), box .getHeight ()))
549559 if not item ["value" ]:
550560 return False
551- par = Image (
552- BytesIO (b64decode (item ["value" ])),
553- width = item ["width" ] / 100 * float (box .getWidth ()),
554- height = item ["height" ] / 100 * float (box .getHeight ()),
555- )
556- par .drawOn (
557- can ,
558- item ["position_x" ] / 100 * float (box .getWidth ()),
559- (100 - item ["position_y" ] - item ["height" ]) / 100 * float (box .getHeight ()),
560- )
561+ try :
562+ base64_str = item ["value" ]
563+ if len (base64_str ) % 4 :
564+ base64_str += "=" * (4 - len (base64_str ) % 4 )
565+ if "," in base64_str :
566+ base64_str = item ["value" ].split ("," )[1 ]
567+ image_data = b64decode (base64_str )
568+ par = Image (
569+ BytesIO (image_data ),
570+ width = item ["width" ] / 100 * float (box .getWidth ()),
571+ height = item ["height" ] / 100 * float (box .getHeight ()),
572+ )
573+ par .drawOn (
574+ can ,
575+ item ["position_x" ] / 100 * float (box .getWidth ()),
576+ (100 - item ["position_y" ] - item ["height" ])
577+ / 100
578+ * float (box .getHeight ()),
579+ )
580+ except Exception as e :
581+ _logger .info (f"Error decoding Base64 string: { e } " )
582+ return False
561583 can .save ()
562584 packet .seek (0 )
563585 new_pdf = PdfFileReader (packet )
564586 return new_pdf .getPage (0 )
565587
566588 def _get_pdf_page (self , item , box ):
567- return getattr (self , "_get_pdf_page_%s" % item [" field_type" ] )(item , box )
589+ return getattr (self , f "_get_pdf_page_{ item [' field_type' ] } " )(item , box )
568590
569591 def _set_action_log (self , action , ** kwargs ):
570592 self .ensure_one ()
@@ -599,7 +621,7 @@ def _get_new_hash(self, secure_seq_number):
599621 )
600622 if prev_sign and len (prev_sign ) != 1 :
601623 raise UserError (
602- _ (
624+ self . env . _ (
603625 "An error occurred when computing the inalterability. "
604626 "Impossible to get the unique previous signer information."
605627 )
0 commit comments