Odoo: Set default team of a module from settings page - odoo

Having created a view on the res.config page, I implemented a many2one field which lists the set of existing teams on the Helpdesk Module. What I would like to do is set the default helpdesk team upon the creation of a ticket automatically (By default this is "Customer Care"). So basically, the team the user chooses in my custom field on the res.config view would be the default helpdesk team. What would be the best way to implement this?

this could be easily achieved using the context in the action:
<field name="context">{'default_team_id': active_id}</field>
or you could do it programmatically (not advisable) using lambda default function as following:
team_id = fields.Many2one(comodel_name='helpdesk.team', default=_default_team_id)
def _default_team_id(self):
team_id = self.env.ref('my_module.best_team_ever').read()[0]
return team_id
# update using configuration would be:
def _default_team_id(self):
get_param = self.env['ir.config_parameter'].sudo().get_param
default_team = get_param('helpdesk.my_custom_default_team')
return default_team

Related

Restrict write permissions for a field - Odoo 15

How can I restrict the write permissions for a field to a specific group ?
I want to check if a user is in a specific group with id 46. If the user is in this group, he should be allowed to write in this field. If he is not in this group, he should not be allowed to write.
The field is a custom field, editing the domain with the studio app I think I should avoid.
My field:
<field name="customer_codename" placeholder="Codename" attrs="{'invisible':['|',('customer_rank','=', 0),('is_company','=', False)]}"/>
I tried the following, but it did not work:
I created a new field using the studio app. Field type is boolean.
In the advanced properties I wanted to define the compute for the field. In dependencies I gave "user_id" and in the compute field I gave
for record in self:
user_id.has_group('__export__.res_groups_46_eff9dc52')
The boolean field should be set to true if the user is in a certain group.
Not sure if I can give you the best answer there is.
But for me, I'd personally create a Boolean field in the view's associated model, with its default field a lambda function checking if the user belongs to the groups you mentioned.
Assuming groups_id is the name of the user groups in model res.users, we have:
class ResUsers(models.Model):
_inherit = "res.users"
can_write_codename = fields.Boolean(default=lambda self: self.groups_id in ("model_name.group_name"))
Then in your xml file, you can include can_write_codename inside attrs, like this:
<field name="customer_codename" placeholder="Codename" attrs="{'invisible':['|',('customer_rank','=', 0),('is_company','=', False)], 'readonly': [('can_write_codename', '=', 'True')]}"}"/>

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.

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')

Openerp Domain Filter Help on Tasks associated to Sales Team

I have been developing a lot of modules and implementing openerp for a wild. But I am stuck in a functional implementation.
I have installed the module crm_todo, it is for have tasks on crm, this module add "My Tasks" menu under Sales.
I need to create a new Menu with a Domain Filter called "Department Tasks" where will show all the task to all the members of an specific Sales Team. The task is assigned to User A; User A belongs to Sales Team A; Sales Team A has 2 more members. This new Menu have to list the task Assigned to User A to all the members of Sales Team A.
I was trying to do with field.function, but something is wrong. I am trying to apply the domain on act_window using the openerp Action Windows Menu and assigning it to the new Menu.
Specifying the login user sale team as domain parameter is not possible but there is another way in which we can achieve this. ie; in my view action i specify the domain as:
<field name="domain">[('user_id.default_section_id', 'in', user_sale_team())]</field>
where user_id is the responsible user of the task.
Now inherit read function of ir.actions.act_window and check if user_sale_team() is present in the domain of read result and replace this with the login user sale team id. This can be done as:
class ir_action_window(osv.osv):
_inherit = 'ir.actions.act_window'
def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
user_pool = self.pool.get('res.users')
obj_user = user_pool.browse(cr, uid, uid, context=context)
res = super(ir_action_window, self).read(cr, uid, ids, fields=fields, context=context, load=load)
if not isinstance(res, list):
res = [res]
sale_team_id = obj_user.default_section_id and obj_user.default_section_id.id or''
for r in res:
mystring = 'user_sale_team()'
if mystring in (r.get('domain', '[]') or ''):
r['domain'] = r['domain'].replace(mystring, str([sale_team_id]))
if isinstance(ids, (int, long)):
if res:
return res[0]
else:
return False
return res
ir_action_window()
This filters the result of the task to be displayed to each user based on his/her sale team.
Hope this helps.....

Openerp 7 many2one dropdown should display field of related record

If you install Openerp 7 with recruitment module. And create a simple entry with following values e.g.
Subject (internal field name = 'name') = 10 Year Experience
Applicant Name = Jhon Smith
Then if you create a custom module with following columns
_columns = {
'applicant_id': fields.many2one('hr.applicant', 'Applicant', required=True),
}
The view widget by default will show a drop-down with the Subject (internal field name ='name') field but i want to show applicant name (internal field name='partner_name') field in drop down, when creating a new record in my custom module.
In Summary how can I display Applicant's Name instead of Subject in drop-down widget in my custom module.
In openerp there is a function called name_get().This function returns a list of tuples containing ID of the record and name tobe displayed. So override this function and return list of tuples containing ID of the record and applicant name
You need to define applicant_id in _rec_name in your custom module.
Try this:
_rec_name = 'applicant_id'
Have a look at Predefined fields.