Merge lp:~akretion-team/openobject-extension/openobject-extension-abstract-automatic-task into lp:openobject-extension/oerp6.1-stable

Proposed by Sébastien BEAU - http://www.akretion.com on 2013-04-22
Status: Merged
Merged at revision: 397
Proposed branch: lp:~akretion-team/openobject-extension/openobject-extension-abstract-automatic-task
Merge into: lp:openobject-extension/oerp6.1-stable
Diff against target: 297 lines (+269/-0)
5 files modified
abstract_automatic_task/__init__.py (+23/-0)
abstract_automatic_task/__openerp__.py (+43/-0)
abstract_automatic_task/abstrack_task_view.xml (+53/-0)
abstract_automatic_task/abstract_task.py (+147/-0)
abstract_automatic_task/security/ir.model.access.csv (+3/-0)
To merge this branch: bzr merge lp:~akretion-team/openobject-extension/openobject-extension-abstract-automatic-task
Reviewer Review Type Date Requested Status
Guewen Baconnier @ Camptocamp 2013-04-22 Pending
Review via email: mp+160201@code.launchpad.net

Description of the change

Add the module abstract_automatic_task
This module introduce the abstract class "automatic task" the aim is to have a generic solution for managing cron for a recuring task
An exemple of use can be found here => http://bazaar.launchpad.net/~akretion-team/+junk/file-exchange/view/head:/file_repository/file_repository.py

