Browse Source

Merge pull request #115 from acsone/7.0-base_export_email-lga

Add module base_export_email
pull/175/head
Alexandre Fayolle 9 years ago
parent
commit
893c788ab2
  1. 22
      base_export_email/__init__.py
  2. 56
      base_export_email/__openerp__.py
  3. 183
      base_export_email/ir_actions.py
  4. 12
      base_export_email/ir_actions_data.xml
  5. 44
      base_export_email/ir_actions_view.xml

22
base_export_email/__init__.py

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Authors: Laetitia Gangloff
# Copyright (c) 2015 Acsone SA/NV (http://www.acsone.eu)
#
# 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 . import ir_actions

56
base_export_email/__openerp__.py

@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Authors: Laetitia Gangloff
# Copyright (c) 2015 Acsone SA/NV (http://www.acsone.eu)
#
# 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": "Base export email addon",
"version": "0.1",
"author": "ACSONE SA/NV, Odoo Community Association (OCA)",
"category": "Other",
"website": "http://www.acsone.eu",
"depends": ['email_template',
],
"description": """
Base export email addon
=========================
Add server action type to send by email a data export
For this new action type you need to fill the following fields :
- Filter : it defines the filter to use on the data.
- Template : it is the mail template to send the email.
- Saved export : it is a saved export list.
- Fields to export : it is the list of field to export. It can be used alone
or to complete the saved export list.
Example of use : in a cron for a periodic export of some data
to see the evolution.
""",
"data": ["ir_actions_view.xml",
"ir_actions_data.xml"],
"demo": [],
"test": [],
"active": False,
"license": "AGPL-3",
"installable": True,
"auto_install": False,
"application": False,
}

183
base_export_email/ir_actions.py

@ -0,0 +1,183 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Authors: Laetitia Gangloff
# Copyright (c) 2015 Acsone SA/NV (http://www.acsone.eu)
#
# 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
from openerp.tools.translate import _
from openerp.addons.web.controllers import main
from openerp.tools.safe_eval import safe_eval
import time
from dateutil.relativedelta import relativedelta
from datetime import datetime
import base64
class actions_server(orm.Model):
_inherit = "ir.actions.server"
def __init__(self, pool, cr):
super(actions_server, self).__init__(pool, cr)
# add state 'export_email'
new_state = ('export_email', _('Export data by email'))
if new_state not in self._columns['state'].selection:
self._columns['state'].selection.append(new_state)
return
_columns = {
'model': fields.related('model_id', 'model',
type="char", size=256, string='Model'),
'filter_id': fields.many2one(
'ir.filters', string='Filter', ondelete='restrict'),
'email_template_id': fields.many2one(
'email.template', string='Template', ondelete='restrict'),
'saved_export_id': fields.many2one(
'ir.exports', string='Saved export', ondelete='restrict'),
'fields_to_export': fields.text(
'Fields to export',
help="The list of fields to export. \
The format is ['name', 'seller_ids/product_name']"),
'export_format': fields.selection([('csv', 'CSV'),
('xls', 'Excel')],
'Export Formats'),
}
def _get_email_template(self, cr, uid, context=None):
email_template_id = 0
try:
model_data_obj = self.pool['ir.model.data']
email_template_id = model_data_obj.get_object_reference(
cr, uid, 'base_export_email', 'export_data_email_template')[1]
except ValueError:
pass
return email_template_id
_defaults = {
'fields_to_export': '[]',
'export_format': 'csv',
'email_template_id': lambda self, cr, uid, c:
self._get_email_template(cr, uid, context=c),
}
def onchange_model_id(self, cr, uid, ids, model_id, context=None):
"""
Used to set correct domain on filter_id and saved_export_id
"""
data = {'model': False,
'filter_id': False,
'saved_export_id': False}
if model_id:
model = self.pool['ir.model'].browse(cr, uid, model_id,
context=context)
data.update({'model': model.model})
return {'value': data}
def _search_data(self, cr, uid, action, context=None):
obj_pool = self.pool[action.model]
domain = action.filter_id and safe_eval(
action.filter_id.domain, {'time': time,
'datetime': datetime,
'relativedelta': relativedelta,
}) or []
ctx = action.filter_id and safe_eval(
action.filter_id.context) or context
return obj_pool.search(cr, uid, domain,
offset=0, limit=False, order=False,
context=ctx)
def _export_data(self, cr, uid, obj_ids, action, context=None):
obj_pool = self.pool[action.model]
export_fields = []
if action.fields_to_export:
export_fields = safe_eval(action.fields_to_export)
if action.saved_export_id:
# retrieve fields of the selected list
export_fields.extend(
[efl.name for efl in
action.saved_export_id.export_fields])
return export_fields, obj_pool.export_data(
cr, uid, obj_ids, export_fields,
context=context).get('datas', [])
def _send_data_email(self, cr, uid, action, export_fields, export_data,
context=None):
"""
Prepare the exported data to send with the template
of the configuration
"""
if action.export_format == 'csv':
export = main.CSVExport()
else:
export = main.ExcelExport()
filename = export.filename(action.model)
content = export.from_data(export_fields, export_data)
return self._send_email(cr, uid, action, filename, content,
context=context)
def _send_email(self, cr, uid, action, filename, content,
context=None):
"""
Prepare a message with the exported data to send with the
template of the configuration
"""
mail_compose = self.pool['mail.compose.message']
values = mail_compose.onchange_template_id(
cr, uid, 0, action.email_template_id, 'comment',
'ir.actions.server', action.id, context=context)['value']
values['partner_ids'] = [
(4, partner_id) for partner_id in values.pop('partner_ids',
[])
]
if context and context.get('encoded_base_64'):
data = content
else:
if isinstance(content, unicode):
content = content.encode('utf-8')
data = base64.b64encode(str(content))
data_attach = {
'name': filename,
'datas': data,
'datas_fname': filename,
'description': filename,
}
values['attachment_ids'] = [(0, 0, data_attach)]
compose_id = mail_compose.create(
cr, uid, values, context=context)
return mail_compose.send_mail(cr, uid, [compose_id], context=context)
def run(self, cr, uid, ids, context=None):
"""
If the state of an action is export_email,
export data related to the configuration and send the result by email
"""
for action in self.browse(cr, uid, ids, context):
if action.state == 'export_email':
# search data to export
obj_ids = self._search_data(cr, uid, action, context=context)
# export data
export_fields, export_data = self._export_data(
cr, uid, obj_ids, action, context=context)
# Prepare a message with the exported data to send with the
# template of the configuration
self._send_data_email(
cr, uid, action, export_fields, export_data,
context=context)
return super(actions_server, self).run(cr, uid, ids, context=context)

12
base_export_email/ir_actions_data.xml

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="1">
<record id="export_data_email_template" model="email.template">
<field name="name">Export data sample</field>
<field name="subject">Export data for ${object.model}</field>
<field name="model_id" ref="base.model_ir_actions_server" />
<field name="auto_delete" eval="True"/>
<field name="body_html"><![CDATA[<div></div>]]></field>
</record>
</data>
</openerp>

44
base_export_email/ir_actions_view.xml

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="view_server_action_form" model="ir.ui.view">
<field name="name">Server Action (base_export_email)</field>
<field name="model">ir.actions.server</field>
<field name="inherit_id" ref="base.view_server_action_form"/>
<field name="arch" type="xml">
<field name="model_id" position="attributes">
<attribute name="on_change">onchange_model_id(model_id, context)</attribute>
</field>
<field name="model_id" position="after">
<field name="model" invisible="1"/>
</field>
<field name="srcmodel_id" position="attributes">
<attribute name="attrs">{'required': [('state', '!=', 'export_email'),
('state','!=','dummy'),
('state','!=','sms'),
('state','!=','code'),
('state','!=','loop'),
('state','!=','trigger'),
('state','!=','object_copy'),
('state','!=','client_action'),
('state','!=','email'),
('state','!=','sms'),
('state','!=','other')]}</attribute>
</field>
<notebook position="inside">
<page string="Export data by email configuration" attrs="{'invisible':[('state','!=','export_email')]}">
<group>
<field name="export_format" attrs="{'required':[('state','=','export_email')]}"/>
<field name="filter_id" domain="[('model_id','=',model)]"/>
<field name="email_template_id" attrs="{'required':[('state','=','export_email')]}"
domain="[('model_id.model', '=', 'ir.actions.server')]"/>
<field name ="fields_to_export" attrs="{'required': [('state', '=', 'export_email'), ('saved_export_id', '=', False)]}" />
<field name ="saved_export_id" attrs="{'required': [('state', '=', 'export_email'), ('fields_to_export', '=', False)]}"
domain="[('resource','=',model)]"/>
</group>
</page>
</notebook>
</field>
</record>
</data>
</openerp>
Loading…
Cancel
Save