onchange method update virtual field's value - odoo

I am working on odoo v8 and I am trying to modify the access rights view in users form. I want to modify the form so that when I select a role for each module's category it will call the onchange method and update the checkboxes below (Technical Settings, Usability and Other). Currently changes made will only appear after I saved the form. But I want to make it update onfly for administrator to verify before saving into the database. But it seems when I return a dict from the onchange method the system is not aware the existence of the field (eg virtual fields like in_group_1, in_group_2 etc). Is there anyway to do this?
#api.v7
def check_acl(self, cr, uid, ids, my_field, context=None):
return {'value': {'in_group_1': True}}

You can check is administrator or not by this function
def check_user(self, uid=None):
if uid is None:
uid = request.uid
is_admin = request.registry['res.users']._is_admin(request.cr, uid, [uid])
if not is_admin:
raise openerp.exceptions.AccessError("Only administrators can upload a module")
and call this function with arguments like:
uid = request.session.authenticate(request.db, login, password)
self.check_user(uid)
and you can use this function in your onchange
#api.v7
def check_acl(self, cr, uid, ids, my_field, context=None):
if self.check_user(uid):
return {'value': {'in_group_1': True}}

Related

In Odoo, how do I override _file_write in the ir_attachment class?

In the Odoo 9 source code the class ir_attachment has the following comment:
The 'data' function field (_data_get,data_set) is implemented using
_file_read, _file_write and _file_delete which can be overridden to
implement other storage engines, such methods should check for other
location pseudo uri (example: hdfs://hadoppserver)
It tells me I can over ride the read, write and delete methods, but I have not been able to find any documentation on how to do so.
I tried overriding like I would other Odoo modules by creating an module with this code:
class Attachments(osv.osv):
_inherit = 'ir.attachment'
def _file_read(self, cr, uid, fname, bin_size=False):
r = super(Attachments, self)._file_read(cr, uid, fname, bin_size)
return r
def _file_write(self, cr, uid, value, checksum):
name = super(Attachments, self)._filewrite(self, cr, uid, value, checksum):
return fname
However, I set several breakpoints and it appears Odoo is not registering these function overrides. Is there a different way to override methods in runtime directory?
See this github project for a complete and working example: https://github.com/tvanesse/odoo-s3

Change Access Rights to all the User at a Time in Odoo

Now i have more than 300 Users in my system. For all of them by default Human Resources : Employee is there. Now, i want to remove that Access to all of them.
I tried to access from database, but no use.
How can i do that?
Try this,
from openerp import SUPERUSER_ID
#api.multi
def update_all_users_hr_employee_access(self):
category_id=self.pool.get('ir.module.category').search(cr,uid,[('name','=','Human Resources')],context=context)
group_ids=self.pool.get('res.groups').search(cr,uid,[('category_id','=',category_id[0]),('name','=','Employee')],context=context)
group_obj = self.pool.get('res.groups').browse(cr, uid,group_ids[0],context=context)
write_op = [(3, user.id) for user in group_obj.users if user.id!=SUPERUSER_ID]
group_obj.write(cr, uid, [group_obj.id], {'users': write_op}, context=context)
return True

Where to write new method of python. Either in existing .py files or I need to create a new one?

I am going to write a method in point of sale containing existing .py files. Should I create new python file ? or write new method in existing .py files??
If you need to add a new method to a particular model (e.g. sale.order), then inherit that particular model and add your method in a separate module i.e. custom module.
class SaleOrder(models.Model):
_inherit='sale.order'
#api.multi
def custom_test_method(self...)
Note:
This is in order to migrate to new version or update your code from github. Mostly, any modification to your model needs to be done in a custom module only.
Never change the code in the base module or the module's not written by you. Because when transition to update the latest code in order to get new functionalities or migration to another version, there is a plenty of chances for code loss and results to weird behaviour.
Use custom module for new method or overwriting the existing method
Eg: To add new method in pos module's, model "pos.order":
class pos_order(orm.Model):
_inherit = "pos.order"
def your_new_method(self, cr, uid, ids, args, context=None):
## your code
return
For existing method:
class pos_order(orm.Model):
_inherit = "pos.order"
def your_existing_method(self, cr, uid, ids, args, context=None):
res = super(pos_order, self).your_existing_method(cr, uid, ids, args, context=context)
## your code to change the existing method result
return res

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.....

How to retrieve the current user in OpenERP module

I'm developing an OpenERP 7 module and I need to add a field that logs the user who created each record. How do I retrieve the current user object?
this kind of field is already available in openerp, as create_uid and write_uid.
In OpenERP Python code, functions generally take cr, the database pointer, and uid, the user id, as arguments. If all you need is the id of the current res.users object (for instance, to write into the one2many field), you can use uid as is. If you need to access the object (to see fields, etc.), something like:
current_user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
should work.