Warehouse stock restriction not working in edit mode odoo10? - odoo

In my custom module I use stock picking type to see the warehouse operations of the user.
class ResUsers(models.Model):
_inherit = 'res.users'
default_picking_type_ids = fields.Many2many(
'stock.picking.type', 'stock_picking_type_users_rel',
'user_id', 'picking_type_id', string='Warehouse Operations')
I added this in user form.
In my sexurity.xml file add
<record id="filter_user_stock_picking_type" model="ir.rule">
<field name="name">Filter Stock Picking Type</field>
<field name="model_id" search="[('model','=','stock.picking.type')]" model="ir.model"/>
<field name="groups" eval="[(4, ref('base.group_user'))]"/>
<field name="domain_force">[('id','in', [ s.id for s in user.default_picking_type_ids ])]</field>
</record>
so it is worked in when a user is created and assigns stock operation.
But when changed the stock operation to particular user, it will not affect.
How to resolve this issue??

I think, the problem is the Odoo Cache.
The old values are stored in the Cache for a long time and your change will not take any effect.
You can try it to clear the cache. It helped me to solve the similar problem.
class User(models.Model):
_inherit = 'res.users'
#api.multi
def write(self, vals):
ret = super(User, self).write(vals)
if 'default_picking_type_ids' in vals:
# clear default ir values when default_picking_type_ids changes
self.env['ir.values'].get_defaults_dict.clear_cache(self.env['ir.values'])
return ret

Related

Odoo write to field not part of the current view

I want to update the field done_date which is part of the project.task form view when pulling a Kanban tile on the stage that is indicated as being the final stage.
My code below works fine if the field is part of the Kanban view but fails to write if the field is only part of the task form view and not part of the Kanban project view.
The done_date field should be written even without being part of the Kanban view.
models.py
# -*- coding: utf-8 -*-
from odoo import models, fields, api
class project_set_end_date(models.Model):
inherit = 'project.task.type'
last_stage = fields.Boolean(string="Final stage")
class project_set_end_date(models.Model):
_inherit = 'project.task'
#api.onchange('stage_id')
def _set_end_date(self):
if self.stage_id.last_stage:
self.date_finished = fields.Datetime.now()
views.py
<odoo>
<data>
<!-- explicit list view definition -->
<record model="ir.ui.view" id="project_set_end_date">
<field name="name">project.task.type.form</field>
<field name="model">project.task.type</field>
<field name="inherit_id" ref="project.task_type_edit"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='fold']" position='after'>
<field name="last_stage"/>
</xpath>
</field>
</record>
</data>
</odoo>
Odoo doesn't write to a field that doesn't exist in the current view. So I suggest adding the field but with the attribute invisible = True to avoid showing it:
<field name="your_field" invisible="True"/>

Save value in Transient model Odoo 10

I added a new field in account.config.settings model. It displays the new field in settings page and can enter the value. But when i reopen the page the value is not there. I know the Transient model won't store values for long.
But rest of the values still there, how can i achieve this?
Below is my code.
*.py
class AccountSettings(models.TransientModel):
_inherit='account.config.settings'
#api.one
def _get_header(self):
header = fields.Char('Header')
*.xml
<record id="view_account_config_settings_inherit" model="ir.ui.view">
<field name="name">view.account.config.settings.inherit.form</field>
<field name="model">account.config.settings</field>
<field name="inherit_id" ref="account.view_account_config_settings"/>
<field name="arch" type="xml">
<xpath expr="//group[#name='accounting']" position="after">
<group string="Reports" name="reports">
<field name="header" class="oe_inline"/>
</group>
</xpath>
</field>
</record>
In account.config.settings Model you can save your value by using this :
class AccountSettings(models.TransientModel):
_inherit='account.config.settings'
header = fields.Char('Header')
#api.multi
def set_header_defaults(self):
return self.env['ir.values'].sudo().set_default(
'account.config.settings', 'header', self.header)
Try this code:
class AccountSettings(models.TransientModel):
_inherit='account.config.settings'
#api.one
def _get_header(self):
header = fields.Char('Header',config_parameter='header.configuration')
you can name the attribute config_parameter anything you want. And it will be used to get the value of header from other models.
example:
test = self.env['ir.config_parameter'].get_param('**header.configuration**', '').strip()
test will return the temporary stored value in account.config.settings.

odoov9 override old api onchange

