How to filter data in odoo calendar? - odoo

i want to filter some data in standart odoo calendar. I need to dont show events, that is private for users, who dont participate that event, i can do this by adding new filter in views, like:
<filter string="new filter" name="dont_show_others_private_events" domain="['|', ('privacy','=','public'), '&' ,('partner_ids.user_ids', 'in', [uid]), ('privacy', '=', 'private')]"/>
but problem is, that this filter is visible and any user can turn it off.
How can i do the same on server level, maybe in models.py file? or there is way to hide this filter but leave it active? i tried also white this filter in views:
<record id="action_calendar_event_type" model="ir.actions.act_window">
<field name="name">Meeting Types</field>
<field name="res_model">calendar.event.type</field>
<field name="view_id" ref="view_calendar_event_type_tree"/>
<field name="domain">['|', ('privacy','=','public'), '&' ,('partner_ids.user_ids', 'in', [uid]), ('privacy', '=', 'private')]</field>
</record>
but is doesnt work for me

if i understood your question correctly you want this "filter" to always be active and users cannot disable it ?
If that is the case then add a new Record Rule and not a filter.
You can add the new entry in the xml under the Security folder.
And here is an example of the record rule:
<record id="private_events_filter" model="ir.rule">
<field name="name">Private events filter</field>
<field name="model_id" ref="calendar.event"/>
<field name="domain_force">['|', ('privacy','=','public'), '&' ,('partner_ids.user_ids', 'in', [uid]), ('privacy', '=', 'private')]</field>
<field name="groups" eval="[(4, ref('base.group_portal'))]"/>
</record>

this works:
<record id="calendar_event_rule_private_dont_show_others" model="ir.rule">
<field name="model_id" ref="model_calendar_event"/>
<field name="name">Dont show other private ivents</field>
<field name="domain_force">['|', ('privacy','=','public'), '&' ,('partner_ids.user_ids', 'in', [user.id]), ('privacy', '=', 'private')]</field>
<field name="perm_read" eval="True"/>
<field name="perm_write" eval="True"/>
<field name="perm_create" eval="True"/>
<field name="perm_unlink" eval="True"/>
</record>

Related

the first record is not processed by the create method, the second one is

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
it checks that the current user is in some groups
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
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>

How to change selection fields with filters search in odoo? [Odoo 9]

I have created a state on models
state = fields.Selection([
('new', 'New'),
('draft', 'Draft'),
('approved', 'Approved')
],default='new')
I make menuitems that are different for user and admin. But in the admin groups, i get an error after adding filters search.
<record id="view_admin_filter" model="ir.ui.view">
<field name="name">Admin</field>
<field name="model">mymodels</field>
<field name="arch" type="xml">
<search string="Admin">
<filter string="Draft" name="state" domain="[('state','=','draft')]"/>
</search>
</field>
</record>
and
<record id="open_module_tree_admin" model="ir.actions.act_window">
<field name="name">Admin</field>
<field name="res_model">mymodels</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="context">{
"search_default_state": 1,
"default_state": 1}
</field>
<field name="domain">[]</field>
<field name="view_ids" eval="[(5, 0, 0),
(0, 0, {'view_mode': 'tree', 'view_id': ref('view_admin_tree')}),
(0, 0, {'view_mode': 'form', 'view_id': ref('view_admin_form')})]"/>
<field name="search_view_id" ref="view_admin_filter"/>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
Click to create.
</p>
</field>
</record>
I found an error like this
ValueError: Wrong value for mail.mail.state: 1
I have tried to replace it like this
"search_default_state": 'draft',
"default_state": 'draft'}
but it still doesn't work and getting error
ValueError: Wrong value for mail.mail.state: u'draft'
how should I fix it?
From what you said you are setting a default value for state some where in your code
with a value that is not in selection.
like for example when you did this:
"default_state": 1
This will cause this error to happen because I'm sure that 1 is not valid value
instead doing this is correct.
"default_state": 'draft'
But only if your selection has this value 'draft'.
One thing you should know in XML removing the code of the context from the action
definition will not remove it from the data base (you will keep having the same problem).
To fix this problem remove this default values from your code then do it again step by step
and make sure you upgrade the moduel and restart the server.
<record id="open_module_tree_admin" model="ir.actions.act_window">
<field name="name">Admin</field>
<field name="res_model">mymodels</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="context">{}</field> <!-- this will update the context to {} -->
.....
....
and check your python code for default value or onchange event make sure your not setting
the state field to a wrong value.
fields.Selection(.... default='draft')
Hope this helps you
And for your filter don't give them names like your fields names
<filter string="Draft" name="draft_state" domain="[('state','=','draft')]"/>
This way you can apply this filter in the context of the action like this:*
{'search_default_draft_state': 1}
I think it's safer.

