Merge lp:~therp-nl/banking-addons/ba70-lp1295163-refactor_period_lookup into lp:banking-addons

Proposed by Stefan Rijnhart (Opener)
Status: Merged
Approved by: Holger Brunn (Therp)
Approved revision: 236
Merged at revision: 250
Proposed branch: lp:~therp-nl/banking-addons/ba70-lp1295163-refactor_period_lookup
Merge into: lp:banking-addons
Diff against target: 167 lines (+45/-42)
2 files modified
account_banking/account_banking.py (+44/-41)
account_banking/banking_import_transaction.py (+1/-1)
To merge this branch: bzr merge lp:~therp-nl/banking-addons/ba70-lp1295163-refactor_period_lookup
Reviewer Review Type Date Requested Status
Ruchir Shukla(BizzAppDev) (community) Needs Information
Lara (Therp) test Approve
Holger Brunn (Therp) code review Approve
Pedro Manuel Baeza code review Approve
Review via email: mp+211970@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Pedro Manuel Baeza (pedro.baeza) wrote :

LGTM

Regards.

review: Approve (code review)
236. By Stefan Rijnhart (Opener)

[FIX] Workaround for lp:1296229, defaults functions are passed context
 as a positional argument

Revision history for this message
Holger Brunn (Therp) (hbrunn) :
review: Approve (code review)
Revision history for this message
Lara (Therp) (lfreeke) :
review: Approve (test)
Revision history for this message
Ruchir Shukla(BizzAppDev) (ruchir.shukla) wrote :

+ except except_osv:
59 + if date:
60 + raise

This is something not needed .
as there date field is already required field for statement and statement lines .
Can you specify in which condition it will not have value date variable?

review: Needs Information
Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote :

