From 0b60ddae67433d07acfd3c7fcf7932c352bd223e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 19 Aug 2010 22:17:42 +0200 Subject: [PATCH] Update for new Transaction --- INSTALL | 2 +- service.py | 90 ++++++++++++++++++++++-------------------------------- work.py | 90 +++++++++++++++++++++++++----------------------------- 3 files changed, 79 insertions(+), 103 deletions(-) diff --git a/INSTALL b/INSTALL index 2f5b09aed6..d4a0ecd5eb 100644 --- a/INSTALL +++ b/INSTALL @@ -4,7 +4,7 @@ Installing trytond_project_revenue Prerequisites ------------- - * Python 2.4 or later (http://www.python.org/) + * Python 2.5 or later (http://www.python.org/) * trytond (http://www.tryton.org/) * trytond_project (http://www.tryton.org/) * trytond_timesheet (http://www.tryton.org/) diff --git a/service.py b/service.py index cf40c1b47d..a7993c9f12 100644 --- a/service.py +++ b/service.py @@ -1,9 +1,9 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. -"Service" from trytond.model import ModelView, ModelSQL, fields from trytond.model.cacheable import Cacheable from trytond.pyson import Eval +from trytond.transaction import Transaction class Employee(ModelSQL, ModelView, Cacheable): @@ -17,44 +17,39 @@ class Employee(ModelSQL, ModelView, Cacheable): currency_digits = fields.Function(fields.Integer('Currency Digits', on_change_with=['company']), 'get_currency_digits') - def get_cost_price(self, cursor, user, ids, name, context=None): + def get_cost_price(self, ids, name): ''' Return the cost price at the date given in the context or the current date ''' res = {} - if context is None: - context = {} - ctx_date = context.get('date', None) + ctx_date = Transaction().context.get('date', None) for employee_id in ids: - res[employee_id] = self.compute_cost_price(cursor, user, - employee_id, ctx_date, context=context) + res[employee_id] = self.compute_cost_price(employee_id, ctx_date) return res - def compute_cost_price(self, cursor, user, employee_id, date=None, - context=None): + def compute_cost_price(self, employee_id, date=None): date_obj = self.pool.get('ir.date') cost_price_obj = self.pool.get('company.employee_cost_price') # Get from cache employee costs or fetch them from the db - employee_costs = self.get(cursor, employee_id) + employee_costs = self.get(employee_id) if employee_costs is None: - cost_price_ids = cost_price_obj.search(cursor, user, [ + cost_price_ids = cost_price_obj.search([ ('employee', '=', employee_id), ], order=[('date', 'ASC')]) - cost_prices = cost_price_obj.browse( - cursor, user, cost_price_ids, context=context) + cost_prices = cost_price_obj.browse(cost_price_ids) employee_costs = [] for cost_price in cost_prices: employee_costs.append( (cost_price.date, cost_price.cost_price)) - self.add(cursor, employee_id, employee_costs) + self.add(employee_id, employee_costs) if date is None: - date = date_obj.today(cursor, user, context=context) + date = date_obj.today() # compute the cost price for the given date cost = 0 if employee_costs and date >= employee_costs[0][0]: @@ -64,28 +59,24 @@ class Employee(ModelSQL, ModelView, Cacheable): break return cost - def get_currency_digits(self, cursor, user, ids, name, context=None): + def get_currency_digits(self, ids, name): res = {} - for employee in self.browse(cursor, user, ids, context=context): + for employee in self.browse(ids): res[employee.id] = employee.company.currency.digits return res - def on_change_with_currency_digits(self, cursor, user, vals, context=None): + def on_change_with_currency_digits(self, vals): company_obj = self.pool.get('company.company') if vals.get('company'): - company = company_obj.browse(cursor, user, vals['company'], - context=context) + company = company_obj.browse(vals['company']) return company.currency.digits return 2 - def default_currency_digits(self, cursor, user, context=None): + def default_currency_digits(self): company_obj = self.pool.get('company.company') - if context is None: - context = {} - company = None - if context.get('company'): - company = company_obj.browse(cursor, user, context['company'], - context=context) + company = Transaction().context.get('company') + if company: + company = company_obj.browse(company) return company.currency.digits return 2 @@ -114,47 +105,40 @@ class EmployeeCostPrice(ModelSQL, ModelView): ] self._order.insert(0, ('date', 'DESC')) - def default_date(self, cursor, user, context=None): + def default_date(self): date_obj = self.pool.get('ir.date') - return date_obj.today(cursor, user, context=context) + return date_obj.today() - def delete(self, cursor, user, ids, context=None): - self.pool.get('company.employee').clear(cursor) - return super(EmployeeCostPrice , self).delete(cursor, user, ids, - context=context) + def delete(self, ids): + self.pool.get('company.employee').clear.reset() + return super(EmployeeCostPrice , self).delete(ids) - def create(self, cursor, user, vals, context=None): - self.pool.get('company.employee').clear(cursor) - return super(EmployeeCostPrice , self).create(cursor, user, vals, - context=context) + def create(self, vals): + self.pool.get('company.employee').clear.reset() + return super(EmployeeCostPrice , self).create(vals) - def write(self, cursor, user, ids, vals, context=None): - self.pool.get('company.employee').clear(cursor) - return super(EmployeeCostPrice , self).write(cursor, user, ids, vals, - context=context) + def write(self, ids, vals): + self.pool.get('company.employee').clear.reset() + return super(EmployeeCostPrice , self).write(ids, vals) - def get_currency_digits(self, cursor, user, ids, name, context=None): + def get_currency_digits(self, ids, name): res = {} - for costprice in self.browse(cursor, user, ids, context=context): + for costprice in self.browse(ids): res[costprice.id] = costprice.employee.company.currency.digits return res - def on_change_with_currency_digits(self, cursor, user, vals, context=None): + def on_change_with_currency_digits(self, vals): company_obj = self.pool.get('company.employee') if vals.get('employee'): - employee = employee_obj.browse(cursor, user, vals['employee'], - context=context) + employee = employee_obj.browse(vals['employee']) return employee.company.currency.digits return 2 - def default_currency_digits(self, cursor, user, context=None): + def default_currency_digits(self): company_obj = self.pool.get('company.company') - if context is None: - context = {} - company = None - if context.get('company'): - company = company_obj.browse(cursor, user, context['company'], - context=context) + company = Transaction().context.get('company') + if company: + company = company_obj.browse(company) return company.currency.digits return 2 diff --git a/work.py b/work.py index c8c2e39042..eba37fabdf 100644 --- a/work.py +++ b/work.py @@ -1,28 +1,29 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. - +from __future__ import with_statement +from decimal import Decimal from trytond.model import ModelView, ModelSQL, fields from trytond.pyson import Not, Equal, Eval -from decimal import Decimal +from trytond.transaction import Transaction + class TimesheetLine(ModelSQL, ModelView): _name = 'timesheet.line' - def compute_cost(self, cursor, user, line, context=None): + def compute_cost(self, line): employee_obj = self.pool.get('company.employee') currency_obj = self.pool.get('currency.currency') - cost_price = employee_obj.compute_cost_price(cursor, user, - line.employee.id, date=line.date, context=context) + cost_price = employee_obj.compute_cost_price(line.employee.id, + date=line.date) line_company = line.employee.company work_company = line.work.company if line_company.id != work_company.id and\ line_company.currency.id != work_company.currency.id: - cost_price = currency_obj.compute(cursor, user, - line_company.currency, cost_price, - work_company.currency, context=context) + cost_price = currency_obj.compute(line_company.currency, + cost_price, work_company.currency) return Decimal(str(line.hours)) * cost_price @@ -53,24 +54,23 @@ class Work(ModelSQL, ModelView): currency_digits = fields.Function(fields.Integer('Currency Digits', on_change_with=['company']), 'get_currency_digits') - def get_cost(self, cursor, user, ids, name, context=None): + def get_cost(self, ids, name): timesheet_line_obj = self.pool.get('timesheet.line') - all_ids = self.search(cursor, user, [ + all_ids = self.search([ ('parent', 'child_of', ids), - ('active', '=', True)], context=context) + ids + ('active', '=', True)]) + ids all_ids = list(set(all_ids)) res = {} id2work = {} leafs = set() - for work in self.browse(cursor, user, all_ids, context=context): + for work in self.browse(all_ids): id2work[work.id] = work if not work.children: leafs.add(work.id) res[work.id] = Decimal('0') for ts_line in work.timesheet_lines: - res[work.id] += timesheet_line_obj.compute_cost(cursor, user, - ts_line, context=context) + res[work.id] += timesheet_line_obj.compute_cost(ts_line) while leafs: parents = set() @@ -89,15 +89,15 @@ class Work(ModelSQL, ModelView): return res - def get_revenue(self, cursor, user, ids, name, context=None): - all_ids = self.search(cursor, user, [ + def get_revenue(self, ids, name): + all_ids = self.search([ ('parent', 'child_of', ids), - ('active', '=', True)], context=context) + ids + ('active', '=', True)]) + ids all_ids = list(set(all_ids)) res = {} id2work = {} leafs = set() - for work in self.browse(cursor, user, all_ids, context=context): + for work in self.browse(all_ids): id2work[work.id] = work if not work.children: leafs.add(work.id) @@ -124,32 +124,28 @@ class Work(ModelSQL, ModelView): return res - def get_currency_digits(self, cursor, user, ids, name, context=None): + def get_currency_digits(self, ids, name): res = {} - for work in self.browse(cursor, user, ids, context=context): + for work in self.browse(ids): res[work.id] = work.company.currency.digits return res - def on_change_with_currency_digits(self, cursor, user, vals, context=None): + def on_change_with_currency_digits(self, vals): company_obj = self.pool.get('company.company') if vals.get('company'): - company = company_obj.browse(cursor, user, vals['company'], - context=context) + company = company_obj.browse(vals['company']) return company.currency.digits return 2 - def default_currency_digits(self, cursor, user, context=None): + def default_currency_digits(self): company_obj = self.pool.get('company.company') - if context is None: - context = {} - company = None - if context.get('company'): - company = company_obj.browse(cursor, user, context['company'], - context=context) + company = Transaction().context.get('company') + if company: + company = company_obj.browse(company) return company.currency.digits return 2 - def on_change_product(self, cursor, user, vals, context=None): + def on_change_product(self, vals): product_obj = self.pool.get('product.product') user_obj = self.pool.get('res.user') company_obj = self.pool.get('company.company') @@ -159,31 +155,27 @@ class Work(ModelSQL, ModelView): if not vals.get('product'): return {} - ctx = context and context.copy() or {} + context = {} - product = product_obj.browse(cursor, user, vals['product'], - context=context) + product = product_obj.browse(vals['product']) if vals.get('party'): - ctx2['customer'] = vals['party'] + context['customer'] = vals['party'] - uom_id = model_data_obj.get_id(cursor, user, 'product', 'uom_hour', - context=context) - hour_uom = uom_obj.browse(cursor, user, uom_id, context=context) + uom_id = model_data_obj.get_id('product', 'uom_hour') + hour_uom = uom_obj.browse(uom_id) - list_price = uom_obj.compute_price(cursor, user, - product.default_uom, product.list_price, hour_uom, - context=ctx) + with Transaction().set_context(context): + list_price = uom_obj.compute_price(product.default_uom, + product.list_price, hour_uom) if vals.get('company'): - user_record = user_obj.browse(cursor, user, user, context=context) - if user_record.company.id != vals['company']: - company = company_obj.browse(cursor, user, vals['company'], - context=context) - if user_record.company.currency.id != company.currency.id: - list_price = currency_obj.compute(cursor, user, - user_record.company.currency, list_price, - company.currency, context=context) + user = user_obj.browse(Transaction().user) + if user.company.id != vals['company']: + company = company_obj.browse(vals['company']) + if user.company.currency.id != company.currency.id: + list_price = currency_obj.compute(user.company.currency, + list_price, company.currency) return {'list_price': list_price} Work()