Permission of ir.config_paramenter in odoo 12 - permissions

I got this problem. Why i get this access error and how can i fix it?
Odoo Server Error - Access Error
Sorry, you are not allowed to access
this document. Only users with the following access level are
currently allowed to do that:
Administration/Settings
(Document model: ir.config_parameter) - (Operation: read, User: 21)
Here is my code:
Button submit:
<button string="Confirm" name="button_submit" states="draft" type="object" class="oe_highlight"/>
My python code:
def send_email(self, subject, message_body, email_from, email_to):
template_obj = self.env['mail.mail']
template_data = {
'subject': subject,
'body_html': message_body,
'email_from': email_from,
'email_to': email_to
}
template_id = template_obj.create(template_data)
template_obj.send(template_id)
template_id.send()
#api.multi
def request_recuitment_send_mail(self):
""" Send mail with wizard """
base_url = request.env['ir.config_parameter'].get_param('web.base.url')
base_url += '/web#id=%d&view_type=form&model=%s' % (self.id, self._name)
subject = '''Request recuitment for {}'''.format(self.job_id.name)
message_body = '''
<div style="font-size: medium;">
Dear {},
Please check this link for more information Click here
'''.format(
self.user_id.name,
base_url,
)
email_from = '''HR Recruiment <{}>'''.format(self.approver_id.work_email)
email_to = self.user_id.email
self.send_email(subject, message_body, email_from, email_to)
#api.multi
def button_approve(self):
subject = "Request recruitment for {self.job_id.name} has been approved "
body = '''
Position Request: {}
Quantity of Position: {}
Department: {}
Expected Gross Salary: {}
'''.format(
self.job_id.name,
self.quantity,
self.department_id.name,
self.salary_cross_expected
)
self.env['mail.message'].create({'message_type': "notification",
"subtype": self.env.ref("mail.mt_comment").id,
'body': body,
'subject': subject,
'needaction_partner_ids': [(4, self.user_id.partner_id.id,)],
'model': self._name,
'res_id': self.id,
})
self.request_recuitment_approved_send_mail()
self.write({'state': 'approved'})

It should be safe to use sudo() in this case:
request.env['ir.config_parameter'].sudo().get_param('web.base.url')
"Normal" Users don't have any rights on model ir.config_parameter (System parameters). Only the admin (one of its default access groups) or the superuser can read such parameters.
About sudo([flag=True]) from the current documentation (Odoo 15):
Returns a new version of this recordset with superuser mode enabled or disabled, depending on flag. The superuser mode does not change the current user, and simply bypasses access rights checks.
IMPORTANT: I'm not completely sure when it was changed, but IIRC the "current user change" was removed since Odoo 13. So for Odoo 12 sudo will change the current user, which for example will have impacts on default values on creation, created message authors, and so on.
In your case that's irrelevant, because you're only getting the base url or the parameter value, and that's it.

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)

Odoo13: How to assign followers to a record via Controller

In my webform controller, I am attempting to assign email addresses entered in a webform to a given record. This is the snippet of code in my controller responsible for that
if 'followers' in request.params:
raw_emails = request.httprequest.form.get('followers').split(',')
emails = [user.strip() for user in raw_emails]
#emails = ['foo#bar.com', 'foo2#bar.com',..]
for email in emails:
follower = request.env['res.users'].search(
[('email', '=', email)])
if bool(follower):
reg = {
'res_id': new_ticket.id,
'res_model': 'helpdesk.ticket',
'partner_id': follower.id
}
request.env['mail.followers'].create(reg)
else:
message = "TO DO: Add {} to the system and make the user a follower of this ticket".format(
email)
new_ticket.message_post(body=message)
With this I get strange results i.e after entering "user A" as a follower on the webform, "user B" gets added as a follower. I'm thinking the problem might be from the wrong user record being loaded into the follower variable but I'm not seeing why. Any feedback would be really appreciated.
You can use the message_subscribe method to add followers to a record set.
def message_subscribe(self, partner_ids=None, channel_ids=None, subtype_ids=None):
    """ Main public API to add followers to a record set. Its main purpose is
    to perform access rights checks before calling _message_subscribe. """
You have already an example in the account move, in message_new method that add a list of partners.
# Assign followers.
all_followers_ids = set(partner.id for partner in followers + senders + partners if is_internal_partner(partner))
move.message_subscribe(list(all_followers_ids))

how to change the created by <user> in log to OdooBot when creating a record using odoo external API

I'm trying to create a new lead from external landing page
The code work as expected so far on Odoo 13.0+e-20200524
url = ODOO_URL
db = ODOO_DB
username = ODOO_USERNAME
password = ODOO_PASSWORD
kwargs = {
'name': 'hello world',
}
common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))
uid = common.authenticate(db, username, password, {})
print(uid)
models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))
id = models.execute_kw(db, uid, password, 'crm.lead', 'create', [{
'name': kwargs.get('name'),
'user_id': 1,
}])
print(id)
But the log of the lead showing that my user created that lead (which is properly right)
Change the created user to OdooBot in the view - screenshot
My question is:
How can I change the created user to OdooBot instead of my user?
PS: I already searched around and tried bellow parameters without luck:
'user_login': "OdooBot",
'create_uid': [1],
'write_uid': [1],
uid represent a key role of User to create a record using xmlrpc.
You can change uid and it will log with that User.

Odoo 10 XMLRPC How to map one2many and many2one

I have recently been doing some development in python 2.7 with Odoo 10 API using XMLRPC.
My questions are:
How do I write a one2many field to a field in odoo via xmlrpc
How do u write a many2one field to a field in odoo via xmlrpc
Many thanks your help is much appreciated!
Samuel
For Many2one fields you can simply use the ID of the record:
my_partner_id = 1 # or use a search to find the correct one
id = models.execute_kw(db, uid, password, 'sale.order', 'create', [{
'partner_id': my_partner_id,
}])
Many2many or One2many fields are a bit special. There are some magic triplets in Odoo, you have to use with such fields -> Model Reference/CRUD/write(vals).
For example if you want to add a tag to a customer (Many2many field):
my_tag_id = 42 # or use a search to find the correct one
id = models.execute_kw(db, uid, password, 'res.partner', 'write',
[my_partner_id], [{
'category_id': [(4, my_tag_id)],
}])
Or if you want to delete all tags:
my_tag_id = 42 # or use a search to find the correct one
id = models.execute_kw(db, uid, password, 'res.partner', 'write',
[my_partner_id], [{
'category_id': [(5,)],
}])
Or if you want to substitute all tags by some others:
my_tag_id1 = 42 # or use a search to find the correct one
my_tag_id2 = 7 # or use a search to find the correct one
id = models.execute_kw(db, uid, password, 'res.partner', 'write',
[my_partner_id], [{
'category_id': [(6, None, [my_tag_id1, my_tag_id2])],
}])
Create Activity (One2many field) in CRM form using Php API In #v11 Odoo Community:
$opportunity_id = 13; (Lead in which you create activity)
$user_id = 1; (User, for whom you assign task)
$c = $_POST["loading_time"]; (Deadline date which you have to set from php)
$enddate = date("Y-m-d H-i-s", strtotime($c));
$model = 'crm.lead';
$res_model_id = $models -> execute_kw($db, $uid, $password,
'ir.model', 'search', array(array(array('model', '=', 'crm.lead'))));
print_r($res_model_id);
$activity_type_id = $models -> execute_kw($db, $uid, $password,
'mail.activity.type', 'search', array(array(array('name', '=', 'Todo')))); (this is activity type like Todo,Call,Email,etc....)
print_r($activity_type_id);
$product_attribute_line = $models -> execute($db, $uid, $password,
'mail.activity', 'create',
array('model'= > $model,
'res_id'= > $opportunity_id,
'note'= > $_POST["question"],
'user_id'= > $user_id,
'date_deadline'=> $_POST["loading_time"],
'res_model_id'= > $res_model_id[0],
'summary'= > $_POST["subject"],
'activity_type_id'= > $activity_type_id[0],
'activity_ids'= > array(array(6, 0, array($opportunity_id))) ));
(activity_ids is a one2many field which will create activity)
Important:
To Create One2many field You must have to pass related many2one Id
You can see in image also by refer following image:
enter image description here

Odoo how to create or update record using XML

I have read Odoo documentation for creating new record. It uses XML RPC.
final Integer id = (Integer)models.execute("execute_kw", asList(
db, uid, password,
"res.partner", "create",
asList(new HashMap() {{ put("name", "New Partner"); }})
));
So is it possible to create new record only using XML message.
Thanks.
yes it is possible. here is the documenttation
and here is an example to create customer record using just a python file.
import xmlrpclib
username = 'admin' #the user
pwd = 'admin' #the password of the user
dbname = 'odoo' #the database
# Get the uid
sock_common = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')
uid = sock_common.login(dbname, username, pwd)
#replace localhost with the address of the server
sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/object')
partner = {
'name': 'atul arvind',
'phone': '8000111234'
}
partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner)
It will return newly created record's id.
hope it helps.