This question has been flagged
1 Reply
7507 Views

Hello, Does anyone can help me how to create a file? I have my tree view with data on it. Now i wonder how can i write it on a file so that user can have c copy on it in a .DAT file. Any one knows?

I tried to make a function, but it the web pop an error message:

fo = open(home+"\\persons.DAT","wb")
IOError: [Errno 13] Permission denied: '~user\\persons.DAT'

Here is my method:
    def write_file(self,cr,uid,ids,context=None):
        fo = {}
        home = os.path.expanduser('~user')
        fo = open(home+"\\persons.DAT","wb")
        active_id = self.search(cr,uid,[])
        for list in self.browse(cr,uid,active_id,context=context):
            fo.write(list.id+","+list.person_id.id)
        return fo

Any help is much appreciated.

Avatar
Discard
Best Answer

The user in which OpenERP is running most likely has no access rights to any other directory then the OpenERP directory itself. You will notice for example that files like attachments are usually stored in database OR in the filestore directory under the openerp directory.  Since you are pointing to a home directory, which might be something this user doesn't have. The /tmp directory is also available, but make since its temporary, it is not permanent and therefor not ideal in your situation.

However, there is an alternative to a fysical file. Look up documentation in python on StringIO. This allows you to create a virtual file, and use any normal file operation on it like you normally would. Difference is that this file is not stored on the disk and therefore does not require specific access rights.

Once you have such a file, you store its contents in a binary field in OpenERP and/or as attachment. I think this will work for you.

Avatar
Discard
Author

Thank you sir for your answer, actually i did what you have told me, but i got an error, or perhaps my code is far way from reality. I made a class named 'file.holder' just like this one: class file_holder(osv.osv): _name = 'file.holder' _columns = { 'name' : fields.char('File name'), 'file_data' : fields.binary('File Name',filters='*.DAT',) } def get_file(self, cr, uid, context=None): file_path = addons.get_module_resource('mp_seven', 'files', 'studentlist.DAT') fo = {} fo = open(file_path,"wb") active_id = self.pool.get('student.student').search(cr,uid,[]) for list in self.browse(cr,uid,active_id,context=context): fo.write(list.id+","+list.name) self.create(cr,uid,{ 'name' : '', 'file_data' : file_path }) return fo.read().encode('base64') file_holder() where addons is a folder in openerpserver-7, mp_seven is my project name, files is subfolder in my project and where studentlist.DAT resides. Whenever this method is bieng called in other class the browser/terminal says : file_path = addons.get_module_resource('mp_seven', 'files', 'studentlist.DAT') NameError: global name 'addons' is not defined