Generate odoo 9 demo data

How can I add inputs (code and name) in the below example for module hr_payroll?
For example: input name is "Stimulation", code = "STM"
<record id="hr_rule_taxable" model="hr.salary.rule">
<field name="name">Gross</field>
<field name="sequence" eval="100"/>
<field name="code">GROSS</field>
<field name="category_id" ref="hr_payroll.GROSS"/>
<field name="condition_select">none</field>
<field name="amount_select">code</field>
<field name="amount_python_compute">result = categories.BASIC + categories.ALW</field>
</record>
Are you asking how to override parts of this data in your module's XML?
If so, it can be done by using <module_name>.<view_id> to override certain fields from the same record, such as:
<record id="hr_payroll.hr_rule_taxable" model="hr.salary.rule">
<field name="name">Stimulation</field>
<field name="code">STM</field>
</record>
Note: This does not work if the original record is in a <data noupdate="1"> block.

How to set default field value from xml code in odoo?

In sales order module I have created a new customised form view, in sale.py file I have created a new field 'is_sample'
'is_sample': fields.boolean("Specimen Order", store=False),
I want to set its default value from xml code so that it doesn't get affected in default form view. I have tried in four ways,
1)<field name="is_sample" eval="True"/>
2)<field name="is_sample" domain="[('is_sample','=',True)]"/>
3)<record id="action_specimen_orders" model="ir.actions.act_window">
<field name="type">ir.actions.act_window</field>
<field name="context">{'is_sample': 'True'}</field>
...
4)<record id="action_specimen_orders" model="ir.actions.act_window">
<field name="type">ir.actions.act_window</field>
<field name="domain">[('is_sample','=','True')]</field>
...
There is an easiest way to set default value from the xml, generally you can it for all those fields of the model. For that you need to add dictionary key/value pair to set default value for any field.
General syntax
default_field_name : default_value
To set default value you need to pass context with window action in which you need to set one key/value pair in context as shown in syntax. default_ is the prefix which needs to be set with field name as key.
Try following:
<record id="action_specimen_orders" model="ir.actions.act_window">
<field name="type">ir.actions.act_window</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="context">{'default_is_sample': True}</field>
</record>
If you want your product must be available in POS, you have to do like:
<record id="module_name.action_name" model="ir.actions.act_window">
<field name="name">My Menu</field>
<field name="res_model">object.name</field>
<field name="context">{'default_available_in_pos':1}
</field>
</record>

How to change context of model "ir.filters" in odoo

In parent module I have something likes :
<record id="filter_hr_timesheet_report_internal_timesheets" model="ir.filters">
<field name="name">Internal Timesheet</field>
<field name="model_id">hr.timesheet.report</field>
<field name="domain">[('state','=','done'), ('date','<=', time.strftime('%Y-%m-%d')),('date','>=',time.strftime('%Y-%m-01'))]</field>
<field name="user_id" eval="False"/>
<field name="context">{'group_by': ['user_id'], 'col_group_by': ['department_id'], 'measures': ['time']}</field>
</record>
and I want to change group_by in context field but can't find any document for model "ir.filters"
Update:
I resolved it by myself by :
<record id="hr_timesheet_sheet.filter_hr_timesheet_report_internal_timesheets" model="ir.filters">
<field name="context">{'group_by': ['employee_id'], 'col_group_by': ['department_id'], 'measures': ['time']}</field>
</record>
We can access record id with module name and we can update any attribute as per our requirement.
For example:
module_name.xml_record_id
Try with following code:
<record id="hr_timesheet_sheet.filter_hr_timesheet_report_internal_timesheets" position="attributes">
<attribute name="context">NEW CONTEXT WILL COME HERE</attribute>
</record>