@ruchir thanks for the review! The answer to your question is: the date is not specified when called as a default function.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'account_banking/account_banking.py'
--- account_banking/account_banking.py 2014-03-15 15:43:08 +0000
+++ account_banking/account_banking.py 2014-03-23 10:07:22 +0000
@@ -298,17 +298,24 @@
298 def _check_company_id(self, cr, uid, ids, context=None):298 def _check_company_id(self, cr, uid, ids, context=None):
299 """299 """
300 Adapt this constraint method from the account module to reflect the300 Adapt this constraint method from the account module to reflect the
301 move of period_id to the statement line301 move of period_id to the statement line: also check the periods of the
302 lines. Update the statement period if it does not have one yet.
303 Don't call super, because its check is integrated below and
304 it will break if a statement does not have any lines yet and
305 therefore may not have a period.
302 """306 """
303 for statement in self.browse(cr, uid, ids, context=context):307 for statement in self.browse(cr, uid, ids, context=context):
308 if (statement.period_id and
309 statement.company_id != statement.period_id.company_id):
310 return False
304 for line in statement.line_ids:311 for line in statement.line_ids:
305 if (line.period_id and312 if (line.period_id and
306 statement.company_id.id != line.period_id.company_id.id):313 statement.company_id != line.period_id.company_id):
307 return False314 return False
308 if not statement.period_id:315 if not statement.period_id:
309 statement.write({'period_id': line.period_id.id})316 statement.write({'period_id': line.period_id.id})
310 return super(account_bank_statement, self)._check_company_id(317 statement.refresh()
311 cr, uid, ids, context=context)318 return True
312 319
313 # Redefine the constraint, or it still refer to the original method320 # Redefine the constraint, or it still refer to the original method
314 _constraints = [321 _constraints = [
@@ -317,13 +324,24 @@
317 ['journal_id','period_id']),324 ['journal_id','period_id']),
318 ]325 ]
319326
320 def _get_period(self, cr, uid, date, context=None):327 def _get_period(self, cr, uid, date=False, context=None):
321 '''328 """
322 Find matching period for date, not meant for _defaults.329 Used in statement line's _defaults, so it is always triggered
323 '''330 on installation or module upgrade even if there are no records
324 period_obj = self.pool.get('account.period')331 without a value. For that reason, we need
325 periods = period_obj.find(cr, uid, dt=date, context=context)332 to be tolerant and allow for the situation in which no period
326 return periods and periods[0] or False333 exists for the current date (i.e. when no date is specified).
334
335 Cannot be used directly as a defaults method due to lp:1296229
336 """
337 local_ctx = dict(context or {}, account_period_prefer_normal=True)
338 try:
339 return self.pool.get('account.period').find(
340 cr, uid, dt=date, context=local_ctx)[0]
341 except except_osv:
342 if date:
343 raise
344 return False
327345
328 def _prepare_move(346 def _prepare_move(
329 self, cr, uid, st_line, st_line_number, context=None):347 self, cr, uid, st_line, st_line_number, context=None):
@@ -368,7 +386,7 @@
368 # Take period from statement line and write to context386 # Take period from statement line and write to context
369 # this will be picked up by the _prepare_move* methods387 # this will be picked up by the _prepare_move* methods
370 period_id = self._get_period(388 period_id = self._get_period(
371 cr, uid, st_line.date, context=context)389 cr, uid, date=st_line.date, context=context)
372 localctx = context.copy()390 localctx = context.copy()
373 localctx['period_id'] = period_id391 localctx['period_id'] = period_id
374392
@@ -424,7 +442,8 @@
424 context=context)442 context=context)
425 for st in self.browse(cr, uid, noname_ids, context=context):443 for st in self.browse(cr, uid, noname_ids, context=context):
426 if st.journal_id.sequence_id:444 if st.journal_id.sequence_id:
427 period_id = self._get_period(cr, uid, st.date)445 period_id = self._get_period(
446 cr, uid, date=st.date, context=context)
428 year = self.pool.get('account.period').browse(447 year = self.pool.get('account.period').browse(
429 cr, uid, period_id, context=context).fiscalyear_id.id448 cr, uid, period_id, context=context).fiscalyear_id.id
430 c = {'fiscalyear_id': year}449 c = {'fiscalyear_id': year}
@@ -436,8 +455,6 @@
436 return super(account_bank_statement, self).button_confirm_bank(455 return super(account_bank_statement, self).button_confirm_bank(
437 cr, uid, ids, context)456 cr, uid, ids, context)
438457
439account_bank_statement()
440
441458
442class account_voucher(orm.Model):459class account_voucher(orm.Model):
443 _inherit = 'account.voucher'460 _inherit = 'account.voucher'
@@ -446,12 +463,11 @@
446 if context is None:463 if context is None:
447 context = {}464 context = {}
448 if not context.get('period_id') and context.get('move_line_ids'):465 if not context.get('period_id') and context.get('move_line_ids'):
449 return self.pool.get('account.move.line').browse(466 move_line = self.pool.get('account.move.line').browse(
450 cr, uid , context.get('move_line_ids'), context=context)[0].period_id.id467 cr, uid , context.get('move_line_ids')[0], context=context)
468 return move_line.period_id.id
451 return super(account_voucher, self)._get_period(cr, uid, context)469 return super(account_voucher, self)._get_period(cr, uid, context)
452470
453account_voucher()
454
455471
456class account_bank_statement_line(orm.Model):472class account_bank_statement_line(orm.Model):
457 '''473 '''
@@ -464,28 +480,15 @@
464 _inherit = 'account.bank.statement.line'480 _inherit = 'account.bank.statement.line'
465 _description = 'Bank Transaction'481 _description = 'Bank Transaction'
466482
467 def _get_period(self, cr, uid, context=None):483 def _get_period(self, cr, uid, date=False, context=None):
468 """484 return self.pool['account.bank.statement']._get_period(
469 Get a non-opening period for today or a date specified in485 cr, uid, date=date, context=context)
470 the context.
471486
472 Used in this model's _defaults, so it is always triggered487 def _get_period_context(self, cr, uid, context=None):
473 on installation or module upgrade. For that reason, we need488 """
474 to be tolerant and allow for the situation in which no period489 Workaround for lp:1296229, context is passed positionally
475 exists for the current date (i.e. when no date is specified).490 """
476 """491 return self._get_period(cr, uid, context=context)
477 if context is None:
478 context = {}
479 date = context.get('date', False)
480 local_ctx = dict(context)
481 local_ctx['account_period_prefer_normal'] = True
482 try:
483 return self.pool.get('account.period').find(
484 cr, uid, dt=date, context=local_ctx)[0]
485 except except_osv:
486 if date:
487 raise
488 return False
489492
490 def _get_currency(self, cr, uid, context=None):493 def _get_currency(self, cr, uid, context=None):
491 '''494 '''
@@ -549,7 +552,7 @@
549 }552 }
550553
551 _defaults = {554 _defaults = {
552 'period_id': _get_period,555 'period_id': _get_period_context,
553 'currency': _get_currency,556 'currency': _get_currency,
554 }557 }
555558
556559
=== modified file 'account_banking/banking_import_transaction.py'
--- account_banking/banking_import_transaction.py 2014-01-21 07:55:55 +0000
+++ account_banking/banking_import_transaction.py 2014-03-23 10:07:22 +0000
@@ -1576,7 +1576,7 @@
1576 self.write(1576 self.write(
1577 cr, uid, [st_line.id], {1577 cr, uid, [st_line.id], {
1578 'period_id': self._get_period(1578 'period_id': self._get_period(
1579 cr, uid, {'date': st_line.date})1579 cr, uid, date=st_line.date, context=context)
1580 })1580 })
1581 st_line.refresh()1581 st_line.refresh()
1582 # Generate the statement number, if it is not already done1582 # Generate the statement number, if it is not already done

Subscribers

People subscribed via source and target branches

to status/vote changes: