[ADD] beginning of the pingen module

This commit is contained in:
Guewen Baconnier @ Camptocamp
2012-11-19 11:55:37 +01:00
commit d7b736451b
6 changed files with 317 additions and 0 deletions

23
pingen/__init__.py Normal file
View File

@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Guewen Baconnier
# Copyright 2012 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 ir_attachment
import pingen_task

57
pingen/__openerp__.py Normal file
View File

@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Guewen Baconnier
# Copyright 2012 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' : 'pingen.com integration',
'version' : '1.0',
'author' : 'Camptocamp',
'maintainer': 'Camptocamp',
'license': 'AGPL-3',
'category': 'category',
'complexity': 'easy',
'depends' : [],
'description': """
Integration with pingen.com
===========================
What is pingen.com
------------------
Pingen.com is a paid online service.
It send uploaded documents by letter post.
Integration
-----------
One can decide, per document / attachment, if it should be pushed
to pingen.com. The documents are pushed asynchronously.
""",
'website': 'http://www.camptocamp.com',
'init_xml': [],
'update_xml': [
'ir_attachment_view.xml',
'pingen_task_view.xml',
],
'demo_xml': [],
'tests': [],
'installable': True,
'auto_install': False,
}

90
pingen/ir_attachment.py Normal file
View File

@@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Guewen Baconnier
# Copyright 2012 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 osv, orm, fields
from openerp.tools.translate import _
class ir_attachment(orm.Model):
_inherit = 'ir.attachment'
_columns = {
'send_to_pingen': fields.boolean('Send to Pingen.com'),
'pingen_task_ids': fields.one2many(
'pingen.task', 'attachment_id',
string="Pingen Task", readonly=True),
}
def _prepare_pingen_task_vals(self, cr, uid, attachment, context=None):
return {'attachment_id': attachment.id,
'config': 'created from attachment'}
def _handle_pingen_task(self, cr, uid, attachment_id, context=None):
""" Reponsible of the related ``pingen.task`` when the ``send_to_pingen``
field is modified.
Only one pingen task can be created per attachment.
When ``send_to_pingen`` is activated:
* Create a ``pingen.task`` if it does not already exist
* Put the related ``pingen.task`` to ``pending`` if it already exist
When it is deactivated:
* Do nothing if no related ``pingen.task`` exists
* Or cancel it
* If it has already been pushed to pingen.com, raises
an `osv.except_osv` exception
"""
pingen_task_obj = self.pool.get('pingen.task')
attachment = self.browse(cr, uid, attachment_id, context=context)
task = attachment.pingen_task_ids[0] if attachment.pingen_task_ids else None
if attachment.send_to_pingen:
if task:
task.write({'state': 'pending'}, context=context)
else:
pingen_task_obj.create(
cr, uid,
self._prepare_pingen_task_vals(
cr, uid, attachment, context=context),
context=context)
else:
if task:
if task.state == 'pushed':
raise osv.except_osv(
_('Error'),
_('The attachment %s is already pushed to pingen.com.') % \
attachment.name)
task.write({'state': 'canceled'}, context=context)
return
def create(self, cr, uid, vals, context=None):
attachment_id = super(ir_attachment, self).create(cr, uid, vals, context=context)
if 'send_to_pingen' in vals:
self._handle_pingen_task(cr, uid, attachment_id, context=context)
return attachment_id
def write(self, cr, uid, ids, vals, context=None):
res = super(ir_attachment, self).write(cr, uid, ids, vals, context=context)
if 'send_to_pingen' in vals:
for attachment_id in ids:
self._handle_pingen_task(cr, uid, attachment_id, context=context)
return res

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="0">
<record id="view_attachment_form" model="ir.ui.view">
<field name="name">ir.attachment.pingen.view</field>
<field name="model">ir.attachment</field>
<field name="type">form</field>
<field name="inherit_id" ref="base.view_attachment_form"/>
<field name="arch" type="xml">
<page string="Notes" position="before">
<page string="Pingen.com">
<field name="send_to_pingen"/>
</page>
</page>
</field>
</record>
<act_window
context="{'search_default_attachment_id': [active_id], 'default_attachment_id': active_id}"
id="act_attachment_to_pingen_task"
name="Pingen Task"
groups=""
res_model="pingen.task"
src_model="ir.attachment"/>
</data>
</openerp>

57
pingen/pingen_task.py Normal file
View File

@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Guewen Baconnier
# Copyright 2012 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 pingen_task(orm.Model):
""" A pingen task is the state of the synchronization of
an attachment with pingen.com
It stores the configuration and the current state of the synchronization.
It also serves as a queue of documents to push to pingen.com
"""
_name = 'pingen.task'
_inherits = {'ir.attachment': 'attachment_id'}
_columns = {
'attachment_id': fields.many2one(
'ir.attachment', 'Document', required=True, ondelete='cascade'),
'state': fields.selection(
[('pending', 'Pending'),
('pushed', 'Pushed'),
('error', 'Error'),
('canceled', 'Canceled')],
string='State', readonly=True, required=True),
'config': fields.text('Configuration (tmp)'),
}
_defaults = {
'state': 'pending',
}
_sql_constraints = [
('pingen_task_attachment_uniq',
'unique (attachment_id)',
'Only one Pingen task is allowed per attachment.'),
]

View File

@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="0">
<record id="view_pingen_task_tree" model="ir.ui.view">
<field name="name">pingen.task.tree</field>
<field name="model">pingen.task</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree string="Pingen Task">
<field name="config"/>
<field name="state"/>
</tree>
</field>
</record>
<record id="view_pingen_task_form" model="ir.ui.view">
<field name="name">pingen.task.form</field>
<field name="model">pingen.task</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Pingen Task">
<field name="config"/>
<field name="state"/>
</form>
</field>
</record>
<record id="view_pingen_task_search" model="ir.ui.view">
<field name="name">pingen.task.search</field>
<field name="model">pingen.task</field>
<field name="type">search</field>
<field name="arch" type="xml">
<search string="Pingen Task">
<filter icon="terp-stage"
string="Pending"
domain="[('state','=','pending')]"/>
<filter icon="terp-stock_align_left_24"
string="Pushed"
domain="[('state','=','pushed')]"/>
<filter icon="terp-stock_align_left_24"
string="Error"
domain="[('state','=','error')]"/>
<separator orientation="vertical"/>
<field name="attachment_id" />
</search>
</field>
</record>
<record id="action_pingen_task" model="ir.actions.act_window">
<field name="name">Pingen Tasks</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">pingen.task</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="search_view_id" ref="view_pingen_task_search"/>
</record>
<menuitem action="action_pingen_task" id="menu_pingen_task" parent="base.next_id_4"/>
</data>
</openerp>