Commit d32caa26 authored by Jérome Perrin's avatar Jérome Perrin

accounting: stop using portal_catalog.getObject

This is not public API and is not supposed to be used outside of catalog
tool.

Using getObject here is also wrong because it does not apply security
and does not check provided argument type.

To prevent retrieving too many documents, we usually pass limit=2 for
safety.

To detect inconsistencies, when only one result is expected we rely on
`brain, = portal_catalog(limit=2)` style unpacking to fail if more than
one result were found.
parent 98a092be
...@@ -8,7 +8,6 @@ request = portal.REQUEST ...@@ -8,7 +8,6 @@ request = portal.REQUEST
getInventoryList_ = portal.portal_simulation.getInventoryList getInventoryList_ = portal.portal_simulation.getInventoryList
traverse = portal.restrictedTraverse traverse = portal.restrictedTraverse
portal_catalog = portal.portal_catalog portal_catalog = portal.portal_catalog
getObject = portal_catalog.getObject
Base_translateString = portal.Base_translateString Base_translateString = portal.Base_translateString
selected_gap = gap_root selected_gap = gap_root
traverseCategory = portal.portal_categories.restrictedTraverse traverseCategory = portal.portal_categories.restrictedTraverse
...@@ -551,6 +550,8 @@ if node_uid_of_strict_account_type_to_group_by_payment: ...@@ -551,6 +550,8 @@ if node_uid_of_strict_account_type_to_group_by_payment:
if src__: if src__:
return src_list return src_list
TRANSLATED_NONE = Base_translateString('None')
node_title_and_id_cache = {} node_title_and_id_cache = {}
def getNodeTitleAndId(node_relative_url): def getNodeTitleAndId(node_relative_url):
try: try:
...@@ -575,11 +576,11 @@ def getSectionPriceCurrencyFromSectionUid(uid): ...@@ -575,11 +576,11 @@ def getSectionPriceCurrencyFromSectionUid(uid):
try: try:
return section_price_currency_dict[uid] return section_price_currency_dict[uid]
except KeyError: except KeyError:
section = getObject(uid)
if section is None:
price_currency = '' price_currency = ''
else: brain_list = portal_catalog(uid=uid, limit=2)
price_currency = section.getProperty('price_currency_reference') if brain_list:
brain, = brain_list
price_currency = brain.getObject().getProperty('price_currency_reference')
section_price_currency_dict[uid] = price_currency section_price_currency_dict[uid] = price_currency
return price_currency return price_currency
...@@ -590,7 +591,11 @@ def getAnalyticTitleFromUid(uid): ...@@ -590,7 +591,11 @@ def getAnalyticTitleFromUid(uid):
try: try:
return analytic_title_dict[uid] return analytic_title_dict[uid]
except KeyError: except KeyError:
node = getObject(uid) title = ''
brain_list = portal_catalog(uid=uid, limit=2)
if brain_list:
brain, = brain_list
node = brain.getObject()
title = node.getTranslatedTitle() title = node.getTranslatedTitle()
reference = node.getReference() reference = node.getReference()
if reference: if reference:
...@@ -598,7 +603,34 @@ def getAnalyticTitleFromUid(uid): ...@@ -598,7 +603,34 @@ def getAnalyticTitleFromUid(uid):
analytic_title_dict[uid] = title analytic_title_dict[uid] = title
return title return title
TRANSLATED_NONE = Base_translateString('None') mirror_section_title_dict = {None: ''}
def getMirrorSectionTitleFromUid(uid):
if uid is MARKER:
return ''
try:
return mirror_section_title_dict[uid]
except KeyError:
title = ''
brain_list = portal_catalog(uid=uid, limit=2)
if brain_list:
brain, = brain_list
title = brain.getObject().getTitle()
mirror_section_title_dict[uid] = title
return title
payment_title_dict = {None: TRANSLATED_NONE}
def getPaymentTitleFromUid(uid):
try:
return payment_title_dict[uid]
except KeyError:
title = ''
brain_list = portal_catalog(uid=uid, limit=2)
if brain_list:
brain, = brain_list
title = brain.getObject().getTitle()
payment_title_dict[uid] = title
return title
line_list = [] line_list = []
for key, data in line_per_account.iteritems(): for key, data in line_per_account.iteritems():
node_relative_url = key[0] node_relative_url = key[0]
...@@ -606,10 +638,9 @@ for key, data in line_per_account.iteritems(): ...@@ -606,10 +638,9 @@ for key, data in line_per_account.iteritems():
payment_uid = key[2] payment_uid = key[2]
analytic_key_list = key[3:] analytic_key_list = key[3:]
if expand_accounts and mirror_section_uid is not MARKER:
mirror_section_title = getObject(mirror_section_uid).getTitle()
else:
mirror_section_title = None mirror_section_title = None
if expand_accounts:
mirror_section_title = getMirrorSectionTitleFromUid(mirror_section_uid)
node_uid, node_title, node_id, node_string_index, node = getNodeTitleAndId(node_relative_url) node_uid, node_title, node_id, node_string_index, node = getNodeTitleAndId(node_relative_url)
...@@ -617,9 +648,7 @@ for key, data in line_per_account.iteritems(): ...@@ -617,9 +648,7 @@ for key, data in line_per_account.iteritems():
continue continue
if payment_uid is not MARKER: if payment_uid is not MARKER:
node_title += " (%s)" % ( node_title += " (%s)" % getPaymentTitleFromUid(payment_uid)
TRANSLATED_NONE if payment_uid is None else getObject(payment_uid).getTitle(),
)
if not node_string_index: if not node_string_index:
node_string_index = '%-10s' % node_id node_string_index = '%-10s' % node_id
......
...@@ -142,18 +142,17 @@ def getAccountName(account_relative_url): ...@@ -142,18 +142,17 @@ def getAccountName(account_relative_url):
account_name_cache[account_relative_url] = name account_name_cache[account_relative_url] = name
return name return name
getObject = portal.portal_catalog.getObject
title_for_uid_cache = {} title_for_uid_cache = {None: ''}
def getTitleForUid(uid): def getTitleForUid(uid):
try: try:
return title_for_uid_cache[uid] return title_for_uid_cache[uid]
except KeyError: except KeyError:
name = '' name = ''
if uid: brain_list = portal.portal_catalog(uid=uid, limit=2)
document = getObject(uid) if brain_list:
if document is not None: brain, = brain_list
name = document.getTitle() name = brain.getObject().getTitle()
title_for_uid_cache[uid] = name title_for_uid_cache[uid] = name
return name return name
......
...@@ -3,7 +3,6 @@ from zExceptions import Redirect ...@@ -3,7 +3,6 @@ from zExceptions import Redirect
portal = context.getPortalObject() portal = context.getPortalObject()
stool = portal.portal_selections stool = portal.portal_selections
getObject = portal.portal_catalog.getObject
countMessage = portal.portal_activities.countMessage countMessage = portal.portal_activities.countMessage
invoice_type_list = portal.getPortalInvoiceTypeList() invoice_type_list = portal.getPortalInvoiceTypeList()
...@@ -11,7 +10,7 @@ stool.updateSelectionCheckedUidList(selection_name, listbox_uid, uids) ...@@ -11,7 +10,7 @@ stool.updateSelectionCheckedUidList(selection_name, listbox_uid, uids)
selection_uid_list = context.portal_selections.getSelectionCheckedUidsFor( selection_uid_list = context.portal_selections.getSelectionCheckedUidsFor(
selection_name) selection_name)
if selection_uid_list: if selection_uid_list:
object_list = [getObject(uid) for uid in selection_uid_list] object_list = [brain.getObject() for brain in portal.portal_catalog(uid=selection_uid_list)]
else: else:
object_list = stool.callSelectionFor(selection_name) object_list = stool.callSelectionFor(selection_name)
......
...@@ -2,14 +2,13 @@ from Products.ERP5Type.Message import translateString ...@@ -2,14 +2,13 @@ from Products.ERP5Type.Message import translateString
from zExceptions import Redirect from zExceptions import Redirect
portal = context.getPortalObject() portal = context.getPortalObject()
stool = portal.portal_selections stool = portal.portal_selections
getObject = portal.portal_catalog.getObject
countMessage = portal.portal_activities.countMessage countMessage = portal.portal_activities.countMessage
stool.updateSelectionCheckedUidList(selection_name, listbox_uid, uids) stool.updateSelectionCheckedUidList(selection_name, listbox_uid, uids)
selection_uid_list = context.portal_selections.getSelectionCheckedUidsFor( selection_uid_list = context.portal_selections.getSelectionCheckedUidsFor(
selection_name) selection_name)
if selection_uid_list: if selection_uid_list:
object_list = [getObject(uid) for uid in selection_uid_list] object_list = [brain.getObject() for brain in portal.portal_catalog(uid=selection_uid_list)]
else: else:
object_list = stool.callSelectionFor(selection_name) object_list = stool.callSelectionFor(selection_name)
......
...@@ -42,8 +42,7 @@ if section_category: ...@@ -42,8 +42,7 @@ if section_category:
checked_uid_list = \ checked_uid_list = \
portal_selections.getSelectionCheckedUidsFor(selection_name) portal_selections.getSelectionCheckedUidsFor(selection_name)
if checked_uid_list: if checked_uid_list:
getObject = portal.portal_catalog.getObject delivery_list = [brain.getObject() for brain in portal.portal_catalog(uid=checked_uid_list)]
delivery_list = [getObject(uid) for uid in checked_uid_list]
else: else:
params = portal_selections.getSelectionParamsFor(selection_name) params = portal_selections.getSelectionParamsFor(selection_name)
params['limit'] = None # XXX potentially very big report params['limit'] = None # XXX potentially very big report
......
...@@ -26,13 +26,17 @@ def getAccountNumber(account_url): ...@@ -26,13 +26,17 @@ def getAccountNumber(account_url):
portal.restrictedTraverse(account_url).Account_getGapId() portal.restrictedTraverse(account_url).Account_getGapId()
return account_number_memo[account_url] return account_number_memo[account_url]
section_title_memo = {} section_title_memo = {None: ''}
def getSectionTitle(uid): def getSectionTitle(uid):
try: try:
return section_title_memo[uid] return section_title_memo[uid]
except KeyError: except KeyError:
section_title_memo[uid] =\ section_title = ''
portal.portal_catalog.getObject(uid).getTranslatedTitle() brain_list = portal.portal_catalog(uid=uid, limit=2)
if brain_list:
brain, = brain_list
section_title = brain.getObject().getTranslatedTitle()
section_title_memo[uid] = section_title
return section_title_memo[uid] return section_title_memo[uid]
last_period_id = 'period_%s' % len(period_list) last_period_id = 'period_%s' % len(period_list)
......
...@@ -7,13 +7,12 @@ portal = context.getPortalObject() ...@@ -7,13 +7,12 @@ portal = context.getPortalObject()
selection_name = \ selection_name = \
context.AccountingTransactionModule_viewGroupingFastInputDialog.listbox.get_value('selection_name') context.AccountingTransactionModule_viewGroupingFastInputDialog.listbox.get_value('selection_name')
getobject = portal.portal_catalog.getobject
selected_uid_list = portal.portal_selections.getSelectionCheckedUidsFor(selection_name)
total_selected_amount = 0 total_selected_amount = 0
# calculate total selected amount # calculate total selected amount
for uid in selected_uid_list or []: selected_uid_list = portal.portal_selections.getSelectionCheckedUidsFor(selection_name)
line = getobject(uid) if selected_uid_list:
for line in portal.portal_catalog(uid=selected_uid_list):
line = line.getObject()
if line.AccountingTransaction_isSourceView(): if line.AccountingTransaction_isSourceView():
total_selected_amount += (line.getSourceInventoriatedTotalAssetPrice() or 0) total_selected_amount += (line.getSourceInventoriatedTotalAssetPrice() or 0)
else: else:
......
...@@ -3,7 +3,6 @@ Used as a fast input dialog action. ...@@ -3,7 +3,6 @@ Used as a fast input dialog action.
""" """
from Products.CMFCore.WorkflowCore import WorkflowException from Products.CMFCore.WorkflowCore import WorkflowException
portal = context.getPortalObject() portal = context.getPortalObject()
getobject = portal.portal_catalog.getobject
Base_translateString = portal.Base_translateString Base_translateString = portal.Base_translateString
psm = Base_translateString('Nothing matches.') psm = Base_translateString('Nothing matches.')
request = container.REQUEST request = container.REQUEST
...@@ -34,8 +33,8 @@ portal.portal_selections.setSelectionParamsFor( ...@@ -34,8 +33,8 @@ portal.portal_selections.setSelectionParamsFor(
# calculate total selected amount # calculate total selected amount
total_selected_amount = 0 total_selected_amount = 0
if uids: if uids:
for uid in uids: for line in portal.portal_catalog(uid=uids):
line = getobject(uid) line = line.getObject()
if line.AccountingTransaction_isSourceView(): # XXX not optimal ! if line.AccountingTransaction_isSourceView(): # XXX not optimal !
total_selected_amount += (line.getSourceInventoriatedTotalAssetPrice() or 0) total_selected_amount += (line.getSourceInventoriatedTotalAssetPrice() or 0)
else: else:
...@@ -108,9 +107,11 @@ if grouping == 'grouping': ...@@ -108,9 +107,11 @@ if grouping == 'grouping':
else: else:
assert grouping == 'ungrouping' assert grouping == 'ungrouping'
# XXX is uids multi page safe here ? # XXX is uids multi page safe here ?
line_list = [getobject(line_uid) for line_uid in uids] line_list = []
ungrouped_line_list = [] if uids:
line_list = [brain.getObject() for brain in portal.portal_catalog(uid=uids)]
ungrouped_line_list = []
for line in line_list: for line in line_list:
if line.getGroupingReference(): if line.getGroupingReference():
ungrouped_line_list.extend(line.AccountingTransactionLine_resetGroupingReference()) ungrouped_line_list.extend(line.AccountingTransactionLine_resetGroupingReference())
......
...@@ -31,8 +31,9 @@ if accounting_transaction_line_uid_list is None: ...@@ -31,8 +31,9 @@ if accounting_transaction_line_uid_list is None:
continue continue
accounting_transaction_line_value_list.append(line) accounting_transaction_line_value_list.append(line)
else: else:
accounting_transaction_line_value_list = [ctool.getObject(uid) for uid in if accounting_transaction_line_uid_list:
accounting_transaction_line_uid_list] accounting_transaction_line_value_list = [
brain.getObject() for brain in portal.portal_catalog(uid=accounting_transaction_line_uid_list)]
for line in accounting_transaction_line_value_list: for line in accounting_transaction_line_value_list:
accounting_transaction = line.getParentValue() accounting_transaction = line.getParentValue()
......
...@@ -12,8 +12,8 @@ def getAccountingPeriodStartDateForSectionCategory(section_category, date): ...@@ -12,8 +12,8 @@ def getAccountingPeriodStartDateForSectionCategory(section_category, date):
period_start_date = None period_start_date = None
for uid in section_uid: for uid in section_uid:
if uid == -1: continue # Base_getSectionUidListForSectionCategory returns [-1] if no section_uid exists if uid == -1: continue # Base_getSectionUidListForSectionCategory returns [-1] if no section_uid exists
section = portal.portal_catalog.getObject(uid) section, = portal.portal_catalog(uid=uid, limit=2)
for ap in section.contentValues(portal_type='Accounting Period', for ap in section.getObject().contentValues(portal_type='Accounting Period',
checked_permission='Access contents information'): checked_permission='Access contents information'):
if ap.getSimulationState() not in ('planned', 'confirmed', if ap.getSimulationState() not in ('planned', 'confirmed',
'started', 'stopped', 'started', 'stopped',
......
...@@ -10,8 +10,8 @@ def getCurrencyForSectionCategory(section_category, section_category_strict): ...@@ -10,8 +10,8 @@ def getCurrencyForSectionCategory(section_category, section_category_strict):
for section_uid in portal.Base_getSectionUidListForSectionCategory( for section_uid in portal.Base_getSectionUidListForSectionCategory(
section_category, section_category_strict): section_category, section_category_strict):
if section_uid != -1: if section_uid != -1:
section = portal.portal_catalog.getObject(section_uid) section, = portal.portal_catalog(uid=section_uid, limit=2)
currency = section.getPriceCurrency() currency = section.getObject().getPriceCurrency()
if currency: if currency:
currency_set.add(currency) currency_set.add(currency)
if len(currency_set) == 1: if len(currency_set) == 1:
......
...@@ -4,9 +4,10 @@ try: ...@@ -4,9 +4,10 @@ try:
return request.other[context.mirror_section_uid] return request.other[context.mirror_section_uid]
except KeyError: except KeyError:
if context.mirror_section_uid: if context.mirror_section_uid:
mirror_section = context.getPortalObject().portal_catalog.getobject(context.mirror_section_uid) brain_list = context.getPortalObject().portal_catalog(uid=context.mirror_section_uid, limit=2)
if mirror_section is not None: if brain_list:
mirror_section_title = mirror_section.getTitle() brain, = brain_list
mirror_section_title = brain.getObject().getTitle()
request.other[context.mirror_section_uid] = mirror_section_title request.other[context.mirror_section_uid] = mirror_section_title
return mirror_section_title return mirror_section_title
...@@ -4,9 +4,10 @@ try: ...@@ -4,9 +4,10 @@ try:
return request.other[context.payment_uid] return request.other[context.payment_uid]
except KeyError: except KeyError:
if context.payment_uid: if context.payment_uid:
payment = context.getPortalObject().portal_catalog.getobject(context.payment_uid) brain_list = context.getPortalObject().portal_catalog(uid=context.payment_uid, limit=2)
if payment is not None: if brain_list:
payment_title = payment.getTitle() brain, = brain_list
payment_title = brain.getObject().getTitle()
request.other[context.payment_uid] = payment_title request.other[context.payment_uid] = payment_title
return payment_title return payment_title
...@@ -4,9 +4,10 @@ try: ...@@ -4,9 +4,10 @@ try:
return request.other[context.project_uid] return request.other[context.project_uid]
except KeyError: except KeyError:
if context.project_uid: if context.project_uid:
project = context.getPortalObject().portal_catalog.getobject(context.project_uid) brain_list = context.getPortalObject().portal_catalog(uid=context.project_uid, limit=2)
if project is not None: if brain_list:
project_title = project.getTitle() brain, = brain_list
project_title = brain.getObject().getTitle()
request.other[context.project_uid] = project_title request.other[context.project_uid] = project_title
return project_title return project_title
if brain.payment_uid: if brain.payment_uid:
bank_account = context.getPortalObject().portal_catalog.getObject(brain.payment_uid) brain_list = context.getPortalObject().portal_catalog(uid=brain.payment_uid, limit=2)
if bank_account is not None: if brain_list:
brain, = brain_list
bank_account = brain.getObject()
# XXX use preference ? # XXX use preference ?
if bank_account.getReference(): if bank_account.getReference():
return '%s - %s' % (bank_account.getReference(), bank_account.getTitle()) return '%s - %s' % (bank_account.getReference(), bank_account.getTitle())
......
...@@ -72,12 +72,11 @@ net_balance = 0.0 ...@@ -72,12 +72,11 @@ net_balance = 0.0
# accounts from PL have a balance calculated differently # accounts from PL have a balance calculated differently
is_pl_account = False is_pl_account = False
if params.get('node_uid'): if params.get('node_uid'):
if context.getUid() == params['node_uid']: if context.getUid() == params['node_uid']: # shortcut
node = context node = context
is_pl_account = context.isMemberOf('account_type/expense')\
or context.isMemberOf('account_type/income')
else: else:
node = portal.portal_catalog.getObject(params['node_uid']) node, = portal.portal_catalog(uid=params['node_uid'], limit=2)
node = node.getObject()
is_pl_account = node.isMemberOf('account_type/expense')\ is_pl_account = node.isMemberOf('account_type/expense')\
or node.isMemberOf('account_type/income') or node.isMemberOf('account_type/income')
...@@ -197,11 +196,16 @@ if from_date or is_pl_account: ...@@ -197,11 +196,16 @@ if from_date or is_pl_account:
node_translated_title=node.getTranslatedTitle() node_translated_title=node.getTranslatedTitle()
) )
if params.get('mirror_section_uid'): if params.get('mirror_section_uid'):
mirror_section = portal.portal_catalog.getObject(params['mirror_section_uid']) brain_list = portal.portal_catalog(uid=params['mirror_section_uid'], limit=2)
if brain_list:
brain, = brain_list
previous_balance.edit( previous_balance.edit(
mirror_section_title=mirror_section.getTitle() mirror_section_title=brain.getObject().getTitle()
) )
section = portal.portal_catalog.getObject(section_uid) # It may happen that user cannot search mirror section from catalog,
# but our own section should be found.
section, = portal.portal_catalog(uid=section_uid, limit=2)
section = section.getObject()
previous_balance.edit( previous_balance.edit(
Movement_getSectionPriceCurrency=section.getPriceCurrencyReference(), Movement_getSectionPriceCurrency=section.getPriceCurrencyReference(),
resource_reference=section.getPriceCurrencyReference(), resource_reference=section.getPriceCurrencyReference(),
......
...@@ -74,7 +74,8 @@ if period_start_date and params.get('node_uid'): ...@@ -74,7 +74,8 @@ if period_start_date and params.get('node_uid'):
if context.getUid() == params['node_uid']: # I bet it's context if context.getUid() == params['node_uid']: # I bet it's context
node = context node = context
else: else:
node = portal.portal_catalog.getObject(params['node_uid']) node, = portal.portal_catalog(uid=params['node_uid'], limit=2)
node = node.getObject()
if node.isMemberOf('account_type/expense') or\ if node.isMemberOf('account_type/expense') or\
node.isMemberOf('account_type/income'): node.isMemberOf('account_type/income'):
# For expense or income accounts, we only take into account transactions # For expense or income accounts, we only take into account transactions
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment