Search code examples
python-2.7odoo

odoo ir.sequence not working when Use a user other than the system administrator


I'm working on Openerp7 I try to convert attachments to files that are saved in the server instead of saving them in the database

My problem is in naming files I'm trying to add a serial number to her name using ir.sequence

It works fine when I log in using the system administrator But when logging in using any other user : ir.sequence not create the sequence the Function create return False

my xml code:

    <record id="ir_attachment" model="ir.sequence.type">
        <field name="name">ir attachment</field>
        <field name="code">ir.attachment</field>
    </record>

    <record id="seq_ir_attachment" model="ir.sequence">
        <field name="name">ir attachment</field>
        <field name="code">ir.attachment</field>
        <field name="prefix">Att</field>
        <field name="padding">5</field>
        <field name="implementation">no_gap</field>
    </record>

python code :

data_rec.write({'file_name':self.pool.get("ir.sequence").get(cr,uid,'ir.attachment')})


Solution

  • The get method returns False because the ir.attachment company is not present in the currently visible companies and different from False (set to default value).

    The get method will end by calling the _next method, which will return False if we provide an empty sequence ids list (seq_ids).

    The sequence ids list (ids) is computed like following:

    company_ids = self.pool.get('res.company').search(cr, uid, [], context=context) + [False]
    ids = self.search(cr, uid, ['&', ('code', '=', sequence_code), ('company_id', 'in', company_ids)])
    

    It works fine when you log in using the system administrator because the superuser bypasses the security rules and can see all the available companies, but a user is able to select only the currently visible companies according to rules.

    After selecting the visible companies of the current user, OpenERP adds False to that list ( which means that if the sequence company_id is not set, the sequence id that corresponds to the code passed to the search method will always be returned without depending on the user settings) then, it will try to find the sequence ids where sequence code is equal to the sequence code passed to the get method and company_id is present in the selected companies.

    If the sequence company_id field value is not present in the user currently visible companies, it will return an empty list and the _next method will return False because seq_ids is empty.

    def _next(self, cr, uid, seq_ids, context=None):
        if not seq_ids:
            return False
    

    Include the user allowed companies in the search result:

    The res company search method was altered to make it possible to return the user company and all his allowed companies by passing the user_preference throw the context.

    You can find a comment in res.company _search method that highlights that:

    if context.get('user_preference'):
        # We browse as superuser. Otherwise, the user would be able to
        # select only the currently visible companies (according to rules,
        # which are probably to allow to see the child companies) even if
        # she belongs to some other companies.
        user = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid, context=context)
        cmp_ids = list(set([user.company_id.id] + [cmp.id for cmp in user.company_ids]))
        return cmp_ids
    

    example:

    if context is None:
        context = {}
    context["user_preference"] = True
    self.pool.get("ir.sequence").get(cr, uid, 'ir.attachment', context=context)
    

    Alternative to store attachments in the server file system:

    To store files in the OpenERP server filesystem, you can install the document management system.

    Managing Attachments documentation of OpenERP-7

    If you do not install the document management system then the files that are attached to an OpenERP resource are stored directly in the database. Once the document management system has been installed, the contents of the files are no longer stored in the database but are stored instead on the OpenERP server filesystem in a directory named 'filestore'.

    You can then read and add attachments to OpenERP resources quite independent of the OpenERP interface or the FTP server using simple drag and drop.