[IMP]: base_action_rule: Apply Doc String + quality check

bzr revid: ksa@tinyerp.co.in-20100325134325-xc427hcsrutqlgqz
This commit is contained in:
ksa (Open ERP) 2010-03-25 19:13:25 +05:30
parent ddbd753d89
commit 8b353184d4
2 changed files with 154 additions and 57 deletions

View File

@ -29,61 +29,86 @@ from osv.orm import except_orm
from tools.translate import _ from tools.translate import _
class base_action_rule(osv.osv): class base_action_rule(osv.osv):
""" Base Action Rules """
_name = 'base.action.rule' _name = 'base.action.rule'
_description = 'Action Rules' _description = 'Action Rules'
_columns = { _columns = {
'name': fields.many2one('ir.model', 'Model', required=True), 'name': fields.many2one('ir.model', 'Model', required=True),
'max_level': fields.integer('Max Level', help='Specifies maximum level.'), 'max_level': fields.integer('Max Level', help='Specifies maximum level.'),
'rule_lines': fields.one2many('base.action.rule.line','rule_id','Rule Lines'), 'rule_lines': fields.one2many('base.action.rule.line','rule_id','Rule Lines'),
'create_date': fields.datetime('Create Date', readonly=1), 'create_date': fields.datetime('Create Date', readonly=1),
'active': fields.boolean('Active') 'active': fields.boolean('Active')
} }
_defaults = { _defaults = {
'active': lambda *a: True, 'active': lambda *a: True,
'max_level': lambda *a: 15, 'max_level': lambda *a: 15,
} }
def format_body(self, body): def format_body(self, body):
""" Foramat Action rule's body
@param self: The object pointer """
return body and tools.ustr(body) or '' return body and tools.ustr(body) or ''
def format_mail(self, obj, body): def format_mail(self, obj, body):
""" Foramat Mail
@param self: The object pointer """
data = { data = {
'object_id': obj.id, 'object_id': obj.id,
'object_subject': hasattr(obj, 'name') and obj.name or False, 'object_subject': hasattr(obj, 'name') and obj.name or False,
'object_date': hasattr(obj, 'date') and obj.date or False, 'object_date': hasattr(obj, 'date') and obj.date or False,
'object_description': hasattr(obj, 'description') and obj.description or False, 'object_description': hasattr(obj, 'description') and obj.description or False,
'object_user': hasattr(obj, 'user_id') and (obj.user_id and obj.user_id.name) or '/', 'object_user': hasattr(obj, 'user_id') and (obj.user_id and obj.user_id.name) or '/',
'object_user_email': hasattr(obj, 'user_id') and (obj.user_id and obj.user_id.address_id and obj.user_id.address_id.email) or '/', 'object_user_email': hasattr(obj, 'user_id') and (obj.user_id and \
'object_user_phone': hasattr(obj, 'user_id') and (obj.user_id and obj.user_id.address_id and obj.user_id.address_id.phone) or '/', obj.user_id.address_id and obj.user_id.address_id.email) or '/',
'object_user_phone': hasattr(obj, 'user_id') and (obj.user_id and\
obj.user_id.address_id and obj.user_id.address_id.phone) or '/',
'partner': hasattr(obj, 'partner_id') and (obj.partner_id and obj.partner_id.name) or '/', 'partner': hasattr(obj, 'partner_id') and (obj.partner_id and obj.partner_id.name) or '/',
'partner_email': hasattr(obj, 'partner_address_id') and (obj.partner_address_id and obj.partner_address_id.email) or '/', 'partner_email': hasattr(obj, 'partner_address_id') and (obj.partner_address_id and\
obj.partner_address_id.email) or '/',
} }
return self.format_body(body % data) return self.format_body(body % data)
def email_send(self, cr, uid, obj, emails, body, emailfrom=tools.config.get('email_from',False), context={}): def email_send(self, cr, uid, obj, emails, body, emailfrom=tools.config.get('email_from',False), context={}):
""" send email
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param email: pass the emails
@param emailfrom: Pass name the email From else False
@param context: A standard dictionary for contextual values """
body = self.format_mail(obj, body) body = self.format_mail(obj, body)
if not emailfrom: if not emailfrom:
if hasattr(obj, 'user_id') and obj.user_id and obj.user_id.address_id and obj.user_id.address_id.email: if hasattr(obj, 'user_id') and obj.user_id and obj.user_id.address_id and\
obj.user_id.address_id.email:
emailfrom = obj.user_id.address_id.email emailfrom = obj.user_id.address_id.email
name = '[%d] %s' % (obj.id, tools.ustr(obj.name)) name = '[%d] %s' % (obj.id, tools.ustr(obj.name))
emailfrom = tools.ustr(emailfrom) emailfrom = tools.ustr(emailfrom)
reply_to = emailfrom reply_to = emailfrom
if not emailfrom: if not emailfrom:
raise osv.except_osv(_('Error!'), raise osv.except_osv(_('Error!'),
_("No E-Mail ID Found for your Company address!")) _("No E-Mail ID Found for your Company address!"))
return tools.email_send(emailfrom, emails, name, body, reply_to=reply_to, openobject_id=str(obj.id)) return tools.email_send(emailfrom, emails, name, body, reply_to=reply_to, openobject_id=str(obj.id))
def do_check(self, cr, uid, action, obj, context={}): def do_check(self, cr, uid, action, obj, context={}):
ok = True """ check Action
if hasattr(obj, 'user_id'): @param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param context: A standard dictionary for contextual values """
ok = True
if hasattr(obj, 'user_id'):
ok = ok and (not action.trg_user_id.id or action.trg_user_id.id==obj.user_id.id) ok = ok and (not action.trg_user_id.id or action.trg_user_id.id==obj.user_id.id)
if hasattr(obj, 'partner_id'): if hasattr(obj, 'partner_id'):
ok = ok and (not action.trg_partner_id.id or action.trg_partner_id.id==obj.partner_id.id) ok = ok and (not action.trg_partner_id.id or action.trg_partner_id.id==obj.partner_id.id)
ok = ok and ( ok = ok and (
not action.trg_partner_categ_id.id or not action.trg_partner_categ_id.id or
( (
@ -113,14 +138,22 @@ class base_action_rule(osv.osv):
return ok return ok
def do_action(self, cr, uid, action, model_obj, obj, context={}): def do_action(self, cr, uid, action, model_obj, obj, context={}):
""" Do Action
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param action: pass action
@param model_obj: pass Model object
@param context: A standard dictionary for contextual values """
if action.server_action_id: if action.server_action_id:
context.update({'active_id':obj.id,'active_ids':[obj.id]}) context.update({'active_id':obj.id,'active_ids':[obj.id]})
self.pool.get('ir.actions.server').run(cr, uid, [action.server_action_id.id], context) self.pool.get('ir.actions.server').run(cr, uid, [action.server_action_id.id], context)
write = {} write = {}
if hasattr(obj, 'user_id') and action.act_user_id: if hasattr(obj, 'user_id') and action.act_user_id:
obj.user_id = action.act_user_id obj.user_id = action.act_user_id
write['user_id'] = action.act_user_id.id write['user_id'] = action.act_user_id.id
if hasattr(obj, 'date_action_last'): if hasattr(obj, 'date_action_last'):
write['date_action_last'] = time.strftime('%Y-%m-%d %H:%M:%S') write['date_action_last'] = time.strftime('%Y-%m-%d %H:%M:%S')
if hasattr(obj, 'state') and action.act_state: if hasattr(obj, 'state') and action.act_state:
obj.state = action.act_state obj.state = action.act_state
@ -135,7 +168,7 @@ class base_action_rule(osv.osv):
write['priority'] = action.act_priority write['priority'] = action.act_priority
model_obj.write(cr, uid, [obj.id], write, context) model_obj.write(cr, uid, [obj.id], write, context)
if hasattr(model_obj, 'remind_user') and action.act_remind_user: if hasattr(model_obj, 'remind_user') and action.act_remind_user:
model_obj.remind_user(cr, uid, [obj.id], context, attach=action.act_remind_attach) model_obj.remind_user(cr, uid, [obj.id], context, attach=action.act_remind_attach)
if hasattr(model_obj, 'remind_partner') and action.act_remind_partner: if hasattr(model_obj, 'remind_partner') and action.act_remind_partner:
@ -146,7 +179,7 @@ class base_action_rule(osv.osv):
if hasattr(obj, 'user_id') and action.act_mail_to_user: if hasattr(obj, 'user_id') and action.act_mail_to_user:
if obj.user_id and obj.user_id.address_id: if obj.user_id and obj.user_id.address_id:
emails.append(obj.user_id.address_id.email) emails.append(obj.user_id.address_id.email)
if action.act_mail_to_watchers: if action.act_mail_to_watchers:
emails += (action.act_email_cc or '').split(',') emails += (action.act_email_cc or '').split(',')
if action.act_mail_to_email: if action.act_mail_to_email:
@ -158,11 +191,19 @@ class base_action_rule(osv.osv):
return True return True
def _action(self, cr, uid, ids, objects, scrit=None, context={}): def _action(self, cr, uid, ids, objects, scrit=None, context={}):
""" Do Action
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param ids: List of Basic Action Rules IDs,
@param objects: pass objects
@param context: A standard dictionary for contextual values """
if not scrit: if not scrit:
scrit = [] scrit = []
rule_line_obj = self.pool.get('base.action.rule.line') rule_line_obj = self.pool.get('base.action.rule.line')
for rule in self.browse(cr, uid, ids): for rule in self.browse(cr, uid, ids):
level = rule.max_level level = rule.max_level
if not level: if not level:
break break
newactions = [] newactions = []
@ -184,7 +225,8 @@ class base_action_rule(osv.osv):
base = mx.DateTime.strptime(obj.date_action_last, '%Y-%m-%d %H:%M:%S') base = mx.DateTime.strptime(obj.date_action_last, '%Y-%m-%d %H:%M:%S')
else: else:
base = mx.DateTime.strptime(obj.create_date[:19], '%Y-%m-%d %H:%M:%S') base = mx.DateTime.strptime(obj.create_date[:19], '%Y-%m-%d %H:%M:%S')
elif hasattr(obj, 'date_deadline') and action.trg_date_type=='deadline' and obj.date_deadline: elif hasattr(obj, 'date_deadline') and action.trg_date_type=='deadline' \
and obj.date_deadline:
base = mx.DateTime.strptime(obj.date_deadline, '%Y-%m-%d %H:%M:%S') base = mx.DateTime.strptime(obj.date_deadline, '%Y-%m-%d %H:%M:%S')
elif hasattr(obj, 'date') and action.trg_date_type=='date' and obj.date: elif hasattr(obj, 'date') and action.trg_date_type=='date' and obj.date:
base = mx.DateTime.strptime(obj.date, '%Y-%m-%d %H:%M:%S') base = mx.DateTime.strptime(obj.date, '%Y-%m-%d %H:%M:%S')
@ -212,7 +254,7 @@ class base_action_rule(osv.osv):
if ok: if ok:
self.do_action(cr, uid, action, model_obj, obj, context) self.do_action(cr, uid, action, model_obj, obj, context)
break break
level -= 1 level -= 1
return True return True
base_action_rule() base_action_rule()
@ -222,20 +264,46 @@ class base_action_rule_line(osv.osv):
_description = 'Action Rule Lines' _description = 'Action Rule Lines'
def _state_get(self, cr, uid, context={}): def _state_get(self, cr, uid, context={}):
""" Get State
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param context: A standard dictionary for contextual values """
return self.state_get(cr, uid, context=context) return self.state_get(cr, uid, context=context)
def _priority_get(self, cr, uid, context={}): def _priority_get(self, cr, uid, context={}):
""" Get Priority
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param context: A standard dictionary for contextual values """
return self.priority_get(cr, uid, context=context) return self.priority_get(cr, uid, context=context)
def state_get(self, cr, uid, context={}): def state_get(self, cr, uid, context={}):
""" Get State
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param context: A standard dictionary for contextual values """
return [('','')] return [('','')]
def priority_get(self, cr, uid, context={}): def priority_get(self, cr, uid, context={}):
""" Get Priority
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param context: A standard dictionary for contextual values """
return [('','')] return [('','')]
_columns = { _columns = {
'name': fields.char('Rule Name',size=64, required=True), 'name': fields.char('Rule Name',size=64, required=True),
'rule_id': fields.many2one('base.action.rule','Rule'), 'rule_id': fields.many2one('base.action.rule','Rule'),
'active': fields.boolean('Active', help="If the active field is set to true, it will allow you to hide the rule without removing it."), 'active': fields.boolean('Active', help="If the active field is set to true,\
'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of rules."), it will allow you to hide the rule without removing it."),
'sequence': fields.integer('Sequence', help="Gives the sequence order when\
displaying a list of rules."),
'trg_date_type': fields.selection([ 'trg_date_type': fields.selection([
('none','None'), ('none','None'),
@ -243,11 +311,13 @@ class base_action_rule_line(osv.osv):
('action_last','Last Action Date'), ('action_last','Last Action Date'),
('date','Date'), ('date','Date'),
], 'Trigger Date', size=16), ], 'Trigger Date', size=16),
'trg_date_range': fields.integer('Delay after trigger date',help="Delay After Trigger Date, specifies you can put a negative number " \ 'trg_date_range': fields.integer('Delay after trigger date',help="Delay After Trigger Date,\
"if you need a delay before the trigger date, like sending a reminder 15 minutes before a meeting."), specifies you can put a negative number " \
'trg_date_range_type': fields.selection([('minutes', 'Minutes'),('hour','Hours'),('day','Days'),('month','Months')], 'Delay type'), "if you need a delay before the trigger date, like sending a reminder 15 minutes before a meeting."),
'trg_date_range_type': fields.selection([('minutes', 'Minutes'),('hour','Hours'),\
('day','Days'),('month','Months')], 'Delay type'),
'trg_user_id': fields.many2one('res.users', 'Responsible'), 'trg_user_id': fields.many2one('res.users', 'Responsible'),
'trg_partner_id': fields.many2one('res.partner', 'Partner'), 'trg_partner_id': fields.many2one('res.partner', 'Partner'),
@ -255,42 +325,59 @@ class base_action_rule_line(osv.osv):
'trg_state_from': fields.selection(_state_get, 'State', size=16), 'trg_state_from': fields.selection(_state_get, 'State', size=16),
'trg_state_to': fields.selection(_state_get, 'Button Pressed', size=16), 'trg_state_to': fields.selection(_state_get, 'Button Pressed', size=16),
'trg_priority_from': fields.selection(_priority_get, 'Minimum Priority'), 'trg_priority_from': fields.selection(_priority_get, 'Minimum Priority'),
'trg_priority_to': fields.selection(_priority_get, 'Maximum Priority'), 'trg_priority_to': fields.selection(_priority_get, 'Maximum Priority'),
'act_method': fields.char('Call Object Method', size=64), 'act_method': fields.char('Call Object Method', size=64),
'act_user_id': fields.many2one('res.users', 'Set responsible to'), 'act_user_id': fields.many2one('res.users', 'Set responsible to'),
'act_state': fields.selection(_state_get, 'Set state to', size=16), 'act_state': fields.selection(_state_get, 'Set state to', size=16),
'act_priority': fields.selection(_priority_get, 'Set priority to'), 'act_priority': fields.selection(_priority_get, 'Set priority to'),
'act_email_cc': fields.char('Add watchers (Cc)', size=250, help="These people will receive a copy of the future communication between partner and users by email"), 'act_email_cc': fields.char('Add watchers (Cc)', size=250, help="These people\
will receive a copy of the future communication between partner and users by email"),
'act_remind_partner': fields.boolean('Remind Partner', help="Check this if you want the rule to send a reminder by email to the partner."), 'act_remind_partner': fields.boolean('Remind Partner', help="Check this if\
'act_remind_user': fields.boolean('Remind responsible', help="Check this if you want the rule to send a reminder by email to the user."), you want the rule to send a reminder by email to the partner."),
'act_remind_user': fields.boolean('Remind responsible', help="Check this if \
you want the rule to send a reminder by email to the user."),
'act_reply_to': fields.char('Reply-To', size=64), 'act_reply_to': fields.char('Reply-To', size=64),
'act_remind_attach': fields.boolean('Remind with attachment', help="Check this if you want that all documents attached to the object be attached to the reminder email sent."), 'act_remind_attach': fields.boolean('Remind with attachment', help="Check this if\
you want that all documents attached to the object be attached \
to the reminder email sent."),
'act_mail_to_user': fields.boolean('Mail to responsible',help="Check this if you want the rule to send an email to the responsible person."), 'act_mail_to_user': fields.boolean('Mail to responsible',help="Check this if \
'act_mail_to_watchers': fields.boolean('Mail to watchers (CC)',help="Check this if you want the rule to mark CC(mail to any other person defined in actions)."), you want the rule to send an email to the responsible person."),
'act_mail_to_email': fields.char('Mail to these emails', size=128,help="Email-id of the persons whom mail is to be sent"), 'act_mail_to_watchers': fields.boolean('Mail to watchers (CC)',help="Check this\
if you want the rule to mark CC(mail to any other person\
defined in actions)."),
'act_mail_to_email': fields.char('Mail to these emails', size=128,help="Email-id \
of the persons whom mail is to be sent"),
'act_mail_body': fields.text('Mail body',help="Content of mail"), 'act_mail_body': fields.text('Mail body',help="Content of mail"),
'regex_name': fields.char('Regular Expression on Model Name', size=128), 'regex_name': fields.char('Regular Expression on Model Name', size=128),
'server_action_id': fields.many2one('ir.actions.server','Server Action',help="Describes the action name." \ 'server_action_id': fields.many2one('ir.actions.server','Server Action',help="Describes the\
"eg:on which object which action to be taken on basis of which condition"), action name." \
"eg:on which object which action to be taken on basis of which condition"),
} }
_defaults = { _defaults = {
'active': lambda *a: 1, 'active': lambda *a: 1,
'trg_date_type': lambda *a: 'none', 'trg_date_type': lambda *a: 'none',
'trg_date_range_type': lambda *a: 'day', 'trg_date_range_type': lambda *a: 'day',
'act_mail_to_user': lambda *a: 0, 'act_mail_to_user': lambda *a: 0,
'act_remind_partner': lambda *a: 0, 'act_remind_partner': lambda *a: 0,
'act_remind_user': lambda *a: 0, 'act_remind_user': lambda *a: 0,
'act_mail_to_watchers': lambda *a: 0, 'act_mail_to_watchers': lambda *a: 0,
} }
_order = 'sequence' _order = 'sequence'
def _check_mail(self, cr, uid, ids, context=None): def _check_mail(self, cr, uid, ids, context=None):
""" Check Mail
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param ids: List of Action Rules IDs
@param context: A standard dictionary for contextual values """
empty = orm.browse_null() empty = orm.browse_null()
rule_obj = self.pool.get('base.action.rule') rule_obj = self.pool.get('base.action.rule')
for rule in self.browse(cr, uid, ids): for rule in self.browse(cr, uid, ids):
@ -300,10 +387,11 @@ class base_action_rule_line(osv.osv):
except (ValueError, KeyError, TypeError): except (ValueError, KeyError, TypeError):
return False return False
return True return True
_constraints = [ _constraints = [
(_check_mail, 'Error: The mail is not well formated', ['act_mail_body']), (_check_mail, 'Error: The mail is not well formated', ['act_mail_body']),
] ]
base_action_rule_line() base_action_rule_line()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<openerp> <openerp>
<data> <data>
<menuitem id="menu_base_action_rule" name="Action Rule" parent="base.menu_base_config" sequence="0"/> <menuitem id="menu_base_action_rule" name="Action Rule"
parent="base.menu_base_config" sequence="0" />
<!-- <!--
Action Rule Action Rule Form View
--> -->
<record id="view_base_action_rule_form" model="ir.ui.view"> <record id="view_base_action_rule_form" model="ir.ui.view">
<field name="name">base.action.rule.form</field> <field name="name">base.action.rule.form</field>
@ -21,6 +22,8 @@
</field> </field>
</record> </record>
<!-- Action Rule Tree View -->
<record id="view_base_action_rule_tree" model="ir.ui.view"> <record id="view_base_action_rule_tree" model="ir.ui.view">
<field name="name">base.action.rule.tree</field> <field name="name">base.action.rule.tree</field>
<field name="model">base.action.rule</field> <field name="model">base.action.rule</field>
@ -34,6 +37,8 @@
</field> </field>
</record> </record>
<!-- Action Rule Action -->
<record id="base_action_rule_act" model="ir.actions.act_window"> <record id="base_action_rule_act" model="ir.actions.act_window">
<field name="name">Action Rules</field> <field name="name">Action Rules</field>
<field name="res_model">base.action.rule</field> <field name="res_model">base.action.rule</field>
@ -42,10 +47,11 @@
<field name="view_id" ref="view_base_action_rule_tree"/> <field name="view_id" ref="view_base_action_rule_tree"/>
</record> </record>
<menuitem id="menu_base_action_rule_form" parent="menu_base_action_rule" action="base_action_rule_act"/> <menuitem id="menu_base_action_rule_form"
parent="menu_base_action_rule" action="base_action_rule_act" />
<!-- <!--
Action Rule Lines Action Rule Lines Form View
--> -->
<record id="view_base_action_rule_line_form" model="ir.ui.view"> <record id="view_base_action_rule_line_form" model="ir.ui.view">
<field name="name">base.action.rule.line.form</field> <field name="name">base.action.rule.line.form</field>
@ -127,6 +133,9 @@
</form> </form>
</field> </field>
</record> </record>
<!-- Action Rule Lines Tree View -->
<record id="view_base_action_rule_line_tree" model="ir.ui.view"> <record id="view_base_action_rule_line_tree" model="ir.ui.view">
<field name="name">base.action.rule.line.tree</field> <field name="name">base.action.rule.line.tree</field>
<field name="model">base.action.rule.line</field> <field name="model">base.action.rule.line</field>