Fold or hide Kanban-stage dependent on parameter in context - odoo

Does anybody have experience with the following problem or any idea how to solve it:
I want to fold or hide specific Kanban-stages dependent on a Boolean parameter in the context.

You can try with invisible attribute and get value from the context.
invisible="context.get('boolean_field', False)"
JFI, I haven't tried it in kanban fold / hide but this logic works elsewhere. For example, field, span, t, div, qweb template etc.

Stage field is a selection field of Odoo. So you can not display its value base on condition but you can display default at some stage like statusbar_visible="stage1,stage2,stage5".

I found a working solution to completely hide kanban stages depending on the context (here: job_id). This function from hr.applicant does the trick:
stage_id = fields.Many2one(
group_expand='_read_group_stage_ids')
#api.model
def _read_group_stage_ids(self, stages, domain, order):
# retrieve job_id from the context and write the domain: ids + contextual columns (job or default)
job_id = self._context.get('default_job_id')
search_domain = [('job_ids', '=', False)]
if job_id:
search_domain = ['|', ('job_ids', '=', job_id)] + search_domain
if stages:
search_domain = ['|', ('id', 'in', stages.ids)] + search_domain
stage_ids = stages._search(search_domain, order=order, access_rights_uid=SUPERUSER_ID)
return stages.browse(stage_ids)

Related

Getting id from URL to filling wizard form, Odoo

I want to get id from url when i click the button.
This is URL, id is 69:
http://localhost:8069/web#id=69&cids=1&menu_id=385&action=507&model=inventory.menu&view_type=form
I need to get this id in many2one field.
This is my wizard py file:
from odoo import api,fields,models
class ReduceInventoryWizard(models.TransientModel):
_name = "reduce.inventory.wizard"
_description = "Reduce Inventory Wizard"
inventory_ids = fields.Many2one('inventory.menu', string="Ürün Referans No: ", default=lambda self: self.env['inventory.menu'].search([('id', '=', 69)], limit=1))
As you can see, ('id', '=', 69) this is running but just one product. I want the information of that product to come automatically when I click the button in which product.
I tried this one: ('id', '=', self.id). But is not working.
In this situation there should be active_id or better active_ids in Odoo's context.
So you just can set the default parameter to use a default method, which will either return a value or an empty recordset:
def default_my_many2one_field_id(self):
active_ids = self.env.context.get("active_ids")
if active_ids:
return self.env["another.model"].browse(active_ids[0])
return self.env["another.model"]
my_many2one_field_id = fields.Many2one(
comodel_name="another.model", string="My M2o Field",
default=default_my_many2one_field_id)

odoo 8 Count attachment and show in list view

I have odoo 8. I want to count the attachment from ir_attachment and and show it in stock.production.lot. Here is my .py
class stock_production_lot(models.Model):
_inherit='stock.production.lot'
#api.multi
def get_attachment_info(self):
for lot in self:
so_line_ids = self.env['ir.attachment'].search([('res_id','=',lot.id)])
for pick in so_line_ids:
pick.count_attachment = 0
if pick.datas_fname:
pick.count_attachment = len(pick.datas_fname)
count_attachment = fields.Float(string='Total Attachment', compute='get_attachment_info')
and this the view
<field name="count_attachment" />
Thanks
It's difficult to answer, because the information in your question are a bit poor. But let me try to answer it in a general way with a general example how i would do it.
Let's say you want a count of all attachments of model stock.picking (Delivery Nots, Slips, Receipts, and so on).
So you need to add a computed field, which could be stored, but it's difficult to trigger a recalculation of this field, because attachments are related to records indirectly (no real foreign keys used in database).
class StockPicking(models.Model):
_inherit = 'stock.picking'
attachment_count = fields.Integer(compute="_compute_attachment_count")
#api.multi
def _compute_attachment_count(self):
for picking in self:
domain = [('res_id', '=', picking.id),
('res_model', '=', self._name)]
picking.attachment_count = self.search_count(domain)
And also add the new field to a view of model stock.picking.
Now let's pretend that you don't only want the attachments of the pickings but also of their move lines, too.
Just "count" them and add that count to the previous one:
#api.multi
def _compute_attachment_count(self):
for picking in self:
domain = [('res_id', '=', picking.id),
('res_model', '=', self._name)]
picking_count = self.search_count(domain)
if picking.move_lines:
domain_move = [('res_id', 'in', picking.move_lines.ids),
('res_model', '=', picking.move_lines._name)]
picking_count += picking.move_lines.search_count(domain_move)
picking.attachment_count = picking_count
Thank you for the respone. I have got the answer
#api.one
def count_attachments(self):
obj_attachment = self.env['ir.attachment']
for record in self:
record.count_attachment = 0
attachment_ids = obj_attachment.search([('res_model','=',self._name),('res_id','=',record.id)])
if attachment_ids:
record.count_attachment = len(attachment_ids)

