|
|
@ -33,7 +33,11 @@ class RecordLifespan(models.Model): |
|
|
|
months = fields.Integer( |
|
|
|
required=True, |
|
|
|
help="Number of month after which the records will be set to inactive" |
|
|
|
"based on their write date" |
|
|
|
" based on their write date", |
|
|
|
) |
|
|
|
archive_states = fields.Char( |
|
|
|
help="Comma-separated list of states in which records should be" |
|
|
|
" archived. Implicit value is `'done, cancel')`.", |
|
|
|
) |
|
|
|
|
|
|
|
_sql_constraints = [ |
|
|
@ -41,6 +45,29 @@ class RecordLifespan(models.Model): |
|
|
|
"Months must be a value greater than 0"), |
|
|
|
] |
|
|
|
|
|
|
|
@api.constrains('archive_states') |
|
|
|
def _check_archive_states(self): |
|
|
|
for lifespan in self: |
|
|
|
if not lifespan.archive_states: |
|
|
|
continue |
|
|
|
model = self.env[lifespan.model_id.model] |
|
|
|
state_field = model.fields_get().get('state', {}) |
|
|
|
if not state_field: |
|
|
|
continue |
|
|
|
allowed_states \ |
|
|
|
= [x[0] for x in state_field.get('selection', [('')])] |
|
|
|
if not all(archive_state in allowed_states |
|
|
|
for archive_state in lifespan._get_archive_states()): |
|
|
|
raise exceptions.ValidationError(_( |
|
|
|
'Invalid set of states for "%s" model:\n' |
|
|
|
'%s\n' |
|
|
|
'Valid states:\n%s' |
|
|
|
) % ( |
|
|
|
lifespan.model_id.name, |
|
|
|
lifespan.archive_states, |
|
|
|
'\n'.join('- {}'.format(s) for s in allowed_states), |
|
|
|
)) |
|
|
|
|
|
|
|
@api.model |
|
|
|
def _scheduler_archive_records(self): |
|
|
|
lifespans = self.search([]) |
|
|
@ -53,16 +80,24 @@ class RecordLifespan(models.Model): |
|
|
|
_logger.info('Rusty Records now rest in peace') |
|
|
|
return True |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _get_archive_states(self): |
|
|
|
self.ensure_one() |
|
|
|
if not self.archive_states: |
|
|
|
return ['done', 'cancel'] |
|
|
|
return [s.strip() for s in self.archive_states.split(',')] |
|
|
|
|
|
|
|
@api.multi |
|
|
|
def _archive_domain(self, expiration_date): |
|
|
|
"""Returns the domain used to find the records to archive. |
|
|
|
|
|
|
|
Can be inherited to change the archived records for a model. |
|
|
|
""" |
|
|
|
self.ensure_one() |
|
|
|
model = self.env[self.model_id.model] |
|
|
|
domain = [('write_date', '<', expiration_date)] |
|
|
|
if 'state' in model.fields_get_keys(): |
|
|
|
domain += [('state', 'in', ('done', 'cancel'))] |
|
|
|
domain += [('state', 'in', self._get_archive_states())] |
|
|
|
return domain |
|
|
|
|
|
|
|
@api.multi |
|
|
|