I want in res.partner model make onchange event on field parent_id. this field already have onchange event by old api, so i changed it`s value on XML to on_change=1 and added .py code
def onchange_parent_id(self):
self.category_id = self.parent_id.category_id
super(res_partner, self).onchange_parent_id(self)
after update, then i open contact form and changing parent_id, everything works as i want, but when i press save error occurrs
TypeError: cannot convert dictionary update sequence element #0 to a sequence
as i understand it occurrs becouse on create other method (_fields_sync) also calls this onchange_parent_id methot by 'old style'.
Maybe someone can advice me how to fix this.
Thanks in advance
It seems that _fields_sync calls onchange_parent_id with context=[partner.id] and this is the raison of the conversion error (api.py line 756).
ODOO LOG:
WARNING ...: partner: res.partner(93,)
api:756:context=[93]
Python test:
>>> from openerp.tools import frozendict
>>> context = [93]
>>> frozendict(context)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot convert dictionary update sequence element #0 to a sequence
You can remove the old onchange event and create a new one (onchange_parent_id_new) without overriding onchange_parent_id method.
Python:
#api.model
#api.onchange('parent_id')
def onchange_parent_id_new(self):
old_res = super(res_partner, self).onchange_parent_id(self.parent_id.id)
if type(old_res) is dict and 'value' in old_res:
for field, value in old_res.get('value').items():
if hasattr(self, field):
setattr(self, field, value)
if self.parent_id:
self.category_id = self.parent_id.category_id
XML:
<record id="view_partner_form_inherit" model="ir.ui.view">
<field name="name">view.partner.form.inherit</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<field name="parent_id" position="replace">
<field name="parent_id"/>
</field>
</field>
</record>

New State in Openerp7

... In openerp7
I want my new state to show up in sale module ... But it is not appearing
Pls have a look at code below
In sale.py module I added state like:
class Sale_order(osv.Model):
_inherit = 'sale.order'
_columns = {
'state': fields.selection([
('draft', 'Draft Quotation'),
('my_new_state', 'My New State'),
('sent', 'Quotation Sent'),
('cancel', 'Cancelled'),
('waiting_date', 'Waiting Schedule'),
('progress', 'Sales Order'),
('manual', 'Sale to Invoice'),
('invoice_except', 'Invoice Exception'),
('done', 'Done'),
], 'Status', readonly=True, track_visibility='onchange',
help="Gives the status of the quotation or sales order. \nThe exception status is automatically set when a cancel operation occurs in the processing of a document linked to the sales order. \nThe 'Waiting Schedule' status is set when the invoice is confirmed but waiting for the scheduler to run on the order date.", select=True),
}
And in sale_view.xml, I added this piece of code..
<openerp>
<data>
<!-- Inherit the sale order model's form view and customize -->
<record id="sale_form_view" model="ir.ui.view">
<field name="name">sale.order.form.inherit</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<!-- Statusbar widget should also contain the new status -->
<field name="state" position="replace">
<field name="state" widget="statusbar" statusbar_visible="draft,my_new_state,sent,invoiced,done" statusbar_colors='{"invoice_except":"red","waiting_date":"blue"}'/>
</field>
</field>
</record>
</data>
But.... My new state is not appearing between draft quotation and quotation sent
Please guide
Why is this so
Thanks
First thing I would check is to make sure you are replacing the proper field in your view (if there are several instances of a field named "state" in the view you inherit, the wrong occurence could be replaced). Check the view by opening the 'Modify FormView' item in the developper tools/view.
If it is the wrong occurence you are replacing, you main need to change your view definition by using an xpath expression.
Second thing I would check, is make sure the sequence of your inherited view is smaller that the original view you are trying to replace/modify. You can check in 'Manage Views' item in the developper tools/view.
Third thing I would try is to rename your class from 'Sale_order' to 'sale_order' just to match the name of the original class your are trying to override.
Hope this helps.

One Module fields use in other module

I am trying to use some fields of "Job Positions" in "Opportunities" and we can say it as a merger fields. but i am unable to do this task . i have no knowledge about Python language.
I have created a user defined fields and use it in opportunities by XML coding by developer options . I know that it is easy because user defined fields has "crm.lead" named module which is same in opportunities.
but now When i want to use this fields in "hr.job" then it give me error "fields not found" . i know that this fields is not in current module and it is part of "crm.lead" not "hr.job".
Is it possible to use one module fields in another module?
Yes you can do so. First you have to create an object for that and then browse the record and fetch the value of the field which you want.
For example create one method and then browse the crm.lead record:
crm_obj = self.pool.get('crm.lead')
crm_brw = crm_obj.browse(cr, uid, crm_rec_id, context=context)
print "my field value:: ", crm_brw.your_field
Here, "crm_rec_id" is the id of the record of crm.lead object
There are lots of examples given in addons.
yes you can it done by _inherits.
for eg.
hr_job in hr module .
class hr_job(osv.osv):
_name = "hr.job"
_description = "job position"
_columns = {
'name': fields.char('job name', size=64)
}
crm_lead in crm module.
class crm_lead(osv.osv):
_name = "crm.lead"
_inherits = {'hr.job': 'job_id'}
_description = "Lead/Opportunity"
_columns = {
'partner_id': fields.many2one('res.partner', 'Partner')
}
in xml file of crm create form view.
<record id="crm_lead_form" model="ir.ui.view">
<field name="name">crm.lead</field>
<field name="model">crm.lead</field>
<field name="arch" type="xml">
<form>
<field name="name"/> # job name from hr_job
<field name="partner_id"/> # partner_id from crm.lead
</form>
</field>
</record>
don't forget add dependency.