Yuniar Kurniawan S
Yuniar Kurniawan S

Reputation: 93

Saving variable get_pdf() into fields.binary

I want to save a variable automatically into a fields.binary (email_attachment_file) from get_pdf function.

My codes below here:

 class example_example(models.Model):
    email_attachment_file   =   fields.Binary('Data (.txt,.pdf)')
    email_filename          =   fields.Char('Filename')

    def generate(self,etc..):
      report_name = "report_name_template"

      datas = {
            'ids':[],
            'model' : etc,
            'form'  : etc
            'context': context
            }

      moddelReport = self.pool.get('report')
      alpha =  modelReport.get_pdf(cr, uid,[],report_name,None,datas,context=context)  

      #alpha = base64.decodestring(alpha)
      #alpha = alpha.decode('unicode_escape').encode('utf-8')

      # --------- how to save alpha variable into fields.binary

And, is there something wrong modelReport.get_pdf function ?

Upvotes: 3

Views: 1194

Answers (1)

Use encodestring() instead of decodestring().

report_obj = self.pool.get('report')
data =  modelReport.get_pdf(cr, uid,[],report_name,None,datas,context=context)
self.email_attachment_file  = base64.encodestring(data)

Upvotes: 2

Related Questions