Stop creating mail message and Followers in perticular model in odoo - odoo

When I create new sale order,it also creates the mail messages and followers at bottom of form view.
But I want to make that system should not create mail message and followers while users creates or writes data of "sale.order" model.
How can I stop such message and followers creation .?

Just need to apply context in create and write method of that model.
#api.model
def create(self,vals):
res=super(sale_order,self.with_context({'mail_create_nosubscribe':True,'tracking_disable':True})).create(vals)
return res
#api.multi
def write(self,vals):
res=super(sale_order,self.with_context({'mail_create_nosubscribe':True,'tracking_disable':True})).write(vals)
return res

If you just want to remove it from the view then override the template and remove the fields named:
message_follower_ids
message_ids
These two fields are responsible for the view part.
Code to remove the fields:
<xpath expr="//field[#name='message_follower_ids']" position="replace"/>
Will this be fine or should I tell you how to update the record at model level?

Related

How to store logged user in Odoo14

I am trying to store the currently logged user into a many2one field using compute method. It's working fine if i define the Mnay2one field without the store="True" parameter. Actually, i need to save it.
Here is the code:
def get_logged_user(self):
for rec in self:
print('inside get_logged_user---------------',rec.env.user.name)
rec.logged_user_id = rec.env.user.id
logged_user_id = fields.Many2one('res.users',string="Logged user",store=True,compute="get_logged_user")
EDIT:
If you only need to control visibility of a field/button inside QWeb view you could archive this without dedicated field. You could use context.get('uid') to get current user like this:
<button ... invisible="context.get('uid') == assigned_user_id">
But if you need to store logged-in user inside a field you could use default instead of compute.
Something like this:
logged_user_id = fields.Many2one('res.users', string="Logged user", default=lambda self: self.env.user)
Note usage of lambda function.
If you really need to use compute field with store=True you need to specify when to compute it. By using api.depends decorator you can trigger it when your_field is changed.
#api.depends('your_field')
def get_logged_user(self):
But I would ask a question why do you need to store logged-in user inside a field? If you could provide more context maybe we could suggest different solution.

How to disable "You have been assigned" email when a user creates a sales order in Odoo 10

In Odoo 10, when user "A" creates a new sales order and assigns it to a different salesperson (user "B"), no matter what configuration you have applied to email templates/subtypes/send notifications, an email is automatically sent to the customer and the salesperson (I am still amazed on which business logic was follow to send internal notification emails to customers by default).
The email is the well known one with this format:
"You have been assigned to SOxxxx."
To make things worse the email is set to "Auto-delete", so you do not even know what your system is sending to customers (no comments).
Which module or function or setting in Odoo 10 CE shall be overwritten to avoid such default behaviour?
Overwrite _message_auto_subscribe_notify method for sale.order class and add to context mail_auto_subscribe_no_notify.
from odoo import models, api
class SaleOrder(models.Model):
_inherit = 'sale.order'
#api.multi
def _message_auto_subscribe_notify(self, partner_ids):
""" Notify newly subscribed followers of the last posted message.
:param partner_ids : the list of partner to add as needaction partner of the last message
(This excludes the current partner)
"""
return super(SaleOrder, self.with_context(mail_auto_subscribe_no_notify=True))\
._message_auto_subscribe_notify(partner_ids)
The original method will not send the message if that key is passed in the context
#api.multi
def _message_auto_subscribe_notify(self, partner_ids):
""" Notify newly subscribed followers of the last posted message.
:param partner_ids : the list of partner to add as needaction partner of the last message
(This excludes the current partner)
"""
if not partner_ids:
return
if self.env.context.get('mail_auto_subscribe_no_notify'): # Here
return
# send the email only to the current record and not all the ids matching active_domain !
# by default, send_mail for mass_mail use the active_domain instead of active_ids.
if 'active_domain' in self.env.context:
ctx = dict(self.env.context)
ctx.pop('active_domain')
self = self.with_context(ctx)
for record in self:
record.message_post_with_view(
'mail.message_user_assigned',
composition_mode='mass_mail',
partner_ids=[(4, pid) for pid in partner_ids],
auto_delete=True,
auto_delete_message=True,
parent_id=False, # override accidental context defaults
subtype_id=self.env.ref('mail.mt_note').id)
If this should only be disabled for SaleOrder which are generated through custom code (e.g. an developed API endpoint), you could use the with_context() method on each model:
sale_order = {
'partner_id': partner['id'],
'state': 'sent',
'user_id': 6,
'source_id': 3,
'currency_id': currency['id'],
'payment_term_id': payment_term['id'],
}
created_sale_order = request.env['sale.order'].with_context(mail_auto_subscribe_no_notify=True).create(sale_order)
In my example, the user with the ID 6 does not get the notification about the assignment of this sale order.

