From edc727bd0a1707aec9ad0e0029ac664035923596 Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Thu, 11 Apr 2013 10:26:45 +0200 Subject: [PATCH 01/18] [ADD] email_template_template --- email_template_template/__init__.py | 1 + email_template_template/__openerp__.py | 52 ++++++++++++++ email_template_template/model/__init__.py | 21 ++++++ .../model/email_template.py | 56 +++++++++++++++ .../view/email_template.xml | 71 +++++++++++++++++++ 5 files changed, 201 insertions(+) create mode 100644 email_template_template/__init__.py create mode 100644 email_template_template/__openerp__.py create mode 100644 email_template_template/model/__init__.py create mode 100644 email_template_template/model/email_template.py create mode 100644 email_template_template/view/email_template.xml diff --git a/email_template_template/__init__.py b/email_template_template/__init__.py new file mode 100644 index 000000000..16e8b082f --- /dev/null +++ b/email_template_template/__init__.py @@ -0,0 +1 @@ +import model diff --git a/email_template_template/__openerp__.py b/email_template_template/__openerp__.py new file mode 100644 index 000000000..502c6a170 --- /dev/null +++ b/email_template_template/__openerp__.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2012 Therp BV (). +# +# 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 . +# +############################################################################## +{ + "name": "Templates for email templates", + "version": "1.0", + "author": "Therp BV", + "category": 'Tools', + 'complexity': "expert", + "description": """If an organisation's email layout is a bit more +complicated, changes can be tedious when having to do that across several email +templates. So this addon allows to define templates for mails that is referenced +by other mail templates. +This way we can put the layout parts into the template template and only content +in the other templates. Changing the layout is then only a matter of changing +the template template. + + +Usage: +Create an email template with the related document model 'Email Templates'. Now +most of the fields gray out and you can only edit body_text and body_html. Be +sure to use ${body_text} and ${body_html} respectively in your template +template. + +Then select this newly created template templates in one of your actual +templates.""", + 'website': 'http://therp.nl', + 'images': [], + 'depends': ['email_template'], + 'data': [ + 'view/email_template.xml', + ], + "license": 'AGPL-3', +} +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/email_template_template/model/__init__.py b/email_template_template/model/__init__.py new file mode 100644 index 000000000..4f09daed4 --- /dev/null +++ b/email_template_template/model/__init__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2012 Therp BV (). +# +# 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 . +# +############################################################################## +import email_template diff --git a/email_template_template/model/email_template.py b/email_template_template/model/email_template.py new file mode 100644 index 000000000..6b7817b3c --- /dev/null +++ b/email_template_template/model/email_template.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2012 Therp BV (). +# +# 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 . +# +############################################################################## +from openerp.osv.orm import Model +from openerp.osv import fields + + +class email_template(Model): + _inherit = 'email.template' + + def _get_is_template_template(self, cr, uid, ids, fields_name, arg, + context=None): + cr.execute('''select + id, (select count(*) > 0 from email_template e + where email_template_id=email_template.id) + from email_template + where id in %s''', (tuple(ids),)) + return dict(cr.fetchall()) + + _columns = { + 'email_template_id': fields.many2one('email.template', 'Template'), + 'is_template_template': fields.function( + _get_is_template_template, type='boolean', + string='Is a template template'), + } + + def get_email_template(self, cr, uid, template_id=False, record_id=None, + context=None): + this = super(email_template, self).get_email_template( + cr, uid, template_id, record_id, context) + + if this.email_template_id and not this.is_template_template: + for field in ['body_html', 'body_text']: + if this[field] and this.email_template_id[field]: + this._data[this.id][field] = self.render_template( + cr, uid, this.email_template_id[field], + this.email_template_id.model, + this.id, this._context) + return this diff --git a/email_template_template/view/email_template.xml b/email_template_template/view/email_template.xml new file mode 100644 index 000000000..4ac8ec922 --- /dev/null +++ b/email_template_template/view/email_template.xml @@ -0,0 +1,71 @@ + + + + + email.template.form + email.template + + form + + + + + + + + + + {'readonly': [('is_template_template','=',True)]} + + + + 0 + + {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]} + + + + 0 + + {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]} + + + + + {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]} + + + + + {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]} + + + + + {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]} + + + + + {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]} + + + + + {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]} + + + + 0 + + {'readonly': ['|',('is_template_template','=',True),('model_id', '=', %(email_template.model_email_template)s)]} + + + + + + + From 06a2b4b3d2b2a6da94edaf8dd146091b5f42db85 Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Thu, 25 Apr 2013 14:43:59 +0200 Subject: [PATCH 02/18] [ADD] unserialize_field --- unserialize_field/__init__.py | 22 ++++++ unserialize_field/__openerp__.py | 36 +++++++++ unserialize_field/ir_model_fields.py | 101 ++++++++++++++++++++++++++ unserialize_field/ir_model_fields.xml | 20 +++++ 4 files changed, 179 insertions(+) create mode 100644 unserialize_field/__init__.py create mode 100644 unserialize_field/__openerp__.py create mode 100644 unserialize_field/ir_model_fields.py create mode 100644 unserialize_field/ir_model_fields.xml diff --git a/unserialize_field/__init__.py b/unserialize_field/__init__.py new file mode 100644 index 000000000..81f4e3698 --- /dev/null +++ b/unserialize_field/__init__.py @@ -0,0 +1,22 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2013 Therp BV () +# All Rights Reserved +# +# 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 . +# +############################################################################## +import ir_model_fields diff --git a/unserialize_field/__openerp__.py b/unserialize_field/__openerp__.py new file mode 100644 index 000000000..8d6d30be0 --- /dev/null +++ b/unserialize_field/__openerp__.py @@ -0,0 +1,36 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2013 Therp BV () +# All Rights Reserved +# +# 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 . +# +############################################################################## + +{ + 'name': 'Make database fields from fields that live in serialized fields', + 'version': '1.0', + 'description': """To be able to search for fields with standard methods, +they have to be database fields. This addon makes it possible to unserialize +them afterwards.""", + 'author': 'Therp BV', + 'website': 'http://www.therp.nl', + "category": "Tools", + "depends": [], + "data": ['ir_model_fields.xml'], + 'installable': True, + 'active': False, +} diff --git a/unserialize_field/ir_model_fields.py b/unserialize_field/ir_model_fields.py new file mode 100644 index 000000000..4c478546d --- /dev/null +++ b/unserialize_field/ir_model_fields.py @@ -0,0 +1,101 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2013 Therp BV () +# All Rights Reserved +# +# 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 . +# +############################################################################## +from openerp.osv.orm import Model +from openerp.osv import fields + + +class ir_model_fields(Model): + _inherit = 'ir.model.fields' + + def action_unserialize_field(self, cr, uid, ids, context=None): + step = 1000 + offset = 0 + + for this in self.browse(cr, uid, ids, context=context): + pool_obj = self.pool.get(this.model_id.model) + self.create_database_column(cr, uid, pool_obj, this.name) + while True: + ids = pool_obj.search( + cr, uid, + [(this.serialization_field_id.name, '!=', '{}')], + offset=offset*step, limit=step, context=context) + if not ids: + break + for data in pool_obj.read(cr, uid, ids, + [this.serialization_field_id.name], + context=context): + self.unserialize_field(cr, uid, pool_obj, data, + this.serialization_field_id.name, + this.name) + offset += 1 + return True + + def create_database_column(self, cr, uid, pool_obj, field_name): + old = pool_obj._columns[field_name] + field_declaration_args = [] + field_declaration_kwargs = dict( + manual=False, + string=old.string, + required=old.required, + readonly=old.readonly, + domain=old._domain, + context=old._context, + states=old.states, + priority=old.priority, + change_default=old.change_default, + size=old.size, + ondelete=old.ondelete, + translate=old.translate, + select=old.select, + ) + + if old._type == 'many2one': + field_declaration_args = [old._obj] + elif old._type == 'selection': + field_declaration_args = [old.selection] + elif old._type == 'one2many': + field_declaration_args = [old._obj, old._fields_id] + field_declaration_kwargs['limit'] = old._limit + elif old._type == 'many2many': + field_declaration_args = [old._obj] + field_declaration_kwargs['rel'] = old._rel + field_declaration_kwargs['id1'] = old._id1 + field_declaration_kwargs['id2'] = old._id2 + field_declaration_kwargs['limit'] = old._limit + + field_declaration = getattr(fields, old._type)( + *field_declaration_args, + **field_declaration_kwargs) + + pool_obj._columns[field_name] = field_declaration + pool_obj._auto_init(cr, {}) + + def unserialize_field(self, cr, uid, pool_obj, read_record, + serialization_field_name, field_name): + if not field_name in read_record[serialization_field_name]: + return False + pool_obj.write( + cr, uid, read_record['id'], + { + field_name: + read_record[serialization_field_name][field_name], + }) diff --git a/unserialize_field/ir_model_fields.xml b/unserialize_field/ir_model_fields.xml new file mode 100644 index 000000000..7be28d00c --- /dev/null +++ b/unserialize_field/ir_model_fields.xml @@ -0,0 +1,20 @@ + + + + + form + ir.model.fields + + + + +