How to extend search record to also look at custom Many2Many field(s)?

In this image, there is a product search record that will search for name and default_code. I need to make it so that it will also look at my custom Many2Many field.
This is the field in the inherited model.
product_list = fields.Many2many("product.list", string="Product List")
The custom model only has _name, _description, and name variables.
The question is how to make the search to also look at all of the possible Many2Many data of this field.
I have tried this in the inherited model:
#api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
res = super(product_template_inherit, self).name_search(name='', args=None, operator='ilike', limit=100)
ids = self.search(args + [(name, 'in', 'product_list.name')], limit=limit)
if ids:
return ids.name_get()
return res
Nothing happens to the search. It still searches using the same behavior regardless of the code above.
Summary: I need to be able to search product by product list (custom Many2Many field inherited in the product.template model)
=============================================
UPDATE
Current code from what I have been trying is now this.
#api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
args = args or []
if operator == 'ilike' and not (name or '').strip():
domain = []
else:
domain = ['|', ('name', 'ilike', name), ('product_list.name', 'ilike', name)]
product_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return self.browse(product_ids).name_get()
However, it looks like it still searches using the same old fields. It does not change to behave as my function is written.
You can compute the search domain then return the result of the _search method.
The fleet module already uses the same logic to search vehicles using the driver name, you have just to replace the driver_id with product_list:
class ProductProduct(models.Model):
_inherit = 'product.product'
#api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
args = args or []
if operator == 'ilike' and not (name or '').strip():
domain = []
else:
domain = ['|', ('name', operator, name), ('product_list.name', operator, name)]
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)

how to get value of a field in fields_view_get?

I want to get a value of a field in fields_view_get method in openerp 7.0.
I tried the following:
1- send the value of the field in the context attribute as following:
< field name="employee_id" context="{'employee_id':employee_id}" />
and in the fields_view_get I get it as following:
print "employee_id in the context value is %s"%(context.get('employee_id', False))
but it always the the context.get(...) returns False. so I tried the following:
2- on the onchange method of the field I send the value of the field in the context as following:
def onchange_employee_id(self, cr, uid, ids, employee_id):
return {'context': {'employee_id': employee_id}}
and in the fields_view_get I get it as following:
print "employee_id in the context value is %s"%(context.get('employee_id', False))
but also the same thing always the context.get(..) returns False.
How can I get the value of a field in fields_view_get function ?
Maybe this answer is too late for you, but perhaps someone will find it useful.
If you need the dynamic view just on form view, you should write a tree view and you can put the selected record id to the context...so with the context id, you can read the fields.
But fields_view_get is not too easy. Dont forget about update the return dictionary (the two very important keys: fields, arch).
If you want to use invisible or readonly tag, you should use modifiers tag like attrs.
Example:
def fields_view_get(self, cr, uid, view_id=False, view_type='tree', context=None, toolbar=False, submenu=False):
fields = self.read(cr, uid, context['working_id'], [])
actualView = super(ModelName, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
# you can write default view in xml and dynamic complete with some field in this method
actualView['fields'].update({'field_name':{'type': 'text', 'string': 'Name'}})
arch = minidom.parseString(actualView['arch'])
#for example: triggered to <newline/> field
newlineField = arch.getElementByTagName('newline').item(0)
element = arch.createElement('field_name')
element.setAttribute('name', 'Name')
newlineField.insertBefore(element, 0)
actualView['arch'] = arch.toxml("utf-8")
return actualView

Openerp get partner category tags

I have 2 fields in my custom module:
'originator_id' : fields.many2one("res.partner",string="Originator", required=True),
'originator_category_ids' : fields.many2many('res.partner.category',
'module_category_rel',
'module_id',
'category_id',
'Categories'),
I want to set the domain for the many2many field "originator_category_ids" according to the selected "originator_id" which is a partner_id. I wrote an onchange method to define the domain dynamically:
def get_domain_originator_category_ids(self,cr,uid,ids,originator_id,context=None):
if originator_id:
obj = self.pool.get('res.partner').browse(cr, uid, originator_id)
return {'domain':{'originator_category_ids':[('id','in',obj.category_id)]}}
But above doesn't work.
Your support will be much appreciated.
This is worked for me, but it is a temporary solution until I find a better one. The solution consist on looping on categories and compare with the selected partner in the partner_ids field:
def get_domain_originator_category_ids(self,cr,uid,ids,originator_id,context=None):
category_obj = self.pool.get('res.partner.category')
category_ids = category_obj.search(cr, uid,[], context=context)
res=[]
for cateory in category_obj.browse(cr, uid, category_ids, context=context):
for partner_id in cateory.partner_ids:
if partner_id.id == originator_id:
res.append(cateory.id)
return {'domain':{'originator_category_ids':[('id','in',res)]}}
If you get a better solution please post it.