Yannick Vaucher
10 years ago
committed by
Guewen Baconnier
9 changed files with 337 additions and 0 deletions
-
1rusty_record_reaper_runner/__init__.py
-
44rusty_record_reaper_runner/__openerp__.py
-
19rusty_record_reaper_runner/data/cron.xml
-
3rusty_record_reaper_runner/models/__init__.py
-
30rusty_record_reaper_runner/models/company.py
-
112rusty_record_reaper_runner/models/record_lifespan.py
-
70rusty_record_reaper_runner/models/res_config.py
-
BINrusty_record_reaper_runner/static/src/img/icon.png
-
58rusty_record_reaper_runner/views/res_config.xml
@ -0,0 +1 @@ |
|||
from . import models |
@ -0,0 +1,44 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# |
|||
# Author: Yannick Vaucher |
|||
# Copyright 2015 Camptocamp SA |
|||
# |
|||
# This program is free software: you can redistribute it and/or modify |
|||
# it under the terms of the GNU Affero General Public License as |
|||
# published by the Free Software Foundation, either version 3 of the |
|||
# License, or (at your option) any later version. |
|||
# |
|||
# This program is distributed in the hope that it will be useful, |
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
# GNU Affero General Public License for more details. |
|||
# |
|||
# You should have received a copy of the GNU Affero General Public License |
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
# |
|||
|
|||
{'name': 'Rusty Record Reaper Runner', |
|||
'version': '0.1', |
|||
'description': """ |
|||
Define a cron job to deactivate old records in order to optimize performances. |
|||
|
|||
Records are deactivated base on last activity on them (write_date). |
|||
|
|||
You can configure lifespan of each type of record in |
|||
Settings -> Configuration -> Rusty Record Reaper Runner |
|||
|
|||
Lifespan is defined per record per company. |
|||
""", |
|||
'author': 'Camptocamp', |
|||
'maintainer': 'Camptocamp', |
|||
'license': 'AGPL-3', |
|||
'category': 'misc', |
|||
'complexity': "easy", # easy, normal, expert |
|||
'depends': ['base'], |
|||
'website': 'www.camptocamp.com', |
|||
'data': ['views/res_config.xml', |
|||
'data/cron.xml'], |
|||
'test': [], |
|||
'installable': True, |
|||
'auto_install': False, |
|||
} |
@ -0,0 +1,19 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<openerp> |
|||
<data noupdate="1"> |
|||
|
|||
<record forcecreate="True" id="ir_cron_rusty_record_reaper_runner" model="ir.cron"> |
|||
<field name="name">Rusty Record Reaper Runner</field> |
|||
<field eval="True" name="active"/> |
|||
<field name="user_id" ref="base.user_root"/> |
|||
<field name="interval_number">1</field> |
|||
<field name="interval_type">months</field> |
|||
<field name="numbercall">-1</field> |
|||
<field eval="False" name="doall"/> |
|||
<field eval="'record.lifespan'" name="model"/> |
|||
<field eval="'_scheduler_rusty_record_reaper'" name="function"/> |
|||
<field eval="'()'" name="args"/> |
|||
</record> |
|||
|
|||
</data> |
|||
</openerp> |
@ -0,0 +1,3 @@ |
|||
from . import company |
|||
from . import res_config |
|||
from . import record_lifespan |
@ -0,0 +1,30 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# |
|||
# Author: Yannick Vaucher |
|||
# Copyright 2015 Camptocamp SA |
|||
# |
|||
# This program is free software: you can redistribute it and/or modify |
|||
# it under the terms of the GNU Affero General Public License as |
|||
# published by the Free Software Foundation, either version 3 of the |
|||
# License, or (at your option) any later version. |
|||
# |
|||
# This program is distributed in the hope that it will be useful, |
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
# GNU Affero General Public License for more details. |
|||
# |
|||
# You should have received a copy of the GNU Affero General Public License |
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
# |
|||
from openerp.osv import orm, fields |
|||
|
|||
|
|||
class Company(orm.Model): |
|||
_inherit = 'res.company' |
|||
|
|||
_columns = { |
|||
'lifespan_ids': fields.one2many( |
|||
'record.lifespan', |
|||
'company_id', |
|||
string="Record Lifespans"), |
|||
} |
@ -0,0 +1,112 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# |
|||
# Author: Yannick Vaucher |
|||
# Copyright 2015 Camptocamp SA |
|||
# |
|||
# This program is free software: you can redistribute it and/or modify |
|||
# it under the terms of the GNU Affero General Public License as |
|||
# published by the Free Software Foundation, either version 3 of the |
|||
# License, or (at your option) any later version. |
|||
# |
|||
# This program is distributed in the hope that it will be useful, |
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
# GNU Affero General Public License for more details. |
|||
# |
|||
# You should have received a copy of the GNU Affero General Public License |
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
# |
|||
import logging |
|||
|
|||
from datetime import datetime |
|||
from dateutil.relativedelta import relativedelta |
|||
|
|||
from openerp.osv import orm, fields, osv |
|||
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DATE_FORMAT |
|||
from openerp.tools.translate import _ |
|||
|
|||
_logger = logging.getLogger(__name__) |
|||
|
|||
|
|||
class RecordLifespan(orm.Model): |
|||
"""Instead of writing this info on ir.model |
|||
here is a new object to be able to configure rec lifespan |
|||
per company |
|||
""" |
|||
_name = 'record.lifespan' |
|||
|
|||
_order = 'model' |
|||
|
|||
_columns = { |
|||
'model': fields.char( |
|||
"Model", |
|||
required=True), |
|||
'months': fields.integer( |
|||
"Months", |
|||
required=True, |
|||
help="Number of month after which the records will be set to " |
|||
"inactive based on write date"), |
|||
'company_id': fields.many2one( |
|||
'res.company', |
|||
string="Company", |
|||
ondelete="cascade", |
|||
required=True), |
|||
} |
|||
|
|||
_sql_constraints = [ |
|||
('model_uniq', 'unique(model, company_id)', |
|||
"A model can only have 1 lifespan per company"), |
|||
('months_gt_0', 'check (months > 0)', |
|||
"Months must be a value greater than 0"), |
|||
] |
|||
|
|||
def _scheduler_rusty_record_reaper(self, cr, uid, context=None): |
|||
lifespan_ids = self.search(cr, uid, [], context=context) |
|||
_logger.info('Record Reaper starts harvesting rusty records') |
|||
for lifespan_id in lifespan_ids: |
|||
try: |
|||
self.harvest_rusty_records( |
|||
cr, uid, [lifespan_id], context=context) |
|||
except osv.except_osv as e: |
|||
_logger.error("Reaper error:\n%s", e[1]) |
|||
_logger.info('Rusty Records now rest in peace') |
|||
return True |
|||
|
|||
def harvest_rusty_records(self, cr, uid, ids, context=None): |
|||
""" Search and deactivate old records for each configured lifespan |
|||
|
|||
Only done and cancelled records will be deactivated. |
|||
""" |
|||
if context is None: |
|||
context = {} |
|||
lifespans = self.browse(cr, uid, ids, context=context) |
|||
today = datetime.today() |
|||
for lifespan in lifespans: |
|||
|
|||
model = self.pool.get(lifespan.model) |
|||
if not model: |
|||
raise osv.except_osv( |
|||
_('Error'), |
|||
_('Model %s not found') % lifespan.model) |
|||
if 'active' not in model._columns.keys(): |
|||
raise osv.except_osv( |
|||
_('Error'), |
|||
_('Model %s has no active field') % lifespan.model) |
|||
delta = relativedelta(months=lifespan.months) |
|||
expiration_date = (today - delta).strftime(DATE_FORMAT) |
|||
domain = [('write_date', '<', expiration_date), |
|||
('company_id', '=', lifespan.company_id.id)] |
|||
if 'state' in model._columns.keys(): |
|||
domain += [('state', 'in', ('done', 'cancel'))] |
|||
rec_ids = model.search(cr, uid, domain, context=context) |
|||
|
|||
if not rec_ids: |
|||
continue |
|||
# use a SQL query to bypass tracking always messages on write for |
|||
# object inheriting mail.thread |
|||
query = ("UPDATE %s SET active = FALSE WHERE id in %%s" |
|||
) % model._table |
|||
cr.execute(query, (tuple(rec_ids),)) |
|||
_logger.info( |
|||
'Archived %s %s older than %s', |
|||
len(rec_ids), lifespan.model, expiration_date) |
@ -0,0 +1,70 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# |
|||
# Author: Yannick Vaucher |
|||
# Copyright 2015 Camptocamp SA |
|||
# |
|||
# This program is free software: you can redistribute it and/or modify |
|||
# it under the terms of the GNU Affero General Public License as |
|||
# published by the Free Software Foundation, either version 3 of the |
|||
# License, or (at your option) any later version. |
|||
# |
|||
# This program is distributed in the hope that it will be useful, |
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
# GNU Affero General Public License for more details. |
|||
# |
|||
# You should have received a copy of the GNU Affero General Public License |
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
# |
|||
import logging |
|||
|
|||
from openerp.osv import orm, fields |
|||
|
|||
_logger = logging.getLogger(__name__) |
|||
|
|||
|
|||
class RustyRecordReaperRunnerConfigSettings(orm.TransientModel): |
|||
_name = 'rusty.record.reaper.runner.config.settings' |
|||
_inherit = 'res.config.settings' |
|||
|
|||
_columns = { |
|||
'company_id': fields.many2one('res.company', 'Company', required=True), |
|||
'lifespan_ids': fields.related( |
|||
'company_id', 'lifespan_ids', |
|||
string='Record Lifespans', |
|||
type='one2many', |
|||
relation='record.lifespan'), |
|||
} |
|||
|
|||
def _default_company(self, cr, uid, context=None): |
|||
user = self.pool.get('res.users').browse(cr, uid, uid, context=context) |
|||
return user.company_id.id |
|||
|
|||
_defaults = { |
|||
'company_id': _default_company, |
|||
} |
|||
|
|||
def create(self, cr, uid, values, context=None): |
|||
rec_id = super(RustyRecordReaperRunnerConfigSettings, self |
|||
).create(cr, uid, values, context=context) |
|||
# Hack: to avoid some nasty bug, related fields are not written upon |
|||
# record creation. |
|||
# Hence we write on those fields here. |
|||
vals = {} |
|||
for fname, field in self._columns.iteritems(): |
|||
if isinstance(field, fields.related) and fname in values: |
|||
vals[fname] = values[fname] |
|||
self.write(cr, uid, [rec_id], vals, context=context) |
|||
return id |
|||
|
|||
def onchange_company_id(self, cr, uid, ids, company_id, context=None): |
|||
# update related fields |
|||
if not company_id: |
|||
return {'value': {}} |
|||
company = self.pool.get('res.company' |
|||
).browse(cr, uid, company_id, context=context) |
|||
lifespan_ids = [l.id for l in company.lifespan_ids] |
|||
values = { |
|||
'lifespan_ids': lifespan_ids, |
|||
} |
|||
return {'value': values} |
After Width: 55 | Height: 65 | Size: 5.7 KiB |
@ -0,0 +1,58 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<openerp> |
|||
<data> |
|||
|
|||
<record id="view_rusty_record_reaper_runner_config_settings" model="ir.ui.view"> |
|||
<field name="name">rusty record reaper runner settings</field> |
|||
<field name="model">rusty.record.reaper.runner.config.settings</field> |
|||
<field name="arch" type="xml"> |
|||
<form string="Configure Rusty Reacord Reaper Runner" version="7.0" class="oe_form_configuration"> |
|||
<header> |
|||
<button string="Apply" type="object" name="execute" class="oe_highlight"/> |
|||
or |
|||
<button string="Cancel" type="object" name="cancel" class="oe_link"/> |
|||
</header> |
|||
|
|||
<group groups="base.group_multi_company"> |
|||
<div> |
|||
<div> |
|||
<label for="company_id" string="Select Company"/> |
|||
<field name="company_id" |
|||
widget="selection" |
|||
on_change="onchange_company_id(company_id, context)" |
|||
class="oe_inline"/> |
|||
</div> |
|||
</div> |
|||
</group> |
|||
<separator string="Record life span"/> |
|||
All following type of record will be harvested by the cron based on write_date. If a record has made his time, it will be deactivated. |
|||
<group> |
|||
<div> |
|||
<div> |
|||
<field name="lifespan_ids" class="oe_inline"> |
|||
<tree editable="bottom"> |
|||
<field name="model"/> |
|||
<field name="months"/> |
|||
</tree> |
|||
</field> |
|||
</div> |
|||
</div> |
|||
</group> |
|||
</form> |
|||
</field> |
|||
</record> |
|||
|
|||
<record id="action_rusty_record_reaper_runner_config" model="ir.actions.act_window"> |
|||
<field name="name">Configure Rusty Record Reaper Runner</field> |
|||
<field name="type">ir.actions.act_window</field> |
|||
<field name="res_model">rusty.record.reaper.runner.config.settings</field> |
|||
<field name="view_mode">form</field> |
|||
<field name="target">inline</field> |
|||
</record> |
|||
|
|||
<menuitem id="menu_rusty_record_reaper_runner_config" name="Rusty Record Reaper Runner" |
|||
parent="base.menu_config" |
|||
sequence="70" action="action_rusty_record_reaper_runner_config"/> |
|||
|
|||
</data> |
|||
</openerp> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue