Browse Source

[IMP] Allow dynamic state recognition on `record.lifespan`

pull/1381/head
Artem Kostyuk 6 years ago
parent
commit
6224d266c3
  1. 4
      record_archiver/README.rst
  2. 41
      record_archiver/models/record_lifespan.py
  3. 1
      record_archiver/views/record_lifespan_view.xml

4
record_archiver/README.rst

@ -57,6 +57,10 @@ lifespan and with a state being ``done`` or ``cancel``. If these rules
need to be modified for a model (e.g. change the states to archive), the
hook ``RecordLifespan._archive_domain`` can be extended.
Alternatively, you can provide a comma-separated list of states to
``record.lifespan`` records to redefine a set of record states that
should be archived.
Bug Tracker
===========

41
record_archiver/models/record_lifespan.py

@ -32,8 +32,12 @@ 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"
help="Number of month after which the records will be set to inactive"
" 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

1
record_archiver/views/record_lifespan_view.xml

@ -8,6 +8,7 @@
<tree string="Records Archiver Lifespans" editable="bottom">
<field name="model_id"/>
<field name="months"/>
<field name="archive_states"/>
</tree>
</field>
</record>

Loading…
Cancel
Save