1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
23 from osv import fields,osv
26 from tools.config import config
30 class actions(osv.osv):
31 _name = 'ir.actions.actions'
34 'name': fields.char('Action Name', required=True, size=64),
35 'type': fields.char('Action Type', required=True, size=32),
36 'usage': fields.char('Action Usage', size=32),
37 'parent_id': fields.many2one('ir.actions.server', 'Parent Action'),
40 'usage': lambda *a: False,
44 class report_custom(osv.osv):
45 _name = 'ir.actions.report.custom'
46 _table = 'ir_act_report_custom'
47 _sequence = 'ir_actions_id_seq'
49 'name': fields.char('Report Name', size=64, required=True, translate=True),
50 'type': fields.char('Report Type', size=32, required=True),
51 'model':fields.char('Object', size=64, required=True),
52 'report_id': fields.integer('Report Ref.', required=True),
53 'usage': fields.char('Action Usage', size=32),
54 'multi': fields.boolean('On multiple doc.', help="If set to true, the action will not be displayed on the right toolbar of a form views.")
57 'multi': lambda *a: False,
58 'type': lambda *a: 'ir.actions.report.custom',
62 class report_xml(osv.osv):
64 def _report_content(self, cursor, user, ids, name, arg, context=None):
66 for report in self.browse(cursor, user, ids, context=context):
67 data = report[name + '_data']
68 if not data and report[name[:-8]]:
70 fp = tools.file_open(report[name[:-8]], mode='rb')
77 def _report_content_inv(self, cursor, user, id, name, value, arg, context=None):
78 self.write(cursor, user, id, {name+'_data': value}, context=context)
80 def _report_sxw(self, cursor, user, ids, name, arg, context=None):
82 for report in self.browse(cursor, user, ids, context=context):
84 res[report.id] = report.report_rml.replace('.rml', '.sxw')
86 res[report.id] = False
89 _name = 'ir.actions.report.xml'
90 _table = 'ir_act_report_xml'
91 _sequence = 'ir_actions_id_seq'
93 'name': fields.char('Name', size=64, required=True, translate=True),
94 'type': fields.char('Report Type', size=32, required=True),
95 'model': fields.char('Object', size=64, required=True),
96 'report_name': fields.char('Internal Name', size=64, required=True),
97 'report_xsl': fields.char('XSL path', size=256),
98 'report_xml': fields.char('XML path', size=256),
99 'report_rml': fields.char('RML path', size=256,
100 help="The .rml path of the file or NULL if the content is in report_rml_content"),
101 'report_sxw': fields.function(_report_sxw, method=True, type='char',
103 'report_sxw_content_data': fields.binary('SXW content'),
104 'report_rml_content_data': fields.binary('RML content'),
105 'report_sxw_content': fields.function(_report_content,
106 fnct_inv=_report_content_inv, method=True,
107 type='binary', string='SXW content',),
108 'report_rml_content': fields.function(_report_content,
109 fnct_inv=_report_content_inv, method=True,
110 type='binary', string='RML content'),
111 'auto': fields.boolean('Automatic XSL:RML', required=True),
112 'usage': fields.char('Action Usage', size=32),
113 'header': fields.boolean('Add RML header',
114 help="Add or not the coporate RML header"),
115 'multi': fields.boolean('On multiple doc.',
116 help="If set to true, the action will not be displayed on the right toolbar of a form views."),
117 'report_type': fields.selection([
122 ], string='Type', required=True),
123 'groups_id': fields.many2many('res.groups', 'res_groups_report_rel', 'uid', 'gid', 'Groups'),
124 'attachment': fields.char('Save As Attachment Prefix', size=32, help='This is the prefix of the file name the print will be saved as attachement. Keep empty to not save the printed reports')
127 'type': lambda *a: 'ir.actions.report.xml',
128 'multi': lambda *a: False,
129 'auto': lambda *a: True,
130 'header': lambda *a: True,
131 'report_sxw_content': lambda *a: False,
132 'report_type': lambda *a: 'pdf',
133 'attachment': lambda *a: False,
138 class act_window(osv.osv):
139 _name = 'ir.actions.act_window'
140 _table = 'ir_act_window'
141 _sequence = 'ir_actions_id_seq'
143 # def search(self, cr, uid, args, offset=0, limit=2000, order=None,
144 # context=None, count=False):
145 # if context is None:
147 # ids = osv.orm.orm.search(self, cr, uid, args, offset, limit, order,
151 # user_groups = self.pool.get('res.users').read(cr, uid, [uid])[0]['groups_id']
153 # for act in self.browse(cr, uid, ids):
154 # if not len(act.groups_id):
155 # result.append(act.id)
157 # for g in act.groups_id:
158 # if g.id in user_groups:
159 # result.append(act.id)
163 def _views_get_fnc(self, cr, uid, ids, name, arg, context={}):
165 for act in self.browse(cr, uid, ids):
166 res[act.id]=[(view.view_id.id, view.view_mode) for view in act.view_ids]
167 modes = act.view_mode.split(',')
168 if len(modes)>len(act.view_ids):
171 res[act.id].append((act.view_id.id, act.view_id.type))
172 for t in modes[len(act.view_ids):]:
173 if act.view_id and (t == act.view_id.type) and not find:
176 res[act.id].append((False, t))
180 'name': fields.char('Action Name', size=64, translate=True),
181 'type': fields.char('Action Type', size=32, required=True),
182 'view_id': fields.many2one('ir.ui.view', 'View Ref.', ondelete='cascade'),
183 'domain': fields.char('Domain Value', size=250),
184 'context': fields.char('Context Value', size=250),
185 'res_model': fields.char('Object', size=64),
186 'src_model': fields.char('Source Object', size=64),
187 'target': fields.selection([('current','Current Window'),('new','New Window')], 'Target Window'),
188 'view_type': fields.selection((('tree','Tree'),('form','Form')),string='Type of view'),
189 'view_mode': fields.char('Mode of view', size=250),
190 'usage': fields.char('Action Usage', size=32),
191 'view_ids': fields.one2many('ir.actions.act_window.view', 'act_window_id', 'Views'),
192 'views': fields.function(_views_get_fnc, method=True, type='binary', string='Views'),
193 'limit': fields.integer('Limit', help='Default limit for the list view'),
194 'auto_refresh': fields.integer('Auto-Refresh',
195 help='Add an auto-refresh on the view'),
196 'groups_id': fields.many2many('res.groups', 'ir_act_window_group_rel',
197 'act_id', 'gid', 'Groups'),
200 'type': lambda *a: 'ir.actions.act_window',
201 'view_type': lambda *a: 'form',
202 'view_mode': lambda *a: 'tree,form',
203 'context': lambda *a: '{}',
204 'limit': lambda *a: 80,
205 'target': lambda *a: 'current',
206 'auto_refresh': lambda *a: 0,
210 class act_window_view(osv.osv):
211 _name = 'ir.actions.act_window.view'
212 _table = 'ir_act_window_view'
213 _rec_name = 'view_id'
215 'sequence': fields.integer('Sequence'),
216 'view_id': fields.many2one('ir.ui.view', 'View'),
217 'view_mode': fields.selection((
221 ('calendar', 'Calendar'),
222 ('gantt', 'Gantt')), string='Type of view', required=True),
223 'act_window_id': fields.many2one('ir.actions.act_window', 'Action', ondelete='cascade'),
224 'multi': fields.boolean('On multiple doc.',
225 help="If set to true, the action will not be displayed on the right toolbar of a form views."),
228 'multi': lambda *a: False,
233 class act_wizard(osv.osv):
234 _name = 'ir.actions.wizard'
235 _table = 'ir_act_wizard'
236 _sequence = 'ir_actions_id_seq'
238 'name': fields.char('Wizard info', size=64, required=True, translate=True),
239 'type': fields.char('Action type', size=32, required=True),
240 'wiz_name': fields.char('Wizard name', size=64, required=True),
241 'multi': fields.boolean('Action on multiple doc.', help="If set to true, the wizard will not be displayed on the right toolbar of a form views."),
242 'groups_id': fields.many2many('res.groups', 'res_groups_wizard_rel', 'uid', 'gid', 'Groups'),
243 'model': fields.char('Object', size=64),
246 'type': lambda *a: 'ir.actions.wizard',
247 'multi': lambda *a: False,
251 class act_url(osv.osv):
252 _name = 'ir.actions.url'
253 _table = 'ir_act_url'
254 _sequence = 'ir_actions_id_seq'
256 'name': fields.char('Action Name', size=64, translate=True),
257 'type': fields.char('Action Type', size=32, required=True),
258 'url': fields.text('Action Url',required=True),
259 'target': fields.selection((
260 ('new', 'New Window'),
261 ('self', 'This Window')),
262 'Action Target', required=True
266 'type': lambda *a: 'ir.actions.act_url',
267 'target': lambda *a: 'new'
271 def model_get(self, cr, uid, context={}):
272 wkf_pool = self.pool.get('workflow')
273 ids = wkf_pool.search(cr, uid, [])
274 osvs = wkf_pool.read(cr, uid, ids, ['osv'])
277 mpool = self.pool.get('ir.model')
279 model = osv.get('osv')
280 id = mpool.search(cr, uid, [('model','=',model)])
281 name = mpool.read(cr, uid, id)[0]['name']
282 res.append((model, name))
286 class ir_model_fields(osv.osv):
287 _inherit = 'ir.model.fields'
288 _rec_name = 'field_description'
290 'complete_name': fields.char('Complete Name', size=64, select=1),
293 def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=80):
294 def get_fields(cr, uid, field, rel):
296 mobj = self.pool.get('ir.model')
297 id = mobj.search(cr, uid, [('model','=',rel)])
299 obj = self.pool.get('ir.model.fields')
300 ids = obj.search(cr, uid, [('model_id','in',id)])
301 records = obj.read(cr, uid, ids)
302 for record in records:
304 fld = field + '/' + record['name']
306 result.append((id, fld))
313 return super(ir_model_fields, self).name_search(cr, uid, name, args, operator, context, limit)
315 if context.get('key') != 'server_action':
316 return super(ir_model_fields, self).name_search(cr, uid, name, args, operator, context, limit)
319 obj = self.pool.get('ir.model.fields')
320 ids = obj.search(cr, uid, args)
321 records = obj.read(cr, uid, ids)
322 for record in records:
324 field = record['name']
326 if record['ttype'] == 'many2one':
327 rel = record['relation']
328 res = get_fields(cr, uid, field, record['relation'])
332 result.append((id, field))
335 obj.write(cr, uid, [rs[0]], {'complete_name':rs[1]})
341 result = super(ir_model_fields, self).name_search(cr, uid, name, [('complete_name','ilike',name), ('id','in',iids)], operator, context, limit)
347 class server_object_lines(osv.osv):
348 _name = 'ir.server.object.lines'
349 _sequence = 'ir_actions_id_seq'
351 'server_id': fields.many2one('ir.actions.server', 'Object Mapping'),
352 'col1': fields.many2one('ir.model.fields', 'Destination', required=True),
353 'value': fields.text('Value', required=True),
354 'type': fields.selection([
356 ('equation','Formula')
357 ], 'Type', required=True, size=32, change_default=True),
360 'type': lambda *a: 'equation',
362 server_object_lines()
365 # Actions that are run on the server side
367 class actions_server(osv.osv):
368 _name = 'ir.actions.server'
369 _table = 'ir_act_server'
370 _sequence = 'ir_actions_id_seq'
372 'name': fields.char('Action Name', required=True, size=64),
373 'state': fields.selection([
374 ('client_action','Client Action'),
375 ('python','Python Code'),
377 ('trigger','Trigger'),
380 ('object_create','Create Object'),
381 ('object_write','Write Object'),
382 ('other','Others Actions'),
383 ], 'Action State', required=True, size=32),
384 'code': fields.text('Python Code'),
385 'sequence': fields.integer('Sequence'),
386 'model_id': fields.many2one('ir.model', 'Object', required=True),
387 'action_id': fields.many2one('ir.actions.actions', 'Client Action'),
388 'trigger_name': fields.char('Trigger Name', size=128),
389 'trigger_obj_id': fields.reference('Trigger On', selection=model_get, size=128),
390 'email': fields.many2one('ir.model.fields', 'Contact'),
391 'message': fields.text('Message', translate=True),
392 'mobile': fields.many2one('ir.model.fields', 'Contact'),
393 'sms': fields.char('SMS', size=160, translate=True),
394 'child_ids': fields.one2many('ir.actions.actions', 'parent_id', 'Others Actions'),
395 'usage': fields.char('Action Usage', size=32),
396 'type': fields.char('Report Type', size=32, required=True),
397 'srcmodel_id': fields.many2one('ir.model', 'Model'),
398 'fields_lines': fields.one2many('ir.server.object.lines', 'server_id', 'Fields Mapping'),
399 'otype': fields.selection([
400 ('copy','Create in Same Model'),
401 ('new','Create in Other Model')
402 ], 'Create Model', size=32, change_default=True),
405 'state': lambda *a: 'dummy',
406 'type': lambda *a: 'ir.actions.server',
407 'sequence': lambda *a: 0,
408 'code': lambda *a: """# You can use the following variables
415 # If you plan to return an action, assign: action = {...}
417 'otype': lambda *a: 'copy',
421 def get_email(self, cr, uid, action, context):
422 obj_pool = self.pool.get(action.model_id.model)
423 id = context.get('active_id')
424 obj = obj_pool.browse(cr, uid, id)
428 if '/' in action.email.complete_name:
429 fields = action.email.complete_name.split('/')
430 elif '.' in action.email.complete_name:
431 fields = action.email.complete_name.split('.')
435 obj = getattr(obj, field)
437 logger.notifyChannel('Workflow', netsvc.LOG_ERROR, 'Failed to parse : %s' % (match.group()))
442 def get_mobile(self, cr, uid, action, context):
443 obj_pool = self.pool.get(action.model_id.model)
444 id = context.get('active_id')
445 obj = obj_pool.browse(cr, uid, id)
449 if '/' in action.mobile.complete_name:
450 fields = action.mobile.complete_name.split('/')
451 elif '.' in action.mobile.complete_name:
452 fields = action.mobile.complete_name.split('.')
456 obj = getattr(obj, field)
458 logger.notifyChannel('Workflow', netsvc.LOG_ERROR, 'Failed to parse : %s' % (match.group()))
462 def merge_message(self, cr, uid, keystr, action, context):
463 logger = netsvc.Logger()
465 obj_pool = self.pool.get(action.model_id.model)
466 id = context.get('active_id')
467 obj = obj_pool.browse(cr, uid, id)
468 exp = str(match.group()[2:-2]).strip()
469 return eval(exp, {'object':obj, 'context': context,'time':time})
471 com = re.compile('(\[\[.+?\]\])')
472 message = com.sub(merge, keystr)
475 # Context should contains:
477 # id : current id of the object
479 # False : Finnished correctly
480 # ACTION_ID : Action to launch
481 def run(self, cr, uid, ids, context={}):
482 logger = netsvc.Logger()
483 for action in self.browse(cr, uid, ids, context):
484 if action.state=='client_action':
485 if not action.action_id:
486 raise osv.except_osv(_('Error'), _("Please specify an action to launch !"))
487 return self.pool.get(action.action_id.type).read(cr, uid, action.action_id.id, context=context)
488 if action.state=='python':
490 'self': self.pool.get(action.model_id.model),
497 exec action.code in localdict
498 if 'action' in localdict:
499 return localdict['action']
501 if action.state == 'email':
502 user = config['email_from']
503 subject = action.name
504 address = self.get_email(cr, uid, action, context)
506 raise osv.except_osv(_('Error'), _("Please specify the Partner Email address !"))
508 raise osv.except_osv(_('Error'), _("Please specify server option --smtp-from !"))
510 body = self.merge_message(cr, uid, str(action.message), action, context)
511 if tools.email_send(user, address, subject, body, debug=False) == True:
512 logger.notifyChannel('email', netsvc.LOG_INFO, 'Email successfully send to : %s' % (address))
514 logger.notifyChannel('email', netsvc.LOG_ERROR, 'Failed to send email to : %s' % (address))
516 if action.state == 'trigger':
517 wf_service = netsvc.LocalService("workflow")
518 res = str(action.trigger_obj_id).split(',')
521 wf_service.trg_validate(uid, model, int(id), action.trigger_name, cr)
523 if action.state == 'sms':
524 #TODO: set the user and password from the system
525 # for the sms gateway user / password
528 to = self.get_mobile(cr, uid, action, context)
529 #TODO: Apply message mearge with the field
530 if tools.sms_send(user, password, api_id, text, to) == True:
531 logger.notifyChannel('sms', netsvc.LOG_INFO, 'SMS successfully send to : %s' % (action.address))
533 logger.notifyChannel('sms', netsvc.LOG_ERROR, 'Failed to send SMS to : %s' % (action.address))
534 if action.state == 'other':
536 'self': self.pool.get(action.model_id.model),
544 for act in action.child_ids:
545 code = """action = {'model':'%s','type':'%s', %s}""" % (action.model_id.model, act.type, act.usage)
546 exec code in localdict
547 if 'action' in localdict:
548 return localdict['action']
550 if action.state == 'object_write':
552 for exp in action.fields_lines:
554 if exp.type == 'equation':
555 obj_pool = self.pool.get(action.model_id.model)
556 obj = obj_pool.browse(cr, uid, context['active_id'], context=context)
557 expr = eval(euq, {'context':context, 'object': obj})
560 res[exp.col1.name] = expr
561 obj_pool = self.pool.get(action.model_id.model)
562 obj_pool.write(cr, uid, [context.get('active_id')], res)
564 if action.state == 'object_create':
566 for exp in action.fields_lines:
568 if exp.type == 'equation':
569 obj_pool = self.pool.get(action.model_id.model)
570 obj = obj_pool.browse(cr, uid, context['active_id'], context=context)
571 expr = eval(euq, {'context':context, 'object': obj})
574 res[exp.col1.name] = expr
577 if action.state == 'object_create' and action.otype == 'new':
578 obj_pool = self.pool.get(action.srcmodel_id.model)
579 obj_pool.create(cr, uid, res)
581 obj_pool = self.pool.get(action.model_id.model)
582 id = context.get('active_id')
583 obj_pool.copy(cr, uid, id, res)
588 class act_window_close(osv.osv):
589 _name = 'ir.actions.act_window_close'
590 _table = 'ir_actions'
591 _sequence = 'ir_actions_id_seq'
593 'name': fields.char('Action Name', size=64, translate=True),
594 'type': fields.char('Action Type', size=32, required=True),
597 'type': lambda *a: 'ir.actions.act_window_close',
601 # This model use to register action services.
602 # if action type is 'configure', it will be start on configuration wizard.
603 # if action type is 'service',
604 # - if start_type= 'at once', it will be start at one time on start date
605 # - if start_type='auto', it will be start on auto starting from start date, and stop on stop date
606 # - if start_type="manual", it will start and stop on manually
607 class ir_actions_todo(osv.osv):
608 _name = 'ir.actions.todo'
610 'name':fields.char('Name',size=64,required=True, select=True),
611 'note':fields.text('Text', translate=True),
612 'start_date': fields.datetime('Start Date'),
613 'end_date': fields.datetime('End Date'),
614 'action_id':fields.many2one('ir.actions.act_window', 'Action', select=True,required=True, ondelete='cascade'),
615 'sequence':fields.integer('Sequence'),
616 'active': fields.boolean('Active'),
617 'type':fields.selection([('configure', 'Configure'),('service', 'Service'),('other','Other')], string='Type', required=True),
618 'start_on':fields.selection([('at_once', 'At Once'),('auto', 'Auto'),('manual','Manual')], string='Start On'),
619 'groups_id': fields.many2many('res.groups', 'res_groups_act_todo_rel', 'act_todo_id', 'group_id', 'Groups'),
620 'users_id': fields.many2many('res.users', 'res_users_act_todo_rel', 'act_todo_id', 'user_id', 'Users'),
621 'state':fields.selection([('open', 'Not Started'),('done', 'Done'),('skip','Skipped'),('cancel','Cancel')], string='State', required=True)
624 'state': lambda *a: 'open',
625 'sequence': lambda *a: 10,
626 'active':lambda *a:True,
627 'type':lambda *a:'configure'
632 # This model to use run all configuration actions
633 class ir_actions_configuration_wizard(osv.osv_memory):
634 _name='ir.actions.configuration.wizard'
635 def next_configuration_action(self,cr,uid,context={}):
636 item_obj = self.pool.get('ir.actions.todo')
637 item_ids = item_obj.search(cr, uid, [('type','=','configure'),('state', '=', 'open'),('active','=',True)], limit=1, context=context)
638 if item_ids and len(item_ids):
639 item = item_obj.browse(cr, uid, item_ids[0], context=context)
642 def _get_action_name(self, cr, uid, context={}):
643 next_action=self.next_configuration_action(cr,uid,context=context)
645 return next_action.note
647 return "Your database is now fully configured.\n\nClick 'Continue' and enjoy your OpenERP experience..."
650 def _get_action(self, cr, uid, context={}):
651 next_action=self.next_configuration_action(cr,uid,context=context)
653 return next_action.id
656 def _progress_get(self,cr,uid, context={}):
657 total = self.pool.get('ir.actions.todo').search_count(cr, uid, [], context)
658 todo = self.pool.get('ir.actions.todo').search_count(cr, uid, [('type','=','configure'),('active','=',True),('state','<>','open')], context)
659 return max(5.0,round(todo*100/total))
662 'name': fields.text('Next Wizard',readonly=True),
663 'progress': fields.float('Configuration Progress', readonly=True),
664 'item_id':fields.many2one('ir.actions.todo', 'Next Configuration Wizard',invisible=True, readonly=True),
667 'progress': _progress_get,
668 'item_id':_get_action,
669 'name':_get_action_name,
671 def button_next(self,cr,uid,ids,context=None):
672 user_action=self.pool.get('res.users').browse(cr,uid,uid)
673 act_obj=self.pool.get(user_action.menu_id.type)
674 action_ids=act_obj.search(cr,uid,[('name','=',user_action.menu_id.name)])
675 action_open=act_obj.browse(cr,uid,action_ids)[0]
676 if context.get('menu',False):
678 'view_type': action_open.view_type,
679 'view_id':action_open.view_id and [action_open.view_id.id] or False,
680 'res_model': action_open.res_model,
681 'type': action_open.type,
682 'domain':action_open.domain
684 return {'type':'ir.actions.act_window_close'}
686 def button_skip(self,cr,uid,ids,context=None):
687 item_obj = self.pool.get('ir.actions.todo')
688 item_id=self.read(cr,uid,ids)[0]['item_id']
690 item = item_obj.browse(cr, uid, item_id, context=context)
691 item_obj.write(cr, uid, item.id, {
697 'res_model': 'ir.actions.configuration.wizard',
698 'type': 'ir.actions.act_window',
701 return self.button_next(cr, uid, ids, context)
703 def button_continue(self, cr, uid, ids, context=None):
704 item_obj = self.pool.get('ir.actions.todo')
705 item_id=self.read(cr,uid,ids)[0]['item_id']
707 item = item_obj.browse(cr, uid, item_id, context=context)
708 item_obj.write(cr, uid, item.id, {
712 'view_mode': item.action_id.view_mode,
713 'view_type': item.action_id.view_type,
714 'view_id':item.action_id.view_id and [item.action_id.view_id.id] or False,
715 'res_model': item.action_id.res_model,
716 'type': item.action_id.type,
717 'target':item.action_id.target,
719 return self.button_next(cr, uid, ids, context)
720 ir_actions_configuration_wizard()
722 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: