This question has been flagged
1 Reply
14373 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,
                            }
Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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

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

Author

@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

Author

@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

Author

But i dnt want to save data in db :(

Author

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