From 18bdb1ecc06a28c71bcdebb39e9457b8d665034c Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Tue, 30 Jun 2015 11:18:51 +0200 Subject: [PATCH] Use a m2o for the models --- record_archiver/models/company.py | 2 +- record_archiver/models/record_lifespan.py | 23 ++++++++++++++++------- record_archiver/models/res_config.py | 8 ++++---- record_archiver/views/res_config.xml | 4 ++-- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/record_archiver/models/company.py b/record_archiver/models/company.py index 2b3c410a3..cf6a9c361 100644 --- a/record_archiver/models/company.py +++ b/record_archiver/models/company.py @@ -23,7 +23,7 @@ class Company(orm.Model): _inherit = 'res.company' _columns = { - 'lifespan_ids': fields.one2many( + 'record_lifespan_ids': fields.one2many( 'record.lifespan', 'company_id', string="Record Lifespans"), diff --git a/record_archiver/models/record_lifespan.py b/record_archiver/models/record_lifespan.py index 74d24d9ef..e6b1928dd 100644 --- a/record_archiver/models/record_lifespan.py +++ b/record_archiver/models/record_lifespan.py @@ -39,9 +39,18 @@ class RecordLifespan(orm.Model): _order = 'model' _columns = { - 'model': fields.char( - "Model", - required=True), + 'model_id': fields.many2one( + 'ir.model', + string='Model', + required=True, + ), + 'model': fields.related( + 'model_id', 'model', + string='Model Name', + type='char', + readonly=True, + store=True, + ), 'months': fields.integer( "Months", required=True, @@ -55,7 +64,7 @@ class RecordLifespan(orm.Model): } _sql_constraints = [ - ('model_uniq', 'unique(model, company_id)', + ('model_uniq', 'unique(model_id, 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"), @@ -81,7 +90,7 @@ class RecordLifespan(orm.Model): model = self.pool[lifespan.model] domain = [('write_date', '<', expiration_date), ('company_id', '=', lifespan.company_id.id)] - if 'state' in model._columns.keys(): + if 'state' in model._columns: domain += [('state', 'in', ('done', 'cancel'))] return domain @@ -95,12 +104,12 @@ class RecordLifespan(orm.Model): """ today = datetime.today() - model = self.pool[lifespan.model] + model = self.pool.get(lifespan.model) if not model: raise orm.except_orm( _('Error'), _('Model %s not found') % lifespan.model) - if 'active' not in model._columns.keys(): + if 'active' not in model._columns: raise orm.except_orm( _('Error'), _('Model %s has no active field') % lifespan.model) diff --git a/record_archiver/models/res_config.py b/record_archiver/models/res_config.py index fb6c2bf61..3ca7f4b9c 100644 --- a/record_archiver/models/res_config.py +++ b/record_archiver/models/res_config.py @@ -29,8 +29,8 @@ class RecordArchiverConfigSettings(orm.TransientModel): _columns = { 'company_id': fields.many2one('res.company', 'Company', required=True), - 'lifespan_ids': fields.related( - 'company_id', 'lifespan_ids', + 'record_lifespan_ids': fields.related( + 'company_id', 'record_lifespan_ids', string='Record Lifespans', type='one2many', relation='record.lifespan'), @@ -63,8 +63,8 @@ class RecordArchiverConfigSettings(orm.TransientModel): 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] + lifespan_ids = [l.id for l in company.record_lifespan_ids] values = { - 'lifespan_ids': lifespan_ids, + 'record_lifespan_ids': lifespan_ids, } return {'value': values} diff --git a/record_archiver/views/res_config.xml b/record_archiver/views/res_config.xml index 0642b4671..83d85306d 100644 --- a/record_archiver/views/res_config.xml +++ b/record_archiver/views/res_config.xml @@ -31,9 +31,9 @@
- + - +