Can we use recursion in Odoo's function?
In my code below
def create_lines(self, item_id=None, parent_id=None):
source_items = self.env['product.source']
duplicate_items = self.env['product.duplicate']
recs = source_items.search([['parent_id', '=', item_id]])
for rec in recs:
value = { 'parent_id': parent_id,
'name': rec.name,
'date': rec.date,
'description': rec.description
}
line = duplicate_items.create(value)
self.create_lines(self, rec.id, line.id)
I'm getting SQLite objects created in a thread can only be used in that same thread
Why is this happening? And how can we enable recursion in Odoo?
It turned out the error happened because I'm using interactive python debugger ipdb.set_trace(); inside the recursion.
Also I need to correct my recursion like this
def create_lines(self, item_id=False, parent_id=False):
source_items = self.env['product.source']
duplicate_items = self.env['product.duplicate']
recs = source_items.search([['parent_id', '=', item_id]])
for rec in recs:
value = { 'parent_id': parent_id,
'name': rec.name,
'date': rec.date,
'description': rec.description
}
line = duplicate_items.create(value)
childs = source_items.search([['parent_id', '=', rec_id]])
if (len(childs)):
self.create_lines(self, rec.id, line.id)
So it doesn't recursive infinitely.
Related
I'm trying to update the body field from the payrun_chatter_log() but it won't update. any idea on how to do this? This is what I did:
def payrun_chatter_log(self):
subtype = self.env['mail.message.subtype'].search([('id','=', '2')])
vals = {
'date': fields.datetime.now(),
'email_from': self.env.user.email_formatted,
'author_id': self.env.user.id,
'message_type': 'notification',
'subtype_id': subtype.id,
'is_internal': True,
'model': 'custom.module',
'res_id': self.id,
'body': 'Test'
}
self.env['mail.message'].create(vals)
def custom_button(self):
chatter = self.env['mail.message'].search([('res_id', '=', self.id)])
message = 'Custom Message here'
chatter.update({'body': message})
return super(CustomModule, self).custom_button()
Your below line of code in custom_button will return multi record because res_id will be repeated for more than one model
chatter = self.env['mail.message'].search([('res_id', '=', self.id)])
You need to add the model to search:
chatter = self.env['mail.message'].search([('model', '=', self._name), ('res_id', '=', self.id)])
The message body should be updated, you need to refresh the page to see the changes.
Try to return the following client action:
return {
'type': 'ir.actions.client',
'tag': 'reload',
}
The chatter has an One2many relation to mail.message model. Your function will update all messages
I have an store procedure Oracle that returns a variable type CLOB with information in JSON format. That variable caught her in a Python
and I return it for a Web Service. The store procedure output is of this style:
{"role":"Proof_Rol","identification":"31056235002761","class":"Proof_Clase","country":"ARGENTINA","stateOrProvince":"Santa Fe","city":"Rosario","locality":"Rosario","streetName":"Brown","streetNr":"2761","x":"5438468,710153","y":"6356634,962204"}
But at the Python service exit it is shown as follows:
{"Atributos": "{\"role\":\"Proof_Rol\",\"identification\":\"31056235002761\",\"class\":\"Proof_Clase\",\"country\":\"ARGENTINA\",\"stateOrProvince\":\"Santa Fe\",\"city\":\"Rosario\",\"locality\":\"Rosario\",\"streetName\":\"Brown\",\"streetNr\":\"2761\",\"x\":\"5438468,710153\",\"y\":\"6356634,962204\"}"}
Does anyone know how to prevent the escape characters from entering that string at the exit of the service?
Part of my Python Code is:
api = Api(APP, version='1.0', title='attributes API',
description='Attibute Microservice\n'
'Conection DB:' + db_str + '\n'
'Max try:' + limite)
ns = api.namespace('attributes', description='Show descriptions of an object')
md_respuesta = api.model('attributes', {
'Attribute': fields.String(required=True, description='Attribute List')
})
class listAtriClass:
Attribute = None
#ns.route('/<string:elementId>')
#ns.response(200, 'Success')
#ns.response(404, 'Not found')
#ns.response(429, 'Too many request')
#ns.param('elementId', 'Id Element (ej:31056235002761)')
class attibuteClass(Resource):
#ns.doc('attributes')
#ns.marshal_with(md_respuesta)
def post(self, elementId):
try:
cur = database.db.cursor()
listOutput = cur.var(cx_Oracle.CLOB)
e, l = cur.callproc('attributes.get_attributes', (elementId, listOutput))
except Exception as e:
database.init()
if database.db is not None:
log.err('Reconection OK')
cur = database.db.cursor()
listOutput = cur.var(cx_Oracle.CLOB)
e, l = cur.callproc('attributes.get_attributes', (elementId, listOutput))
print(listOutput)
else:
log.err('Conection Fails')
listOutput = None
result = listAtriClass()
result.Attribute =listOutput.getvalue()
print(result.Attribute)
return result, 200
Attribute is defined to render as a fields.String but actually it should be defined to render as fields.Nested.
attribute_fields = {
"role": fields.String,
"identification": fields.String,
"class": fields.String,
# ...you get the idea.
}
md_respuesta = api.model('attributes', {
'Attribute': fields.Nested(attribute_fields)
})
Update for flask-restplus
In flask-restplus, a nested field must also register a model.
attribute_fields = api.model('fields', {
"role": fields.String,
"identification": fields.String,
"class": fields.String,
# ...you get the idea.
})
Another way is to inline attribute_fields instead of registering a separate model for it.
md_respuesta = api.model('attributes', {
'Attribute': {
'role': fields.String,
'identification': fields.String,
'class': fields.String,
# ...you get the idea.
}
})
Hopefully a pretty easy questions follows. When I get an item with Pyrebase's .get() method, like so:
for company_id in game[company_type]:
pyre_company = db.child("companies/data").order_by_child("id").equal_to(company_id).limit_to_first(
1).get()
company = pyre_company.val()
print(company)
break # Run only once for testing purposes
I get this following output, even though I use the .val()
OrderedDict([('-LEw2zHYiJ6p15iBhKuZ', {'id': 427, 'name': 'Bugbear Entertainment', 'type': 'developer'})])
But I only want the JSON Object
{'id': 427, 'name': 'Bugbear Entertainment', 'type': 'developer'}
This is because
db.child("companies/data").order_by_child("id").equal_to(company_id).limit_to_first(1).get()
is a Query, because you call the orderByChild() method on a Reference (as well as an equalTo() method btw).
As explained here in the JavaScript SDK doc:
Even when there is only a single match for the query, the snapshot is still a
list; it just contains a single item. To access the item,
you need to loop over the result:
ref.once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
// ...
});
});
With pyrebase you should use the each() method, as explained here, which "Returns a list of objects on each of which you can call val() and key()".
pyre_company = db.child("companies/data").order_by_child("id").equal_to(company_id).limit_to_first(1).get()
for company in pyre_company.each():
print(company.val()) // {'id': 427, 'name': 'Bugbear Entertainment', 'type': 'developer'}
In Python Eve framework, is it possible to have a condition which checks combination of two fields to be unique?
For example the below definition restricts only firstname and lastname to be unique for items in the resource.
people = {
# 'title' tag used in item links.
'item_title': 'person',
'schema': {
'firstname': {
'type': 'string',
'required': True,
'unique': True
},
'lastname': {
'type': 'string',
'required': True,
'unique': True
}
}
Instead, is there a way to restrict firstname and lastname combination to be unique?
Or is there a way to implement a CustomValidator for this?
You can probably achieve what you want by overloading the _validate_unique and implementing custom logic there, taking advantage of self.document in order to retrieve the other field value.
However, since _validate_unique is called for every unique field, you would end up performing your custom validation twice, once for firstname and then for lastname. Not really desirable. Of course the wasy way out is setting up fullname field, but I guess that's not an option in your case.
Have you considered going for a slighty different design? Something like:
{'name': {'first': 'John', 'last': 'Doe'}}
Then all you need is make sure that name is required and unique:
{
'name': {
'type':'dict',
'required': True,
'unique': True,
'schema': {
'first': {'type': 'string'},
'last': {'type': 'string'}
}
}
}
Inspired by Nicola and _validate_unique.
from eve.io.mongo import Validator
from eve.utils import config
from flask import current_app as app
class ExtendedValidator(Validator):
def _validate_unique_combination(self, unique_combination, field, value):
""" {'type': 'list'} """
self._is_combination_unique(unique_combination, field, value, {})
def _is_combination_unique(self, unique_combination, field, value, query):
""" Test if the value combination is unique.
"""
if unique_combination:
query = {k: self.document[k] for k in unique_combination}
query[field] = value
resource_config = config.DOMAIN[self.resource]
# exclude soft deleted documents if applicable
if resource_config['soft_delete']:
query[config.DELETED] = {'$ne': True}
if self.document_id:
id_field = resource_config['id_field']
query[id_field] = {'$ne': self.document_id}
datasource, _, _, _ = app.data.datasource(self.resource)
if app.data.driver.db[datasource].find_one(query):
key_names = ', '.join([k for k in query])
self._error(field, "value combination of '%s' is not unique" % key_names)
The way I solved this issue is by creating a dynamic field using a combination of functions and lambdas to create a hash that will use
which ever fields you provide
def unique_record(fields):
def is_lambda(field):
# Test if a variable is a lambda
return callable(field) and field.__name__ == "<lambda>"
def default_setter(doc):
# Generate the composite list
r = [
str(field(doc)
# Check is lambda
if is_lambda(field)
# jmespath is not required, but it enables using nested doc values
else jmespath.search(field, doc))
for field in fields
]
# Generate MD5 has from composite string (Keep it clean)
return hashlib.md5(''.join(r).encode()).hexdigest()
return {
'type': 'string',
'unique': True,
'default_setter': default_setter
}
Practical Implementation
My use case was to create a collection that limits the amount of key value pairs a user can create within the collection
domain = {
'schema': {
'key': {
'type': 'string',
'minlength': 1,
'maxlength': 25,
'required': True,
},
'value': {
'type': 'string',
'minlength': 1,
'required': True
},
'hash': unique_record([
'key',
lambda doc: request.USER['_id']
]),
'user': {
'type': 'objectid',
'default_setter': lambda doc: request.USER['_id'] # User tenant ID
}
}
}
}
The function will receive a list of either string or lambda function for dynamic value setting at request time, in my case the user's "_id"
The function supports the use of JSON query with the JMESPATH package, this isn't mandatory, but leave the door open for nested doc flexibility in other usecases
NOTE: This will only work with values that are set by the USER at request time or injected into the request body using the pre_GET trigger pattern, like the USER object I inject in the pre_GET trigger which represents the USER currently making the request
I have written code to migrate data from sql server 2008 to PostGreSQL using OpenERPLib in Python for OpenERP. I want to set the value of "categ_id" column of type "Many2one" of "crm.opportunity2phonecall" object. Here below is my existing code.
scheduleCall = {
'name': 'test',
'action': ['schedule'],
'phone': "123456",
'user_id': 1,
"categ_id": 10,
'note': mail['body']
}
SCHEDULECALL_MODEL.create(scheduleCall)
SCHEDULECALL_MODEL = OECONN.get_model("crm.opportunity2phonecall")
In the above code i have set the hard-coded value "10" for "categ_id" field as per my requirement. When i execute above code, it gives me an error -
TypeError: unhashable type: 'list'
Try assigning a list instead of an integer as follows:
categ_id: [10]
anyway, as Atul said in his comment, update OpenERP with xmlrpc, it is safe and stable, and suppports different versions of OpenERP
Okay, I got the solution.
What i had done is - define one method in python which returns categ_id and set its value in "scheduleCall" dict and surprisingly its work. Here is my code.
scheduleCall = {
'name': 'test',
'action': ['schedule'],
'phone': "123456",
'user_id': 1,
"categ_id": get_categid_by_name('Outbound'),
'note': mail['body']
}
SCHEDULECALL_MODEL.create(scheduleCall)
SCHEDULECALL_MODEL = OECONN.get_model("crm.opportunity2phonecall")
And here is the method that i had define.
def get_categid_by_name(name):
"""return category id"""
categ_id = False
ids = CATEG_MODEL.search([('name', '=', name)])
categ_id = ids[0]
return categ_id
CATEG_MODEL = OECONN.get_model("crm.case.categ")
Hope it'll help to others.