Batch create based on user selection in Odoo

I have the following models Program, Course, ProgramAdmission, CourseEnrollment.
Each course is associated with a program.
During program admission, I am showing the list of available courses for the selected program. For each shown course, I want to show a dropdown menu with the following selections: Not Planned and Planned.
Now if the user saves the new program admission, I want also to enroll the user in the planned courses by creating CourseEnrollment in the server-side for each planned course.
And if the user discards the new program admission, nothing should be created in the database.
How can I allow for such conditional batch creation of model objects?
Thank you!
It's easy, you just need to start to coding it. Create the module with the models relations and fields. On the ProgramAdmission model add a Many2one field to the Program model and another to the Course model. If you cancel the form while creating a new record nothing will be saved in your DB, but if you hit the Save button it will call the create method for new records and write methods for existing ones. Override the create method to be able to dynamically create a new record of the CourseEnrollment model and associate it with the ProgramAdmission record to be created by saving it to a new Many2one field in the same ProgramAdmission model.
To make what I mean more clear:
from odoo import models, fields, api
class Program(models.Model):
_name = 'school.program'
name = fields.Char()
class Course(models.Model):
_name = 'school.course'
name = fields.Char()
class ProgramAdmission(models.Model):
_name = 'school.program.admission'
course_id = fields.Many2one('school.course')
program_id = fields.Many2one('school.program')
enrollment_id = fields.Many2one('school.course.enrollment')
#api.model
def create(self, vals):
enrollment_id = self.env['school.course.enrollment'].create({
'course_id': vals['course_id'],
'program_id': vals['program_id']
})
vals['enrollment_id'] = enrollment_id.id
return super(ProgramAdmission, self).create(vals)
class CourseEnrollment(models.Model):
_name = 'school.course.enrollment'
course_id = fields.Many2one('school.course')
program_id = fields.Many2one('school.program')

How to send multiple values in many2many field in odoo?

I am trying to generate purchase order from manufacturing order.I have created many2many field for getting multiple products.I want to send multiple product ids to my custom function.I am able to send 1 value but sending more than one gives error as Expected singleton: product.template(4, 3).
from openerp import models,fields,api
class generate_purchase_order(models.Model):
_name = 'mrp_to_purchase_order'
product_id = fields.Many2many('product.template',string='Products',required=True)
#api.multi
def generate_purchase_order2(self):
for wizard in self:
mrp_obj = self.env['mrp.production']
mrp_obj.generate_purchase_order(wizard.product_id.id) #function call
return { 'type': 'ir.actions.act_window_close'}
mrp_custom.py,
from openerp import models,api
class mrp_production(models.Model):
_inherit = 'mrp.production'
#api.multi
def generate_purchase_order(self,product_id):
purchase_line_obj = self.env['purchase.order.line']
context = self._context
for order in self.browse(context['active_ids']):
for line in order.bom_id.bom_line_ids:
if line.product_id.id != product_id:#problem line
continue
#rest of code
singleton: product.template(4, 3)
This error means that code is expecting single record not recordset, so you must change code to allow recordset using or make ensureone with try-catch and avoid errors. Thats general information.
Now if you want to get multiple records from many2many its not problem at all, you must pass this many2many object only and then work with it.
After getting many2many object to work with every record from this recordset you must use for record in recordset:
Also wizard.product_id.id this is error!!! product_id is many2many so you must pass product_id or if you want to browse by yourself you must pass product_id.ids

Generate a code with a wizard on openERP 7

i'm new to openERP and I hadn't found an exhaustive and simple guide for wizards.
I have to do a wizard that generates a code by using the product_id.
This wizard have to generate the code of all the products when i click on it and put it in in the field EAN13. I have no idea how to create the wizard that take the code, generates his own code and put it in the field.
Sorry for my bad english :(
You can check OpenERP Technical Memento
You need to create a new memory model
class ean13_wiz(osv.osv_memory):
_name = 'ean13.wiz'
_description = 'EAN13 wizard'
_columns = {
'ean_template':fields.char('ean_template', size=13, required=True),
}
_defaults = {
'ean_template': '2100000000000',
}
def ean13_logic(self, cr, uid, ids, context=None):
# your duplicate buziness logic
...
I added only 1 field for init the ean13 template
When you click on the submit button you should add the view xml action to your ean13_logic to add a ean to every product. maybe have some feedback how many are changed.
look at the link for more info: wizard example