Skip to content

Commit

Permalink
[ADD] account_lock_constrains: perform check in write
Browse files Browse the repository at this point in the history
  • Loading branch information
smetl committed Feb 19, 2018
1 parent 8136ef0 commit ee4a5ce
Showing 1 changed file with 48 additions and 29 deletions.
77 changes: 48 additions & 29 deletions addons/account_lock_constrains/models/res_company.py
Expand Up @@ -17,41 +17,60 @@ class ResCompany(models.Model):
@api.multi
def write(self, vals):
# fiscalyear_lock_date can't be set to a prior date
if vals.get('fiscalyear_lock_date'):
old_fiscalyear_lock_date = self and len(self) == 1 and self.fiscalyear_lock_date or None
self = self.with_context(dict(self._context, old_fiscalyear_lock_date=old_fiscalyear_lock_date))
if 'fiscalyear_lock_date' in vals or 'period_lock_date' in vals:
self._check_lock_dates(vals)
return super(ResCompany, self).write(vals)

@api.constrains('period_lock_date', 'fiscalyear_lock_date')
def _check_lock_dates(self):
period_lock_date = self.period_lock_date and\
time.strptime(self.period_lock_date, DEFAULT_SERVER_DATE_FORMAT) or False
old_fiscalyear_lock_date = self._context.get('old_fiscalyear_lock_date') and \
time.strptime(self._context['old_fiscalyear_lock_date'], DEFAULT_SERVER_DATE_FORMAT) or False
fiscalyear_lock_date = self.fiscalyear_lock_date and\
time.strptime(self.fiscalyear_lock_date, DEFAULT_SERVER_DATE_FORMAT) or False

# Check the irreversibility of the lock date for all users
if old_fiscalyear_lock_date:
if fiscalyear_lock_date:
if fiscalyear_lock_date < old_fiscalyear_lock_date:
raise ValidationError(_('The lock date for all users is irreversible and must be strictly higher than the previous date.'))
else:
raise ValidationError(_('The lock date for all users is irreversible and can\'t be removed'))
@api.multi
def _check_lock_dates(self, vals):
'''Check the lock dates for the current companies. This can't be done in a api.constrains because we need
to perform some comparison between new/old values. This method forces the lock dates to be irreversible.
if not fiscalyear_lock_date:
return
* The lock date for advisers can't be unset.
* The lock date for advisers must be higher than the lock date for non-advisers.
* The lock date for advisers must be higher than the last day of the previous month.
* The lock date for advisers must be higher than the previous lock date set if exists.
:param vals: The values passed to the write method.
'''
period_lock_date = vals.get('period_lock_date') and\
time.strptime(vals['period_lock_date'], DEFAULT_SERVER_DATE_FORMAT)
fiscalyear_lock_date = vals.get('fiscalyear_lock_date') and\
time.strptime(vals['fiscalyear_lock_date'], DEFAULT_SERVER_DATE_FORMAT)

# Check the lock date for all users
previous_month = datetime.strptime(fields.Date.today(), DEFAULT_SERVER_DATE_FORMAT) + relativedelta(months=-1)
days_previous_month = calendar.monthrange(previous_month.year, previous_month.month)
previous_month = previous_month.replace(day=days_previous_month[1]).timetuple()
if fiscalyear_lock_date <= previous_month:
raise ValidationError(_('The lock date for all users must be higher than the last day of the previous month.'))
for company in self:
old_fiscalyear_lock_date = company.fiscalyear_lock_date and\
time.strptime(company.fiscalyear_lock_date, DEFAULT_SERVER_DATE_FORMAT)

# The user attempts to remove the existing fiscal year lock date
if old_fiscalyear_lock_date and not fiscalyear_lock_date and 'fiscalyear_lock_date' in vals:
raise ValidationError(_('The lock date for all users is irreversible and can\'t be removed'))

# The user attempts to set a fiscal year lock date prior to the previous one
if old_fiscalyear_lock_date and fiscalyear_lock_date and fiscalyear_lock_date < old_fiscalyear_lock_date:
raise ValidationError(_('The lock date for all users is irreversible and must be strictly higher than the previous date.'))

# In case of no new fiscal year in vals, fallback to the oldest
if not fiscalyear_lock_date:
if old_fiscalyear_lock_date:
fiscalyear_lock_date = old_fiscalyear_lock_date
else:
continue

# The user attempts to set a fiscal year lock date prior to the last day of previous month
if fiscalyear_lock_date <= previous_month:
raise ValidationError(_('The lock date for all users must be higher than the last day of the previous month.'))

if not period_lock_date:
return
# In case of no new period lock date in vals, fallback to the one defined in the company
if not period_lock_date:
if company.period_lock_date:
period_lock_date = time.strptime(company.period_lock_date, DEFAULT_SERVER_DATE_FORMAT)
else:
continue

# Check the lock date for non-advisers
if period_lock_date > fiscalyear_lock_date:
raise ValidationError(_('The lock date for all users must be higher or equal to the lock date for non-advisers.'))
# The user attempts to set a fiscal year lock date prior to the period lock date
if period_lock_date > fiscalyear_lock_date:
raise ValidationError(_('The lock date for all users must be higher or equal to the lock date for non-advisers.'))

0 comments on commit ee4a5ce

Please sign in to comment.