From eb1d98143548d55b7cdb1e46e3cc7d3ef99021a5 Mon Sep 17 00:00:00 2001 From: Gery Debongnie Date: Wed, 7 May 2014 16:45:32 +0200 Subject: [PATCH] [FIX] code cleanup (addon purchase) simplify a few _count methods by removing useless try/except/pass and by using search_count when appropriate. It allows us to remove two one2many fields as well. bzr revid: ged@openerp.com-20140507144532-dgm9mfgt9k5p10jr --- addons/purchase/partner.py | 34 ++++++++++++---------------------- addons/purchase/purchase.py | 16 +++++++--------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/addons/purchase/partner.py b/addons/purchase/partner.py index b7757c2f91f..36ad724f0c4 100644 --- a/addons/purchase/partner.py +++ b/addons/purchase/partner.py @@ -25,22 +25,16 @@ class res_partner(osv.osv): _name = 'res.partner' _inherit = 'res.partner' - def _purchase_order_count(self, cr, uid, ids, field_name, arg, context=None): - res = dict(map(lambda x: (x,{'purchase_order_count': 0, 'supplier_invoice_count': 0}), ids)) - invoice_ids = self.pool.get('account.invoice').search(cr,uid, [('type', '=', 'in_invoice'), ('partner_id', '=', ids[0])]) - for partner in self.browse(cr, uid, ids, context=context): - res[partner.id] = { - 'purchase_order_count': len(partner.purchase_order_ids), - 'supplier_invoice_count': len(invoice_ids), - } - return res - def copy(self, cr, uid, id, default=None, context=None): - if default is None: - default = {} - - default.update({'purchase_order_ids': []}) - - return super(res_partner, self).copy(cr, uid, id, default=default, context=context) + def _purchase_invoice_count(self, cr, uid, ids, field_name, arg, context=None): + PurchaseOrder = self.pool['purchase.order'] + Invoice = self.pool['account.invoice'] + return { + partner_id: { + 'purchase_order_count': PurchaseOrder.search_count(cr,uid, [('partner_id', '=', partner_id)], context=context), + 'supplier_invoice_count': Invoice.search_count(cr,uid, [('partner_id', '=', partner_id), ('type','=','in_invoice')], context=context) + } + for partner_id in ids + } def _commercial_fields(self, cr, uid, context=None): return super(res_partner, self)._commercial_fields(cr, uid, context=context) + ['property_product_pricelist_purchase'] @@ -52,13 +46,9 @@ class res_partner(osv.osv): domain=[('type','=','purchase')], string="Purchase Pricelist", help="This pricelist will be used, instead of the default one, for purchases from the current partner"), - 'purchase_order_count': fields.function(_purchase_order_count, string='# of Purchase Order', type='integer', multi="count"), - 'purchase_order_ids': fields.one2many('purchase.order','partner_id','Purchase Order'), - 'invoice_ids': fields.one2many('account.invoice','partner_id','Supplier Invoices'), - 'supplier_invoice_count': fields.function(_purchase_order_count, string='# Supplier Invoices', type='integer', multi="count"), + 'purchase_order_count': fields.function(_purchase_invoice_count, string='# of Purchase Order', type='integer', multi="count"), + 'supplier_invoice_count': fields.function(_purchase_invoice_count, string='# Supplier Invoices', type='integer', multi="count"), } - - # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/purchase/purchase.py b/addons/purchase/purchase.py index 4e98282d444..457523d6d97 100644 --- a/addons/purchase/purchase.py +++ b/addons/purchase/purchase.py @@ -148,15 +148,13 @@ class purchase_order(osv.osv): return res and res[0] or False def _count_all(self, cr, uid, ids, field_name, arg, context=None): - res = dict(map(lambda x: (x,{'shipment_count': 0, 'invoice_count': 0,}), ids)) - try: - for data in self.browse(cr, uid, ids, context=context): - res[data.id] = {'shipment_count': len(data.picking_ids), - 'invoice_count': len(data.invoice_ids), - } - except: - pass - return res + return { + purchase.id: { + 'shipment_count': len(purchase.picking_ids), + 'invoice_count': len(purchase.invoice_ids), + } + for purchase in self.browse(cr, uid, ids, context=context) + } STATE_SELECTION = [ ('draft', 'Draft PO'),