Changes in Notifications

This commit is contained in:
Anand Doshi
2012-01-04 19:31:32 +05:30
parent 1fad05aed8
commit f80f20f480
18 changed files with 179 additions and 103 deletions

View File

@@ -7,4 +7,40 @@ cur_frm.cscript.select_transaction = function(doc, dt, dn) {
}
$c_obj('Notification Control','get_message',doc.select_transaction, callback)
}
}
}
cur_frm.cscript.notify = function(doc, args) {
if(validate_email(args['send_to'] || doc.contact_email || '')) {
$c_obj('Notification Control', 'get_formatted_message', {
type: args['type'],
doctype: args['doctype'],
contact_name: args['contact_name'] || doc.contact_display
}, function(r, rt) {
if(!r.exc) {
var res = JSON.parse(r.message);
var send_from = (function() {
if(user!='Administrator') {
return user;
} else {
var cp = locals['Control Panel']['Control Panel'];
return (cp.auto_email_id || 'automail@erpnext.com');
}
})();
if(res.send) {
var print_heading = (doc.select_print_heading || args['type'])
sendmail(
args['send_to'] || doc.contact_email,
send_from,
send_from,
doc.company + " - " + print_heading,
res.message,
res.print_format
);
msgprint('This ' + print_heading + ' is being sent to <b>'
+ (args['send_to'] || doc.contact_email) + '</b><br />...');
}
}
//console.log(JSON.parse(r.message));
});
}
}

View File

@@ -3,27 +3,12 @@ import webnotes
from webnotes.utils import validate_email_add, cint, cstr
from webnotes.model.doc import Document
from webnotes.model.code import get_obj
from webnotes import msgprint
sql = webnotes.conn.sql
# -----------------------------------------------------------------------------------------
def get_formatted_message(head, body):
if head:
head = '<div style="font-size: 19px; margin-bottom: 13px; color: #333; font-family: Arial;">%s</div>' % head
else:
head = ''
return '''
<div style="margin: 13px">
%(head)s
<p style="font-size: 14px; line-height: 1.7em; color: #555; font-family: Arial;">
%(body)s
</p>
</div>
''' % {'head':head, 'body':body.replace('\n', '<br>')}
# Notification control
class DocType:
def __init__(self,d,dl):
@@ -43,62 +28,64 @@ class DocType:
webnotes.conn.set(self.doc, fn, self.doc.custom_message)
msgprint("Custom Message for %s updated!" % self.doc.select_transaction)
# notify contact
# --------------
def notify_contact(self, key, dt, dn, contact_email, contact_nm):
if contact_email:
dt_small = dt.replace(' ','_').lower()
if cint(self.doc.fields.get(dt_small)):
self.send_notification(key, dt, dn, contact_email, contact_nm)
# send notification
def send_notification(self, key, dt, dn, contact_email, contact_nm):
import webnotes.utils.encrypt
import os
from webnotes.utils.email_lib import sendmail
cp = Document('Control Panel', 'Control Panel')
banner = cp.client_name
sender_nm = sql("select concat_ws(' ', first_name, last_name) from tabProfile where name = %s", webnotes.session['user'])[0][0] or ''
if contact_nm:
contact_nm = ' ' + contact_nm
else:
contact_nm = ''
msg = '''
<div style="margin-bottom: 13px;">%(company_banner)s</div>
Hi%(contact)s,
%(message)s
<a href="http://%(domain)s/v170/index.cgi?page=Form/%(dt)s/%(dn)s&ac_name=%(account)s&akey=%(akey)s">Click here to see the document.</a></p>
Thanks,
%(sent_by)s
%(company_name)s
''' % {
'company_banner': banner,
'contact': contact_nm,
'message': self.doc.fields[key.lower().replace(' ','_')+'_message'],
'sent_by': sender_nm,
'company_name':cp.company_name,
'dt': dt.replace(' ', '%20'),
'dn': dn.replace('/', '%2F'),
'domain': os.environ.get('HTTP_HOST'),
'account': cp.account_id,
'akey': webnotes.utils.encrypt.encrypt(dn)
def get_formatted_message(self, args):
"""
args can contain:
* type
* doctype
* contact_name
"""
import json
args = json.loads(args)
res = {
'send': 0,
'message': self.prepare_message(args),
'print_format': self.get_default_print_format(args)
}
if not validate_email_add(webnotes.session['user']):
sender = "automail@webnotestech.com"
else:
sender = webnotes.session['user']
rec_lst = [contact_email, sender]
subject = cp.company_name + ' - ' + dt
sendmail(rec_lst, sender, get_formatted_message(None, msg), subject)
dt_small = args.get('doctype').replace(' ', '_').lower()
if cint(self.doc.fields.get(dt_small)):
res['send'] = 1
return json.dumps(res)
def prepare_message(self, args):
"""
Prepares message body
"""
if args.get('type') and args.get('doctype'):
msg_dict = {}
msg_dict['message'] = self.get_message(args.get('type'))
msg_dict['company'] = Document('Control Panel', 'Control Panel').company_name
msg_dict['salutation'] = "Hi" + (args.get('contact_name') and (" " + args.get('contact_name')) or "")
msg_dict['send_from'] = webnotes.conn.sql("""\
SELECT CONCAT_WS(' ', first_name, last_name)
FROM `tabProfile`
WHERE name = %s""", webnotes.session['user'], as_list=1)[0][0] or ''
return """\
<div>
%(salutation)s,
%(message)s
Thanks,
%(send_from)s
%(company)s
</div>""" % msg_dict
else: return ""
def get_default_print_format(self, args):
"""
Get default print format from doclayer
"""
doclayer = get_obj('DocLayer', 'DocLayer')
doclayer.doc.doc_type = args.get('doctype')
doclayer.get()
if doclayer.doc.default_print_format:
return doclayer.doc.default_print_format
else: return 'Standard'