Skip to Content
Menu
This question has been flagged
6 Replies
4852 Views

Hello every one ! hope you're doing well 

i'm a beginner in odoo and i'm facing some issues hope you nice people could help me , 

i'm trying to add a binary field or attachment (any  type of field that will allow me to upload a file  ) in my details form in the customer.

first i added a binary field with attachment=True and inherited the "portal.portal_my_details" template added my fields and i added them in the MANDATORY_BILLING_FIELDS and now i can update the values exept for the binary field whenever i try to update the values dictionary  mentions that str has no attribute read i don't know if i got things right but i did logged the dictionary and the type(post.get('my binary field')) returns str i don't undersatnd i just need to upload  a file in the customer portal form 
thank you for you help and time 
 

Avatar
Discard

It would make more sense if you will share your code also.

Author

In fact i solved the issue here's what i did if anyone may need this in the futur

1/ first i inherited the res.partner and added my binary field as follow :

field = fields.binary(string="my field")

2/ next i ingerited the portal.portal_my_details view and added my field as <input type="file"> without forgetting to add the attribute <attribute name="enctype">multipart/form-data</attribute> to the form in the template

3/ last step was to inherit the CustomerPortal class in the portal controller and this is where i encoded the file by doing this at the begining befor updation the values dict :

content = post.get('content')

FileData = content.read()

file_base64 = base64.encodestring(FileData)

and it worked perfectly

thank you hope it helps

Best Answer

This view and Controller override retain the uploaded filename as well.
View:

<data>

  <xpath expr="//form" position="attributes">

    <attribute name="enctype">multipart/form-data</attribute>

  </xpath>

  <xpath expr="//div/div/div/div[13]" position="after">

    <div t-attf-class="form-group #{error.get('x_test_file') and 'o_has_error' or ''} col-xl-6">

      <label class="col-form-label label-optional" for="x_test_file">Test File</label>

      <input id="upload" type="file" name="x_test_file" t-attf-class="form-control #{error.get('x_test_file') and 'is-invalid' or ''}" t-att-value="x_test_file or partner.x_test_file"/>

      <!--input id="upload_name" type="text" class="form-control o_website_form_input" name="x_test_file_filename"/-->

    </div>

  </xpath>

</data>

Controller:

import base64


from odoo import http

from odoo.http import content_disposition, Controller, request, route


from odoo.addons.portal.controllers.portal import CustomerPortal


CustomerPortal.OPTIONAL_BILLING_FIELDS.append('x_test_file')

CustomerPortal.OPTIONAL_BILLING_FIELDS.append('x_test_file_filename')


class CustomCustomerPortal(CustomerPortal):


    @route(['/my/account'], type='http', auth='user', website=True)

    def account(self, redirect=None, **post):

        

        test_file = post.get('x_test_file')

        if test_file:

            file_name = test_file.filename

            file_base64 = base64.encodestring(test_file.read())

            post.update({'x_test_file': file_base64})

            post['x_test_file_filename'] = file_name

        

        res = super(CustomCustomerPortal, self).account(redirect=None, **post)

        

        return res

Avatar
Discard
Author Best Answer

hello

dear Ajin A K thank you for your response but my requirements are different, i know about the attachement in the mail.message object but i need the attachementt or binary field to be inside the form along side with the others fields such as the name and the vat i need a file upload field.

thank you for your response anyways.
Avatar
Discard
Best Answer

Dear Amal ben aissa,

By default odoo you can attach any files to the customer portal or any form. you can see attachment symbol from the bottom of every form. you can click the symbol to attach files.


Thank you.

Avatar
Discard