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
getInventoryList_ = portal.portal_simulation.getInventoryList
traverse = portal.restrictedTraverse
portal_catalog = portal.portal_catalog
getObject = portal_catalog.getObject
Base_translateString = portal.Base_translateString
selected_gap = gap_root
traverseCategory = portal.portal_categories.restrictedTraverse
......@@ -551,6 +550,8 @@ if node_uid_of_strict_account_type_to_group_by_payment:
if src__:
return src_list
TRANSLATED_NONE = Base_translateString('None')
node_title_and_id_cache = {}
def getNodeTitleAndId(node_relative_url):
try:
......@@ -575,11 +576,11 @@ def getSectionPriceCurrencyFromSectionUid(uid):
try:
return section_price_currency_dict[uid]
except KeyError:
section = getObject(uid)
if section is None:
price_currency = ''
else:
price_currency = section.getProperty('price_currency_reference')
brain_list = portal_catalog(uid=uid, limit=2)
if brain_list:
brain, = brain_list
price_currency = brain.getObject().getProperty('price_currency_reference')
section_price_currency_dict[uid] = price_currency
return price_currency
......@@ -590,7 +591,11 @@ def getAnalyticTitleFromUid(uid):
try:
return analytic_title_dict[uid]
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()
reference = node.getReference()
if reference:
......@@ -598,7 +603,34 @@ def getAnalyticTitleFromUid(uid):
analytic_title_dict[uid] = 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 = []
for key, data in line_per_account.iteritems():
node_relative_url = key[0]
......@@ -606,10 +638,9 @@ for key, data in line_per_account.iteritems():
payment_uid = key[2]
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
if expand_accounts:
mirror_section_title = getMirrorSectionTitleFromUid(mirror_section_uid)
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():
continue
if payment_uid is not MARKER:
node_title += " (%s)" % (
TRANSLATED_NONE if payment_uid is None else getObject(payment_uid).getTitle(),
)
node_title += " (%s)" % getPaymentTitleFromUid(payment_uid)
if not node_string_index:
node_string_index = '%-10s' % node_id
......
......@@ -142,18 +142,17 @@ def getAccountName(account_relative_url):
account_name_cache[account_relative_url] = name
return name
getObject = portal.portal_catalog.getObject
title_for_uid_cache = {}
title_for_uid_cache = {None: ''}
def getTitleForUid(uid):
try:
return title_for_uid_cache[uid]
except KeyError:
name = ''
if uid:
document = getObject(uid)
if document is not None:
name = document.getTitle()
brain_list = portal.portal_catalog(uid=uid, limit=2)
if brain_list:
brain, = brain_list
name = brain.getObject().getTitle()
title_for_uid_cache[uid] = name
return name
......
......@@ -3,7 +3,6 @@ from zExceptions import Redirect
portal = context.getPortalObject()
stool = portal.portal_selections
getObject = portal.portal_catalog.getObject
countMessage = portal.portal_activities.countMessage
invoice_type_list = portal.getPortalInvoiceTypeList()
......@@ -11,7 +10,7 @@ stool.updateSelectionCheckedUidList(selection_name, listbox_uid, uids)
selection_uid_list = context.portal_selections.getSelectionCheckedUidsFor(
selection_name)
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:
object_list = stool.callSelectionFor(selection_name)
......
......@@ -2,14 +2,13 @@ from Products.ERP5Type.Message import translateString
from zExceptions import Redirect
portal = context.getPortalObject()
stool = portal.portal_selections
getObject = portal.portal_catalog.getObject
countMessage = portal.portal_activities.countMessage
stool.updateSelectionCheckedUidList(selection_name, listbox_uid, uids)
selection_uid_list = context.portal_selections.getSelectionCheckedUidsFor(
selection_name)
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:
object_list = stool.callSelectionFor(selection_name)
......
......@@ -42,8 +42,7 @@ if section_category:
checked_uid_list = \
portal_selections.getSelectionCheckedUidsFor(selection_name)
if checked_uid_list:
getObject = portal.portal_catalog.getObject
delivery_list = [getObject(uid) for uid in checked_uid_list]
delivery_list = [brain.getObject() for brain in portal.portal_catalog(uid=checked_uid_list)]
else:
params = portal_selections.getSelectionParamsFor(selection_name)
params['limit'] = None # XXX potentially very big report
......
......@@ -26,13 +26,17 @@ def getAccountNumber(account_url):
portal.restrictedTraverse(account_url).Account_getGapId()
return account_number_memo[account_url]
section_title_memo = {}
section_title_memo = {None: ''}
def getSectionTitle(uid):
try:
return section_title_memo[uid]
except KeyError:
section_title_memo[uid] =\
portal.portal_catalog.getObject(uid).getTranslatedTitle()
section_title = ''
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]
last_period_id = 'period_%s' % len(period_list)
......
......@@ -7,13 +7,12 @@ portal = context.getPortalObject()
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
# calculate total selected amount
for uid in selected_uid_list or []:
line = getobject(uid)
selected_uid_list = portal.portal_selections.getSelectionCheckedUidsFor(selection_name)
if selected_uid_list:
for line in portal.portal_catalog(uid=selected_uid_list):
line = line.getObject()
if line.AccountingTransaction_isSourceView():
total_selected_amount += (line.getSourceInventoriatedTotalAssetPrice() or 0)
else:
......
......@@ -3,7 +3,6 @@ Used as a fast input dialog action.
"""
from Products.CMFCore.WorkflowCore import WorkflowException
portal = context.getPortalObject()
getobject = portal.portal_catalog.getobject
Base_translateString = portal.Base_translateString
psm = Base_translateString('Nothing matches.')
request = container.REQUEST
......@@ -34,8 +33,8 @@ portal.portal_selections.setSelectionParamsFor(
# calculate total selected amount
total_selected_amount = 0
if uids:
for uid in uids:
line = getobject(uid)
for line in portal.portal_catalog(uid=uids):
line = line.getObject()
if line.AccountingTransaction_isSourceView(): # XXX not optimal !
total_selected_amount += (line.getSourceInventoriatedTotalAssetPrice() or 0)
else:
......@@ -108,9 +107,11 @@ if grouping == 'grouping':
else:
assert grouping == 'ungrouping'
# XXX is uids multi page safe here ?
line_list = [getobject(line_uid) for line_uid in uids]
ungrouped_line_list = []
line_list = []
if uids:
line_list = [brain.getObject() for brain in portal.portal_catalog(uid=uids)]
ungrouped_line_list = []
for line in line_list:
if line.getGroupingReference():
ungrouped_line_list.extend(line.AccountingTransactionLine_resetGroupingReference())
......
......@@ -31,8 +31,9 @@ if accounting_transaction_line_uid_list is None:
continue
accounting_transaction_line_value_list.append(line)
else:
accounting_transaction_line_value_list = [ctool.getObject(uid) for uid in
accounting_transaction_line_uid_list]
if 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:
accounting_transaction = line.getParentValue()
......
......@@ -12,8 +12,8 @@ def getAccountingPeriodStartDateForSectionCategory(section_category, date):
period_start_date = None
for uid in section_uid:
if uid == -1: continue # Base_getSectionUidListForSectionCategory returns [-1] if no section_uid exists
section = portal.portal_catalog.getObject(uid)
for ap in section.contentValues(portal_type='Accounting Period',
section, = portal.portal_catalog(uid=uid, limit=2)
for ap in section.getObject().contentValues(portal_type='Accounting Period',
checked_permission='Access contents information'):
if ap.getSimulationState() not in ('planned', 'confirmed',
'started', 'stopped',
......
......@@ -10,8 +10,8 @@ def getCurrencyForSectionCategory(section_category, section_category_strict):
for section_uid in portal.Base_getSectionUidListForSectionCategory(
section_category, section_category_strict):
if section_uid != -1:
section = portal.portal_catalog.getObject(section_uid)
currency = section.getPriceCurrency()
section, = portal.portal_catalog(uid=section_uid, limit=2)
currency = section.getObject().getPriceCurrency()
if currency:
currency_set.add(currency)
if len(currency_set) == 1:
......
......@@ -4,9 +4,10 @@ try:
return request.other[context.mirror_section_uid]
except KeyError:
if context.mirror_section_uid:
mirror_section = context.getPortalObject().portal_catalog.getobject(context.mirror_section_uid)
if mirror_section is not None:
mirror_section_title = mirror_section.getTitle()
brain_list = context.getPortalObject().portal_catalog(uid=context.mirror_section_uid, limit=2)
if brain_list:
brain, = brain_list
mirror_section_title = brain.getObject().getTitle()
request.other[context.mirror_section_uid] = mirror_section_title
return mirror_section_title
......@@ -4,9 +4,10 @@ try:
return request.other[context.payment_uid]
except KeyError:
if context.payment_uid:
payment = context.getPortalObject().portal_catalog.getobject(context.payment_uid)
if payment is not None:
payment_title = payment.getTitle()
brain_list = context.getPortalObject().portal_catalog(uid=context.payment_uid, limit=2)
if brain_list:
brain, = brain_list
payment_title = brain.getObject().getTitle()
request.other[context.payment_uid] = payment_title
return payment_title
......@@ -4,9 +4,10 @@ try:
return request.other[context.project_uid]
except KeyError:
if context.project_uid:
project = context.getPortalObject().portal_catalog.getobject(context.project_uid)
if project is not None:
project_title = project.getTitle()
brain_list = context.getPortalObject().portal_catalog(uid=context.project_uid, limit=2)
if brain_list:
brain, = brain_list
project_title = brain.getObject().getTitle()
request.other[context.project_uid] = project_title
return project_title
if brain.payment_uid:
bank_account = context.getPortalObject().portal_catalog.getObject(brain.payment_uid)
if bank_account is not None:
brain_list = context.getPortalObject().portal_catalog(uid=brain.payment_uid, limit=2)
if brain_list:
brain, = brain_list
bank_account = brain.getObject()
# XXX use preference ?
if bank_account.getReference():
return '%s - %s' % (bank_account.getReference(), bank_account.getTitle())
......
......@@ -72,12 +72,11 @@ net_balance = 0.0
# accounts from PL have a balance calculated differently
is_pl_account = False
if params.get('node_uid'):
if context.getUid() == params['node_uid']:
if context.getUid() == params['node_uid']: # shortcut
node = context
is_pl_account = context.isMemberOf('account_type/expense')\
or context.isMemberOf('account_type/income')
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')\
or node.isMemberOf('account_type/income')
......@@ -197,11 +196,16 @@ if from_date or is_pl_account:
node_translated_title=node.getTranslatedTitle()
)
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(
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(
Movement_getSectionPriceCurrency=section.getPriceCurrencyReference(),
resource_reference=section.getPriceCurrencyReference(),
......
......@@ -74,7 +74,8 @@ if period_start_date and params.get('node_uid'):
if context.getUid() == params['node_uid']: # I bet it's context
node = context
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\
node.isMemberOf('account_type/income'):
# 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