For V7 a more advanced version will be done and we will use it for all synchronisation task. (each task will inherit the "abstract_task" and so this module will solve the cron configuration)

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'abstract_automatic_task'
2=== added file 'abstract_automatic_task/__init__.py'
3--- abstract_automatic_task/__init__.py 1970-01-01 00:00:00 +0000
4+++ abstract_automatic_task/__init__.py 2013-04-22 20:21:34 +0000
5@@ -0,0 +1,23 @@
6+# -*- coding: utf-8 -*-
7+###############################################################################
8+#
9+# abstract_automatic_task for OpenERP
10+# Authors: Benoît Guillot <benoit.guillot@akretion.com>
11+# Copyright 2013 Akretion
12+#
13+# This program is free software: you can redistribute it and/or modify
14+# it under the terms of the GNU Affero General Public License as
15+# published by the Free Software Foundation, either version 3 of the
16+# License, or (at your option) any later version.
17+#
18+# This program is distributed in the hope that it will be useful,
19+# but WITHOUT ANY WARRANTY; without even the implied warranty of
20+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+# GNU Affero General Public License for more details.
22+#
23+# You should have received a copy of the GNU Affero General Public License
24+# along with this program. If not, see <http://www.gnu.org/licenses/>.
25+#
26+###############################################################################
27+
28+import abstract_task
29\ No newline at end of file
30
31=== added file 'abstract_automatic_task/__openerp__.py'
32--- abstract_automatic_task/__openerp__.py 1970-01-01 00:00:00 +0000
33+++ abstract_automatic_task/__openerp__.py 2013-04-22 20:21:34 +0000
34@@ -0,0 +1,43 @@
35+# -*- coding: utf-8 -*-
36+###############################################################################
37+#
38+# abstract_automatic_task for OpenERP
39+# Author: Benoît Guillot <benoit.guillot@akretion.com>
40+# Copyright 2013 Akretion
41+#
42+# This program is free software: you can redistribute it and/or modify
43+# it under the terms of the GNU Affero General Public License as
44+# published by the Free Software Foundation, either version 3 of the
45+# License, or (at your option) any later version.
46+#
47+# This program is distributed in the hope that it will be useful,
48+# but WITHOUT ANY WARRANTY; without even the implied warranty of
49+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50+# GNU Affero General Public License for more details.
51+#
52+# You should have received a copy of the GNU Affero General Public License
53+# along with this program. If not, see <http://www.gnu.org/licenses/>.
54+#
55+###############################################################################
56+
57+{
58+ 'name': 'abstract_automatic_task',
59+ 'version': '1.0',
60+ 'category': 'Generic Modules/Others',
61+ 'license': 'AGPL-3',
62+ 'description': """
63+Abstract tasks which can be inherited to create automaticaly task for an object.
64+ For instance automatic import tasks for connector with external application.
65+""",
66+ 'author': 'Akretion',
67+ 'website': 'http://www.akretion.com/',
68+ 'depends': ['base',
69+ ],
70+ 'demo': [],
71+ 'data': [
72+ 'abstrack_task_view.xml',
73+ 'security/ir.model.access.csv',
74+ ],
75+ 'installable': True,
76+ 'active': False,
77+}
78
79=== added file 'abstract_automatic_task/abstrack_task_view.xml'
80--- abstract_automatic_task/abstrack_task_view.xml 1970-01-01 00:00:00 +0000
81+++ abstract_automatic_task/abstrack_task_view.xml 2013-04-22 20:21:34 +0000
82@@ -0,0 +1,53 @@
83+<?xml version="1.0" encoding="utf-8"?>
84+<!--
85+ file_repository for OpenERP
86+ Copyright (C) 2013 Akretion
87+ Author: Benoît Guillot <benoit.guillot@akretion.com>
88+ The licence is in the file __openerp__.py
89+-->
90+
91+<openerp>
92+ <data>
93+ <!-- repeat the cron view to add a button to save and close the view -->
94+ <record model="ir.ui.view" id="automatic_cron_form_view">
95+ <field name="name">automatic_cron_form_view</field>
96+ <field name="model">ir.cron</field>
97+ <!-- priotity to be selected only with the button create cron -->
98+ <field name="priority">51</field>
99+ <field name="type">form</field>
100+ <field name="arch" type="xml">
101+ <form string="Scheduled Actions">
102+ <field name="name"/>
103+ <field name="active"/>
104+ <field name="user_id" />
105+ <field name="priority" />
106+ <notebook colspan="4">
107+ <page string="Information">
108+ <field name="interval_number"/>
109+ <field name="interval_type"/>
110+ <newline/>
111+ <field name="nextcall"/>
112+ <field name="numbercall"/>
113+ <field name="doall"/>
114+ </page>
115+ <page string="Technical Data" groups="base.group_extended">
116+ <separator string="Action to Trigger" colspan="4"/>
117+ <field name="model" groups="base.group_extended"/>
118+ <field name="function"/>
119+ <separator string="Arguments" colspan="4"/>
120+ <field colspan="4" name="args" nolabel="1"/>
121+ </page>
122+ </notebook>
123+ <group name="button" colspan="4">
124+ <button name="save_and_close_cron"
125+ string="Save and Close"
126+ type="object"/>
127+ <button special="cancel" string="Cancel"/>
128+ </group>
129+
130+ </form>
131+ </field>
132+ </record>
133+
134+ </data>
135+</openerp>
136
137=== added file 'abstract_automatic_task/abstract_task.py'
138--- abstract_automatic_task/abstract_task.py 1970-01-01 00:00:00 +0000
139+++ abstract_automatic_task/abstract_task.py 2013-04-22 20:21:34 +0000
140@@ -0,0 +1,147 @@
141+# -*- coding: utf-8 -*-
142+###############################################################################
143+#
144+# abstract_automatic_task for OpenERP
145+# Author: Benoît Guillot <benoit.guillot@akretion.com>
146+# Copyright 2013 Akretion
147+#
148+# This program is free software: you can redistribute it and/or modify
149+# it under the terms of the GNU Affero General Public License as
150+# published by the Free Software Foundation, either version 3 of the
151+# License, or (at your option) any later version.
152+#
153+# This program is distributed in the hope that it will be useful,
154+# but WITHOUT ANY WARRANTY; without even the implied warranty of
155+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
156+# GNU Affero General Public License for more details.
157+#
158+# You should have received a copy of the GNU Affero General Public License
159+# along with this program. If not, see <http://www.gnu.org/licenses/>.
160+#
161+###############################################################################
162+
163+from openerp.osv import fields, orm
164+from collections import defaultdict
165+from datetime import datetime, timedelta
166+from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
167+
168+
169+class automatic_task(orm.Model):
170+ _name = "automatic.task"
171+ _description = "Automatic Task"
172+
173+ def _get_task_type(self, cr, uid, context=None):
174+ return self.get_task_type(cr, uid, context=context)
175+
176+ def get_task_type(self, cr, uid, context=None):
177+ return []
178+
179+ _columns = {
180+ 'cron_ids': fields.many2many('ir.cron',
181+ 'cron_automatic_task_rel',
182+ 'cron_id',
183+ 'task_id',
184+ 'Cron Tasks'),
185+ 'active': fields.boolean('Active'),
186+ 'type': fields.selection(_get_task_type, 'Type'),
187+ #maybe use a fields.reference to various task to go easily from the automatic_task to the various task
188+ #in that case be careful that the relation is one2one between auto_task and the various task
189+ }
190+
191+ _defaults = {
192+ 'active': 1,
193+ }
194+
195+ def _run_all_task_for_cron(self, cr, uid, cron_id, context=None):
196+ for name, model in self.pool.models.iteritems():
197+ if model._inherits.get('automatic.task') == 'automatic_task_id' \
198+ and name != 'abstrack.task':
199+ task_ids = model.search(cr, uid,
200+ [('cron_ids', 'in', [cron_id])],
201+ context=context)
202+ model.run(cr, uid, task_ids, context=context)
203+ return True
204+
205+
206+class abstract_task(orm.AbstractModel):
207+ _name = "abstrack.task"
208+ _description = "Abstrack Task"
209+ _inherits = {'automatic.task': 'automatic_task_id'}
210+
211+ _columns ={
212+ 'automatic_task_id': fields.many2one('automatic.task',
213+ string='Automatic Taks',
214+ required=True,
215+ ondelete="cascade"),
216+ }
217+
218+ def create_cron_from_abstract(self, cr, uid, ids, context=None):
219+ model_data_obj = self.pool['ir.model.data']
220+ cron_obj = self.pool['ir.cron']
221+ cron_ids = []
222+ for task in self.browse(cr, uid, ids, context=context):
223+ nextcall = datetime.now() + timedelta(minutes=20)
224+ nextcall_fmt = datetime.strftime(nextcall,
225+ DEFAULT_SERVER_DATETIME_FORMAT)
226+ create_vals = {
227+ 'auto_task_ids': [(4, task.automatic_task_id.id)],
228+ 'active': True,
229+ 'model': 'automatic.task',
230+ 'function': '_run_all_task_for_cron',
231+ 'name': task.name,
232+ 'nextcall': nextcall_fmt,
233+ 'doall': False,
234+ 'numbercall': -1,
235+ }
236+ cron_id = cron_obj.create(cr, uid, create_vals, context=context)
237+ write_vals = {'args': '[%s]' % cron_id}
238+ cron_obj.write(cr, uid, [cron_id], write_vals, context=context)
239+ cron_ids.append(cron_id)
240+ #if several crons are created, open a tree view instead of a form
241+ #maybe useless because it's rare to call this button with several task_ids
242+ view_id = False
243+ if len(cron_ids) > 1:
244+ view_name = 'ir_cron_view_tree'
245+ view_mode = 'tree,form'
246+ res_id = cron_ids
247+ target = False
248+ else:
249+ view_name = 'automatic_cron_form_view'
250+ res_id = cron_ids[0]
251+ view_mode = 'form'
252+ target = 'new'
253+ model_data_id = model_data_obj.search(cr, uid,
254+ [['model', '=', 'ir.ui.view'],
255+ ['name', '=', 'automatic_cron_form_view']
256+ ], context=context)
257+ if model_data_id:
258+ view_id = model_data_obj.read(cr, uid,
259+ model_data_id,
260+ fields=['res_id'])[0]['res_id']
261+ return {
262+ 'name': 'Cron',
263+ 'view_type': 'form',
264+ 'view_mode': view_mode,
265+ 'view_id': view_id and [view_id] or False,
266+ 'res_model': 'ir.cron',
267+ 'context': context,
268+ 'type': 'ir.actions.act_window',
269+ 'nodestroy': True,
270+ 'target': target,
271+ 'res_id': res_id,
272+ }
273+
274+
275+class ir_cron(orm.Model):
276+ _inherit = "ir.cron"
277+
278+ _columns = {
279+ 'auto_task_ids': fields.many2many('automatic.task',
280+ 'cron_automatic_task_rel',
281+ 'task_id',
282+ 'cron_id',
283+ 'Task'),
284+ }
285+
286+ def save_and_close_cron(self, cr, uid, ids, context=None):
287+ return {'type': 'ir.actions.act_window_close'}
288\ No newline at end of file
289
290=== added directory 'abstract_automatic_task/security'
291=== added file 'abstract_automatic_task/security/ir.model.access.csv'
292--- abstract_automatic_task/security/ir.model.access.csv 1970-01-01 00:00:00 +0000
293+++ abstract_automatic_task/security/ir.model.access.csv 2013-04-22 20:21:34 +0000
294@@ -0,0 +1,3 @@
295+"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
296+"access_abstract_automatic_task_automatic_task_group_manager","abstract_automatic_task_automatic_task_group_manager","model_automatic_task","base.group_system",1,1,1,1
297+"access_abstract_automatic_task_automatic_task_group_user","abstract_automatic_task_automatic_task_group_user","model_automatic_task","base.group_user",1,0,0,0