0

I'm trying to add some demo data to a module

one of the models extends res.partner and has a create method (it overrides it from the parent)

The create method does 2 things

  1. it checks that the current user is in some groups
  2. it populates a serial number field with a value (calculated with a sequence)

This is the code

    @api.model
    def create(self, vals_list):
        if not self.env.user.has_group('my-project.group_super') and not self.env.user.has_group(
                'my-project.group_some_other_group'):
            raise ValidationError(
                f"Warning! You have no permission to create a patient. Contact your administrator")

        vals_list['patient_number'] = self.env['ir.sequence'].next_by_code('my-project.res.partner')

        return super(Patient, self).create(vals_list)

please, note the ValidationError in this method. The message it reports is

Warning! You have no permission to create a patient. Contact your administrator

in the xml file I'm using to add demo data to this model I have this record

   <record id="johnsmith" model="res.partner">

            <field name="active" eval="True"></field>
            <field name="is_patient" eval="True"></field>
            <field name="company_type">person</field>

            <field name="firstname">John</field>
            <field name="lastname">Smith</field>
            <field name="phone">+39 345 345 345</field>
            <field name="email">asdfasdfasd</field>
            <field name="birthdate">1999-02-17</field>
            <field name="place_of_birth">Milan</field>
            <field name="fiscalcode">IUCGIUCHEUIOHD38</field>
            <field name="city">Milan</field>
            <field name="country_id" ref="base.it"></field>
            <field name="gender">male</field>
            <field name="type">contact</field>
        </record>

This record mostly works.

Meaning, it shows up as a demo record when I load demo data.

I say "mostly" because the field patient-number is not populated

But then I copy and paste this record and I only change the id to johnsmith2, like this

       <record id="johnsmith2" model="res.partner">
    
    
                <field name="active" eval="True"></field>
                <field name="is_patient" eval="True"></field>
                <field name="company_type">person</field>
    
                <field name="firstname">John</field>
                <field name="lastname">Smith</field>
                <field name="phone">+39 345 345 345</field>
                <field name="email">asdfasdfasd</field>
                <field name="birthdate">1999-02-17</field>
                <field name="place_of_birth">Milan</field>
                <field name="fiscalcode">IUCGIUCHEUIOHD38</field>
                <field name="city">Milan</field>
                <field name="country_id" ref="base.it"></field>
                <field name="gender">male</field>
                <field name="type">contact</field>
            </record>

this second record doesn't work

when installing the module in a database with demo data enabled, I get the error message in the body of the create method override

Warning! You have no permission to create a patient. Contact your administrator

in the log and the loading of demo data fails

It seems that the first record is not being processed by the create method override, the second one is

what is going on here ?

EDIT

I'm adding the definition of user groups as this came up in the comments

These are the 2 groups mentioned in the create method


    <record id="my_project" model="ir.module.category">
        <field name="name">My Project</field>
        <field name="description">Users groups for My project</field>
    </record>

[...]

<record id="group_super" model="res.groups">
    <field name="name">Supervisor</field>
    <field name="category_id" ref="my-project.my_project"/>
</record>

[...]


 <record id="group_some_other_group" model="res.groups">
    <field name="name">Some Other Group</field>
    <field name="category_id" ref="my-project.my_project"/>
 </record>

These are the relevant lines in the ir.model.access.csv file

sc_study_partner_super,res.partner.super,model_res_partner,group_super,1,1,1,1

[...]

sc_study_partner_some_other_role,res.partner.some_other_role,model_res_partner,group_some_other_group,1,1,1,1

The demo/patients.xml file does not contain the no update flag

user1632812
  • 431
  • 3
  • 16

1 Answers1

0

You can use self.env.is_admin() to check if its runned by administrator environment

Also, it is common to add rights to the super user and the admisstrator:

<record model="res.users" id="base.user_root">
    <field eval="[(4,ref('my-project.group_super'))]" name="groups_id"/>
</record>
    
<record model="res.users" id="base.user_admin">
    <field eval="[(4,ref('my-project.group_super'))]" name="groups_id"/>
</record>
Paxmees
  • 1,540
  • 1
  • 15
  • 28