diff --git a/js/legacy/form.compressed.js b/js/legacy/form.compressed.js index 7fd9d15d2a..8f20026344 100644 --- a/js/legacy/form.compressed.js +++ b/js/legacy/form.compressed.js @@ -61,7 +61,8 @@ this.setup_footer();if(!(this.meta.istable||user=='Guest'))this.frm_head=new _f. this.layout.wrapper.style.backgroundColor='#'+this.meta.colour.split(':')[1];this.setup_fields_std();} _f.Frm.prototype.setup_print=function(){var fl=getchildren('DocFormat',this.meta.name,'formats','DocType');var l=[];this.default_format='Standard';if(fl.length){this.default_format=fl[0].format;for(var i=0;i'+label+'';col.label=label;if(reqd) col.childNodes[0].style.color="#D22";col.style.width=width;col.options=options;col.perm=perm;this.col_idx_by_name[fieldname]=idx;} _f.Grid.prototype.reset_table_width=function(){var w=0;for(var i=0,len=this.head_row.cells.length;i : contains the name of DocType initiating the function String : contains the name of DocType created by the function @@ -64,6 +40,10 @@ class DocType: String : contains doclist of 'to_doctype' String : contains list of tables which will be mapped ''' + + if not from_docname: + msgprint(from_doctype + " not selected for mapping", raise_exception=1) + # Validate reference doc docstatus self.ref_doc = from_docname self.check_ref_docstatus() @@ -71,75 +51,93 @@ class DocType: if not doclist: doclist.append(to_doc) - tbl_list = sql("select from_table, to_table, from_field, to_field, match_id, validation_logic from `tabTable Mapper Detail` where parent ='%s' order by match_id" % (from_doctype + "-" + to_doctype)) - - for t in tbl_list: - from_table_name = t[0] - to_table_name = t[1] - from_table_fname = t[2] - to_table_fname = t[3] - match_id = t[4] - validation_logic = t[5] - - - from_to = [from_table_name, to_table_name] + tbl_list = sql("select from_table, to_table, from_field, to_field, match_id, validation_logic from `tabTable Mapper Detail` where parent ='%s' order by match_id" % self.doc.name, as_dict=1) - if from_to in eval(from_to_list): - fld_list = sql("select from_field, to_field, map from `tabField Mapper Detail` where parent = '%s' and match_id = %s" % (from_doctype + "-" + to_doctype, match_id)) - if not from_docname: - msgprint(from_doctype + " not selected for mapping") - raise Exception - - # Parent to parent mapping - if from_table_name == self.doc.from_doctype and to_table_name == self.doc.to_doctype: - - # Check validation - nm = sql("select name from `tab%s` where name = '%s' and %s" % (from_doctype, from_docname, validation_logic)) - nm = nm and nm[0][0] or '' - - # If validation failed raise exception - if not nm: - msgprint("Validation failed in doctype mapper. Please contact Administrator.") - raise Exception - - from_doc = Document(from_doctype, nm) - - # Map fields with same name + for t in tbl_list: + if [t['from_table'], t['to_table']] in eval(from_to_list): + self.map_fields(t, from_doctype, from_docname, to_doc, doclist) + + # Doclist is required when called from server side for refreshing table + return doclist - self.map_fields_with_same_name(from_doctype, to_doctype, from_doc, to_doc, fld_list) - # Maps field in parent - - if fld_list: - self.set_value(fld_list, from_doc, to_doc) + #--------------------------------------------------------------------------- + def map_fields(self, t, from_dt, from_dn, to_doc, doclist): + """ + Creates from, to obj and maps flds as per mapper and with same name + """ + flds = self.get_mapper_fields(t) + flds += self.get_fields_with_same_name(t, flds) - # Parent to child OR child to child mapping + if flds: + from_docnames = self.get_docnames(t, from_dt, from_dn) + + for dn in from_docnames: + # Creates object for 'From DocType', it can be parent or child + from_doc_obj = Document(t['from_table'], dn[0]) + + # Add a row in target table in 'To DocType' and returns obj + if t['to_table'] != self.doc.to_doctype: + to_doc_obj = addchild(to_doc, t['to_field'], t['to_table'], 1, doclist) else: - dnlist = () - if from_table_name == self.doc.from_doctype: - dnlist = ((from_docname,),) - else: - dnlist = sql("select name from `tab%s` where parent='%s' and parenttype = '%s' and %s order by idx" % (from_table_name, from_docname, self.doc.from_doctype, validation_logic)) - - for dn in dnlist: - # Add a row in target table in 'To DocType' and returns obj - ch = addchild(to_doc, t[3], t[1], 1, doclist) - # Creates object for 'From DocType', it can be parent or child - d = Document(t[0], dn[0]) - # Map fields with same name - self.map_fields_with_same_name(from_table_name, t[1], d, ch, fld_list) - # Map values - if fld_list: - self.set_value(fld_list, d, ch) + to_doc_obj = to_doc + self.set_value(flds, from_doc_obj, to_doc_obj) + + #--------------------------------------------------------------------------- + def get_docnames(self, t, from_dt, from_dn): + """ + Returns docnames of source document (parent/child) + """ + docnames = () + if t['from_table'] == self.doc.from_doctype: + docnames = sql("select name from `tab%s` where name = '%s' and %s" % (from_dt, from_dn, t['validation_logic'])) + if not docnames: + msgprint("Validation failed in doctype mapper. Please contact Administrator.", raise_exception=1) + else: + docnames = sql("select name from `tab%s` where parent='%s' and parenttype = '%s' and %s order by idx" % (t['from_table'], from_dn, self.doc.from_doctype, t['validation_logic'])) + + return docnames + + + #--------------------------------------------------------------------------- + def get_mapper_fields(self, t): + return [[f[0], f[1], f[2]] for f in sql(""" + select from_field, to_field, map + from `tabField Mapper Detail` + where parent = '%s' and match_id = %s + """ % (self.doc.name, t['match_id']))] - # Required when called from server side for refreshing table - return doclist + #--------------------------------------------------------------------------- + def get_fields_with_same_name(self, t, flds): + """ + Returns field list with same name in from and to doctype + """ + + exception_flds = default_fields + exception_flds += [f[1] for f in flds] + + similar_flds = [ + [d[0], d[0], 'Yes'] for d in sql(""" + select t1.fieldname + from `tabDocField` t1, `tabDocField` t2 + where t1.parent = %s and t2.parent = %s + and t1.fieldname = t2.fieldname + and t1.docstatus != 2 and t2.docstatus != 2 + and ifnull(t1.no_copy, 0) = 0 + and ifnull(t1.fieldname, '') != '' + and t1.fieldtype not in ('Table', 'Section Break', 'Column Break', 'HTML') + """,(t['from_table'], t['to_table'])) if d[0] not in exception_flds + ] + + return similar_flds - # Assigns value to "To Doctype" - #------------------------------ + #--------------------------------------------------------------------------- def set_value(self, fld_list, obj, to_doc): + """ + Assigns value to fields in "To Doctype" + """ for f in fld_list: if f[2] == 'Yes': if f[0].startswith('eval:'): @@ -152,9 +150,12 @@ class DocType: else: to_doc.fields[f[1]] = obj.fields.get(f[0]) - # Validate - #--------- + + #--------------------------------------------------------------------------- def validate(self): + """ + Validate mapper while saving + """ for d in getlist(self.doclist, 'field_mapper_details'): # Automatically assigns default value if not entered if not d.match_id: @@ -169,10 +170,13 @@ class DocType: # Check wrong field name self.check_fields_in_dt() - - # Check if any wrong fieldname entered - #-------------------------------------- + + + #--------------------------------------------------------------------------- def check_fields_in_dt(self): + """ + Check if any wrong fieldname entered in mapper + """ for d in getlist(self.doclist, 'field_mapper_details'): table_name = sql("select from_table, to_table from `tabTable Mapper Detail` where parent ='%s' and match_id = '%s'" % (self.doc.name, d.match_id)) @@ -186,6 +190,7 @@ class DocType: if not exists2 and d.to_field not in default_fields: msgprint('"' + cstr(d.to_field) + '" does not exists in DocType "' + cstr(table_name[0][1]) + '"') + # Check consistency of value with reference document #--------------------------------------------------- def validate_reference_value(self, obj, to_docname): @@ -235,7 +240,6 @@ class DocType: if cl[2] == '=' and (ft[1] == 'Currency' or ft[1] == 'Float'): consistent = sql("select name, %s from `tab%s` where name = '%s' and '%s' - %s <= 0.5" % (cl[0], t.from_table, child_obj.fields[t.reference_key], flt(cur_val), cl[0])) else: - #consistent = sql("select name, %s from `tab%s` where name = '%s' and '%s' %s %s" % (cl[0], t.from_table, child_obj.fields[t.reference_key], cur_val, cl[2], cl[0])) consistent = sql("select name, %s from `tab%s` where name = '%s' and '%s' %s ifnull(%s, '')" % (cl[0], t.from_table, child_obj.fields[t.reference_key], ft[1] in ('Currency', 'Float', 'Int') and flt(cur_val) or cstr(cur_val), cl[2], cl[0])) if not self.ref_doc: @@ -253,22 +257,21 @@ class DocType: from_fld_label = sql("select label from tabDocField where parent = '%s' and fieldname = '%s'" % (from_table, from_field)) op_in_words = {'=':'equal to ', '>=':'greater than equal to ', '>':'greater than ', '<=':'less than equal to ', '<':'less than '} - msgprint(to_fld_label[0][0] + " should be " + op_in_words[operator] + from_fld_label[0][0] + " of " + self.doc.from_doctype + ": " + self.ref_doc) - raise Exception, "Validation Error." + msgprint(to_fld_label[0][0] + " should be " + op_in_words[operator] + from_fld_label[0][0] + " of " + self.doc.from_doctype + ": " + self.ref_doc, raise_exception=1) def check_ref_docstatus(self): if self.ref_doc: det = sql("select name, docstatus from `tab%s` where name = '%s'" % (self.doc.from_doctype, self.ref_doc)) if not det: - msgprint(self.doc.from_doctype + ": " + self.ref_doc + " does not exists in the system") - raise Exception, "Validation Error." + msgprint(self.doc.from_doctype + ": " + self.ref_doc + " does not exists in the system", raise_exception=1) elif self.doc.ref_doc_submitted and det[0][1] != 1: - msgprint(self.doc.from_doctype + ": " + self.ref_doc + " is not Submitted Document.") - raise Exception, "Validation Error." + msgprint(self.doc.from_doctype + ": " + self.ref_doc + " is not Submitted Document.", raise_exception=1) def on_update(self): + """ + If developer_mode = 1, mapper will be written to files + """ import webnotes.defs if hasattr(webnotes.defs, 'developer_mode') and webnotes.defs.developer_mode: from webnotes.modules.export_module import export_to_files - export_to_files(record_list=[[self.doc.doctype, self.doc.name]]) - + export_to_files(record_list=[[self.doc.doctype, self.doc.name]]) diff --git a/py/webnotes/modules/patch.py b/py/webnotes/modules/patch.py index 4ead5121b0..4bc0d17c5c 100644 --- a/py/webnotes/modules/patch.py +++ b/py/webnotes/modules/patch.py @@ -5,6 +5,10 @@ def run(log_exception=1): from patches import patch from webnotes.utils import cint + if webnotes.conn.cur_db_name=='accounts': + # no patches on accounts + return + next_patch = cint(webnotes.conn.get_global('next_patch')) if next_patch <= patch.last_patch: diff --git a/py/webnotes/profile.py b/py/webnotes/profile.py index b74fa08e5c..fc57cbabbb 100644 --- a/py/webnotes/profile.py +++ b/py/webnotes/profile.py @@ -162,12 +162,12 @@ class Profile: # update tab Profile webnotes.conn.sql("UPDATE tabProfile SET password=password(%s) WHERE name=%s", (pwd, profile[0][0])) - + self.send_email("Password Reset", "

Dear %s%s,

your password has been changed to %s

[Automatically Generated]

" % (profile[0][2], (profile[0][3] and (' ' + profile[0][3]) or ''), pwd), profile[0][1]) def send_email(self, subj, mess, email): import webnotes.utils.email_lib - + webnotes.utils.email_lib.sendmail(email, msg=mess, subject=subj) # update recent documents diff --git a/py/webnotes/utils/email_lib/__init__.py b/py/webnotes/utils/email_lib/__init__.py index abda347923..7d0572d46b 100644 --- a/py/webnotes/utils/email_lib/__init__.py +++ b/py/webnotes/utils/email_lib/__init__.py @@ -29,19 +29,18 @@ def sendmail(recipients, sender='', msg='', subject='[No Subject]', parts=[], cc email = EMail(sender, recipients, subject, reply_to=reply_to) email.cc = cc - - if msg: - if template: - msg = make_html_body(msg, template) + + if msg: + if template: + msg = make_html_body(msg, template).encode('utf-8') else: # if not html, then lets put some whitespace if (not '
' in msg) or (not '

' in msg): - msg = msg.replace('\n','
') - + msg = msg.replace('\n','
') footer = get_footer() - msg = msg + (footer or '') - email.set_text(html2text(msg)) - email.set_html(msg) + msg = msg + (footer or '') + email.set_text(html2text(msg)) + email.set_html(msg) for p in parts: email.set_message(p[1]) for a in attach: diff --git a/py/webnotes/utils/email_lib/html2text.py b/py/webnotes/utils/email_lib/html2text.py index 66258d2477..d81c970546 100644 --- a/py/webnotes/utils/email_lib/html2text.py +++ b/py/webnotes/utils/email_lib/html2text.py @@ -447,7 +447,8 @@ def html2text_file(html, out=wrapwrite, baseurl=''): return h.close() def html2text(html, baseurl=''): - return optwrap(html2text_file(html, None, baseurl)) + txt = html2text_file(html.decode('utf-8'), None, baseurl) + return optwrap(txt.encode('utf-8')) if __name__ == "__main__": baseurl = '' diff --git a/py/webnotes/utils/email_lib/send.py b/py/webnotes/utils/email_lib/send.py index 2eff8ed9bc..52e4572459 100644 --- a/py/webnotes/utils/email_lib/send.py +++ b/py/webnotes/utils/email_lib/send.py @@ -16,6 +16,9 @@ class EMail: """ def __init__(self, sender='', recipients=[], subject='', from_defs=0, alternative=0, reply_to=None): from email.mime.multipart import MIMEMultipart + from email import Charset + Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8') + if type(recipients)==str: recipients = recipients.replace(';', ',') recipients = recipients.split(',') @@ -36,7 +39,8 @@ class EMail: Attach message in the text portion of multipart/alternative """ from email.mime.text import MIMEText - part = MIMEText(message, 'plain') + msg = unicode(message, 'utf-8') + part = MIMEText(msg.encode('utf-8'), 'plain', 'UTF-8') self.msg_multipart.attach(part) def set_html(self, message):