Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to update function field on button click

By
Anuradha
on 9/28/13, 6:44 AM 5,778 views
How can i update function field on button click. I want to display names of files in a directory on button click. i will get file names in a list . how can i show that in xml using function field ? Any one please help       
 My xml is
 <?xml version="1.0" encoding="utf-8" ?>
    <openerp>
           <data>
              <record id="voip_form_view" model="ir.ui.view">
                <field name="name">Voip Details</field>
                 <field name="model">filedata</field>
                  <field name="arch" type="xml">
                     <form string="voip" version="7.0">
                             <style>.openerp .oe_application 
                               .oe_breadcrumb_title  {display:none }</style>
                             <style>.openerp .oe_view_manager table.
                             oe_view_manager_header .oe_view_manager_buttons{display:none } 
                             </style>
                            <button name="get_filenames" type="object" string="List Files" />
                           </form>
                          </field>
        </record>
         <record id="voipdata_tree_view" model="ir.ui.view">
              <field name="name">File Details</field>
               <field name="model">filedata</field>
                 <field name="priority" eval="1"/>
                 <field name="type">tree</field>
                 <field name="arch" type="xml">
                 <tree string="File Names" >
                     <field name="file_name"/>
                      <field name="time_created" string="Time Created"/>
                       <field name="size" string="Size"/>
                        <field name="file_path"/>
                    </tree>
                 </field>
          </record>
          <record model="ir.actions.act_window" id="action_voipdata_form">
              <field name="name">Voip Details</field>
               <field name="res_model">filedata</field>
                 <field name="view_type">tree</field>
                  <field name="view_mode">tree</field>
                   <field name="view_id" ref="voipdata_tree_view"/>
             </record>
             <record model="ir.actions.act_window" id="action_voip_form">
                  <field name="name">Voip Details</field>
                    <field name="res_model">filedata</field>
                     <field name="view_type">form</field>
                      <field name="view_mode">form</field>
                       <field name="view_id" ref="voip_form_view"/>
            </record>
            <menuitem name="Voip" parent="base.menu_reporting" id="voip_menu_mainform"/>
             <menuitem name="Voip" parent="voip_menu_mainform" id="voip_menu_subform" 
               action="action_voip_form"/>
  </data>
</openerp>
    My python code is
    class filedata(osv.osv):
                    _name = 'filedata'
                    _log_access = False
                    def compute(self, cr, uid, ids, field_names, arg, context):
                       result = {
                           2: {'file_name': 'Bob', 'file_path': 'ffff',
                              'time_created':'2011-06-09','size':'222'}}
                           return result
                    _columns = {
                      'file_name' : fields.function
                                (compute, type='char', size=50,multi='person_data'),
                      'file_path' :fields.function
                              (compute, type='char', size=50,multi='person_data'),
                      'time_created':fields.function
                               (compute, type='char', size=50,multi='person_data'),
                       'size' : fields.function
                                (compute, type='char', size=50,multi='person_data')
                        }
                    def get_filenames(self, cr, uid, ids, context=None):
                        lst_dirfiles = os.listdir('/voice-recordings')
                        cr.execute("select id,file_name from filedata")
                        result = cr.fetchall()
                        lst_dbfiles = []
                        for res in result:
                            file_name = res[1]
                            if (file_name == None):
                                cr.execute("delete from filedata where id=%s",(res[0],))
                            else:
                                lst_dbfiles.append(file_name)
                         if lst_dirfiles:
                            for file_name in lst_dirfiles:
                                if file_name not in lst_dbfiles:
                                  time_created = time.ctime(os.path.getctime
                                                           ("/voice-recordings/"+file_name))
                                  size = str(os.path.getsize
                                               ("/voice-recordings/"+file_name))+' Bytes'
                                  file_path = "/voice-recordings/"+file_name
                                  file_base64 = ''
                                   file_object = open(file_path, "rb")
                                   file_base64 = base64.b64encode(file_object.read())
                                   file_path = "ftp://test.claudion.com:123/"+file_name
                                   cr.execute("inserit into filedata
                                                    (file_name,file_path,time_created,size) 
                                                    values(%s,%s,%s,%s);",   
                                                    (file_name,file_path,time_created,size))
                        return {
                            'type' : 'ir.actions.act_window',
                            'name' : 'File Details',
                            'view_mode' : 'tree',
                            'view_type' :  'form',
                            'res_model' : 'filedata',
                            'context' : context,
                            }
1

Andreas Brueckl

--Andreas Brueckl--
5056
| 7 8 7
Vienna, Austria
--Andreas Brueckl--
OpenERP Consulting and Development
Andreas Brueckl
On 9/28/13, 7:22 AM

What you want to accomplish is not that easy. Here are some hints for your implementation:

  • Use a parent object filedata with child object filedata.line
  • filedata has a function field lines of type one2many (relation filedata.line)
  • The compute function then returns the lines which you currently want to return in function get_filenames
  • Keep in mind that the function is called every time you open the view (so performance could be poor with this approach)

Here is another approach:

  • Use only one model filedata
  • Put the logic of get_filenames into a Server Action which you call by clicking onto a menu item
  • The server action then deletes all current records of filedata and creates a new entry for every record with self.pool.get('filedata').create ...
  • This approach only computes on button-click and is much faster when only displaying the data

Hope this helps.

@Andreas Brueck i checked sale/sale.py but i didnt understand anything. Can you please explain it to me

Anuradha
on 9/28/13, 7:32 AM

Function fields are automatically updated after a button click. If would help if you post your code in your question.

Andreas Brueckl
on 9/28/13, 7:40 AM

@Andreas Brueckl I added my code in question.my button calls get_filenames function which should return a view to display file names. In my code i inserted file names in to db in that function and displays it in view,. Now i dont want to save anything in db . that is my issue please help

Anuradha
on 9/28/13, 8:02 AM

@Andreas Brueck Any other way to achive this ? Please help !! I have been on this for long time :(. I dnt want to use function field i want to display file name in view and play that mp3 files from there

Anuradha
on 9/28/13, 8:19 AM

But i dnt want to save data in db :(

Anuradha
on 9/28/13, 8:39 AM

i dnt know abt "Server Action" how use that in menu click. Can you explain to me with my code example

Anuradha
on 9/28/13, 8:54 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 9/28/13, 6:44 AM
Seen: 5778 times
Last updated: 8/25/15, 2:23 PM