class marcos_artwork(osv.osv):
def _check_appraisal_price(self, cr, uid, ids, context=None):
record = self.pool.get('appraisal_price')
if record<0:
return False
return True
"""artwork class"""
_name = 'marcos.artwork'
_columns = {
'name': fields.char('Artwork',size=32,required=True),
'description': fields.char('Description',size=200),
'appraisal_price': fields.integer('Appraisal price' ),
'createArtWork': fields.integer('Year of creation'),
'award': fields.boolean('Award'),
'barcode': fields.integer('Barcode'),
'commission': fields.integer('Commission',size=10),
'author_ids': fields.many2one('marcos.author', 'Author'),
'typeartwork_ids': fields.many2one('marcos.artwork_type', 'Artwork Type'),
'owner_ids': fields.many2one('marcos.owner','Owner'),
'style_ids': fields.many2one('marcos.style','Style'),
'lots_ids': fields.many2many('marcos.lots','artworks_lots_form_rel','id_artwork','id_lot','Artworks'),
}
_defaults = {
'award': lambda *a: False,
}
_sql_constraints = [
('name_uniqe', 'unique(name)', 'only equals name!'),
('barcode_uniqe', 'unique(barcode)', 'only equals barcode!')
]
_constraints = [(_check_appraisal_price, 'Error: Length must be Positive', ['appraisal_price'])]
marcos_artwork()
When I want to install the module, I get shown this error:
-cannot concatenate 'str' and 'function' object...
My function is for checking that the appraisal price is positive.
Can anybody help me?
your price checking won't work this way. the big problem here is the line
record = self.pool.get('appraisal_price')
that's not correct. self.pool.get will get you instances of your "business models" like your 'marcos.artwork' but you want some field values to check.
in this case you don't even need self.pool.get, because you're already on this model and can use self instead.
so here is the code you need (you will find so many examples in the addons):
def _check_appraisal_price(self, cr, uid, ids, context=None):
for artwork in self.browse(cr, uid, ids, context):
if artwork.appraisal_price < 0:
return False
return True
I don't know this specific framework. but I guess from the code It's some kind of "orm" style class. mapping some behavior to a db/table of some sort. Really hard to tell without the full stacktrace but I believe that the error is that your are passing the __check_appraisal_price function as reference and not the result of the function - that would be something like _check_appraisal_price(arg,arg2, etc)
Related
Please help me to solve this problem,
I have a function which used to check validation terms of the input and I wants to call it inside the "create"function as a nested function. Once I call it in normal way it keeps giving me parameter mismatching error. Please help me.
Inside the create function I call it like this
def create(self, cr, uid, values, context=None):
#There are 219 lines in side the create function but I just showing you the invoke for this particular fucntion.
date_from = values['date_from']
date_to=values['date_to']
sub_nominee= values['sub_nominee']
self.onchange_sub_nominee(self, cr, uid, date_to, date_from,sub_nominee)
return super(hr_holidays, self).create(cr, uid, values, context=context)
Function is like this,
def onchange_sub_nominee(self, cr, uid,ids, date_to, date_from,sub_nominee):
#Employees data
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
from_dt = datetime.datetime.strptime(date_from, DATETIME_FORMAT).date()
to_dt=datetime.datetime.strptime(date_to, DATETIME_FORMAT).date()
sub_name=self.pool.get('hr.employee').browse(cr, uid, sub_nominee).name
hol_obj=self.pool.get('hr.holidays')
#if date_from==date_to:
hol_objs=hol_obj.search(cr, uid, [('employee_id','=',sub_nominee),('type','=','remove'),('state','not in',['draft','refuse'])])
#nominees data
if hol_objs:
for a in hol_obj.browse(cr, uid, hol_objs):
sub_from_dt = a.date_to
sub_to_dt=a.date_from
no_days=a.number_of_days_temp
sub_half_day=a.half_day
sub_half_day_sts=a.half_day_status
f_dt = datetime.datetime.strptime(sub_from_dt, DATETIME_FORMAT).date()
t_dt=datetime.datetime.strptime(sub_to_dt, DATETIME_FORMAT).date()
if ((from_dt==to_dt)and(to_dt==f_dt==t_dt)):
raise osv.except_osv(_('Warning!'),_(' %s already on leave on %s . Please nominate another person 111')%(sub_name,from_dt))
if ((from_dt!=to_dt)) and(no_days<=2):
while from_dt <= to_dt :
new_con=self.search(cr, uid, [('date_from', '<=', date_to), ('date_to', '>=', date_from), ('employee_id', '=', sub_nominee)])
if new_con:
raise osv.except_osv(_('Warning!'),_(' %s has/have already on leave(s) on that period . 55555Please nominate another person')%(sub_name))
from_dt = from_dt + datetime.timedelta(days=1)
if ((f_dt!=t_dt)):
while t_dt <= f_dt:
if (from_dt==t_dt)or(to_dt==t_dt):
raise osv.except_osv(_('Warning!'),_(' %s has/have already on leave(s) on that period . Please nominate another person 2222')%(sub_name))
t_dt=t_dt+datetime.timedelta(days=1)
return True
The error is self explanatory. There is a mismatch in the invoking parameters.
onchange_sub_nominee method accepts parameters
cr, uid, ids, date_to, date_from,sub_nominee
but was invoked as,
self.onchange_sub_nominee(self, cr, uid, date_to, date_from,sub_nominee)
You need to add the value of the parameter id after uid when calling it or remove that parameter from onchange_sub_nominee definition.
I am new to python and OpenERP and I apologise if my question is inconvenient. But I am trying to add a control that check if a field is empty, but I am getting this error:
`if isinstance(res2[record['id']], str): res2[record['id']] = eval(res2[record['id']]) #TOCHECK : why got string instend of dict in python2.6`
Here is my function in hr_employee class:
def _sum_of_fields(self, cr, uid, ids, name, args, context=None):
res = {}
for employee_job in self.browse(cr, uid, ids, context=context):
if (employee_job.job_id == True):
for total in self.pool.get('hr.job').browse(cr, uid, ids, context=context):
res[total.id] = {
'sum': total.number_a + total.number_b,
}
else: {
'sum' : 0.00,
}
return res
Please could you help me? What the problem could be? Is my function OK? Thanks in advance
I realized that the problem was at the part:
else: {
'sum' : 0.00,
}
In order to work correctly you should do it in that way:
else:
for total in self.pool.get('hr.job').browse(cr, uid, ids, context=context):
res[total.id] = {
'sum': 0.00,
}
It works for me now. Also you should use this, to test if the field is empty or not(not the one in the code above) :
if empjob.job_id.id:
Hope this helps you
I've been trying to copy over certain fields from the hr_expenses form into my custom module, purchase_orders.
The below method was written for me to do this in the extended hr_expenses.py file:
def po_generator(self, cr, uid, ids, arg, context=None):
res = {}
for expense in self.browse(cr, uid, ids, context=context):
expense_line = self.pool.get('hr.expense.line')
purchase_orders = self.pool.get('purchase.orders')
for line in expense.line_ids:
expense_line_record = expense_line.search(cr, uid, [('id', '=', line.id)], context=context)
# pdb.set_trace()
purchase_orders.create(cr, uid,
{
'submitted_employee_name':expense.employee_id,
'dateofexpense':expense_line.date_value,
'project_id_expense':expense_line.project_id,
'Description':expense_line.name,
'netamount':expense_line.product_exc_VAT,
'raised_employee_name':expense.employee_id,
'project_id_account_type':expense_line.project_id_account_type,
},
context
)
return res
However, when I tru to execute this piece of code, OpenERP gives me an 'AttributeError' with the traceback:
File
"/opt/openerp/custom_modules/legacy_expense_po/legacy_expense.py",
line 121, in po_generator
'dateofexpense':expense_line.date_value, AttributeError: 'hr.expense.line' object has no attribute 'date_value'
I'm presuming this has something to do with the line_ids field in the Expenses form. I'm trying to get each row in the line_ids and enter these as new records in the purchase_orders table.
Thanks.
#Sagar Mohan
You are doing vary basic mistake here.
You are already looping on browse record by saying statement
for line in expense.line_ids:
When you say expense.line_ids this will already pull all related o2m record you don't need to search again by writing statement expense_line_record = expense_line.search(cr, uid, [('id', '=', line.id)], context=context) and you must know that search in v7 return then only integer id of the matching ids not recordset or browse record.
Correct code is :
def po_generator(self, cr, uid, ids, arg, context=None):
res = {}
purchase_orders = self.pool.get('purchase.orders')
for expense in self.browse(cr, uid, ids, context=context):
expense_line = self.pool.get('hr.expense.line')
purchase_orders = self.pool.get('purchase.orders')
for line in expense.line_ids:
purchase_orders.create(cr, uid,
{
'submitted_employee_name':line.employee_id.name,
'dateofexpense': line.date_value,
'project_id_expense': line.project_id.id,
'Description': line.name,
'netamount': line.product_exc_VAT,
'raised_employee_name': expense.employee_id.name,
'project_id_account_type': line.project_id_account_type,
},
context
)
return res
This code might need correction based on your need but you see that line is direct browse variable so you just need to . and access the field.
and one more note here that you never use self.pool.get inside loop that make your code heavy.
Thanks
I am trying to create a function field which will get the current membership type of a member and store it in a new field in res.partner. However this code is not getting called whenever I am creating or editing membership of a member. But if I remove the store attribute the functional field works just as expected. Note that I am reusing the membership module of openerp and using odoo8 now. I am attaching the code, please let me know where am I going wrong. I need this method to be called atleast when I am using the store attribute. Am I using the store attribute incorrectly:
from openerp.osv import osv, fields
class partner_member(osv.Model):
'''Partner'''
_inherit = 'res.partner'
def _get_membership_type(self,cr,uid,ids,context=None):
member_line_obj = self.pool.get('membership.membership_line')
partner_list = []
for line in member_line_obj.browse(cr, uid, ids, context=context):
if line.state == 'paid':
partner_list.append(line.partner.id)
return partner_list
def _current_membership(self, cr, uid, ids, field_name= None, arg=False, context=None):
res_obj = self.pool.get('res.partner')
res_data_obj = res_obj.browse(cr, uid, ids, context=context)
res=dict()
for member in res_data_obj:
if member.member_lines:
for lines in member.member_lines:
if (lines.state == 'paid'):
res[member.id] = lines.membership_id.name_template
break
else:
res[member.id] = 'None'
else:
res[member.id] = 'None'
return res
_columns = {
'current_membership':
fields.function(_current_membership,type='char',
string='Current Membership Type',
store = {
'membership.membership_line':
(_get_membership_type, ['state'], 10)
},
help="Shows the current membership of a user"),
}
You made a mistake on the _get_membership_type() method. Indeed, you return only the list of res.partner that are in a line with state == 'paid'.
I think you must return all the partner that are in lines no matter the state of the line.
def _get_membership_type(self,cr,uid,ids,context=None):
member_line_obj = self.pool.get('membership.membership_line')
partner_list = []
for line in member_line_obj.browse(cr, uid, ids, context=context):
partner_list.append(line.partner.id)
return partner_list
If you want your function _current_membership to be a method of your class (as you did) you need to add the method=True parameter to your field definition:
_columns = {
'current_membership':
fields.function(_current_membership,type='char',
string='Current Membership Type',
store = {
'membership.membership_line':
(_get_membership_type, ['state'], 10)
},
help="Shows the current membership of a user",
method=True),
}
That should resolve your problem.
Certainly you can simply use store=True to have your field recalculated on every change in whatever field of your object.
I need to change the state of a invoice using it's payment method.
def _payment_type_get(self, cr, uid, ids, field_name, arg, context={}):
result = {}
invoice_obj = self.pool.get('account.move.line')
for rec in self.browse(cr, uid, ids, context):
result[rec.id] = (0, 0)
invoice_id = invoice_obj.search(cr, uid, [(
'move_id', '=', rec.move_id.id)], context=context)
if invoice_id:
inv = invoice_obj.browse(cr, uid, invoice_id[0], context)
if inv.payment_type:
result[rec.id] = (inv.payment_type.id, self.pool.get(
'payment.type').browse(cr, uid, inv.payment_type.id, context).name)
else:
result[rec.id] = (0, 0)
return result
if result != '1':
return self.write(cr, uid, ids, {'state_cheque': 'usado'})
else:
return self.write(cr, uid, ids, {'state_cheque': 'caixa'})
I need to get the payment type at the creation of the "cheque", during the invoice closing, so I can set it to Caixa, if it's a Cheque or to Usado if it is not. I don't know if all the names are correct, since I copied it from a guy here who said it would(Shame on me if I let it pass).
The Cheque, like any payment is saved at a journal, cheque is saved at an specific journal with it's name(ChequeJournal), if I can use that to create the default state, It would be better.
Every single way I tried to do it, failed. Recently I've found that the payment type is saved as a int and not as a char or string, but changing it still give no results.
I could not use a self.write , since I'm editing the account_move_line.py, OpenERP can't find what I'm trying to add the state to. So, I need to get the invoice ID in order to change that state. A new problem arises?
Once a return statement is called, the function immediately exits, so your last 4 lines aren't being executed. (Or maybe that's a formatting error?)