This commit is contained in:
Rushabh Mehta 2012-05-09 12:01:46 +05:30
commit 2f39be52d0
5 changed files with 82 additions and 54 deletions

View file

@ -61,15 +61,19 @@ _e.make = function() {
if(!emailfrom)
emailfrom = user_email;
emailto = emailto.replace(/ /g, "");
// validate email ids
var email_list = emailto.split(/[,|;]/);
var valid = 1;
for(var i=0;i<email_list.length;i++){
if(!validate_email(email_list[i])) {
if(!email_list[i]) {
email_list.splice(i, 1);
} else if(!validate_email(email_list[i])) {
msgprint('error:'+email_list[i] + ' is not a valid email id');
valid = 0;
}
}
}
emailto = email_list.join(",");
// validate from
if(emailfrom && !validate_email(emailfrom)) {

View file

@ -23,12 +23,12 @@
# Please edit this list and import only required elements
import webnotes
from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, generate_hash, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add
from webnotes.utils import cint, cstr, default_fields, flt, formatdate, get_defaults, getdate, now, nowdate, replace_newlines, set_default
from webnotes.model import db_exists, default_fields
from webnotes.model.doc import Document, addchild, removechild, getchildren, make_autoname, SuperDocType
from webnotes.model.doclist import getlist, copy_doclist
from webnotes.model.code import get_obj, get_server_obj, run_server_obj, updatedb, check_syntax
from webnotes import session, form, is_testing, msgprint, errprint
from webnotes.model.doclist import getlist
from webnotes.model.code import get_obj
from webnotes import session, form, msgprint, errprint
from webnotes.model.doctype import get
set = webnotes.conn.set
@ -46,7 +46,6 @@ class DocType:
self.doclist = doclist
self.ref_doc = ''
# Autoname
#---------------------------------------------------------------------------
def autoname(self):
self.doc.name = make_autoname(self.doc.from_doctype + '-' + self.doc.to_doctype)
@ -74,7 +73,8 @@ 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" % self.doc.name, as_dict=1)
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)
for t in tbl_list:
if [t['from_table'], t['to_table']] in eval(from_to_list):
@ -118,7 +118,8 @@ class DocType:
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']))
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
@ -141,10 +142,16 @@ class DocType:
exception_flds = copy.copy(default_fields)
exception_flds += [f[1] for f in flds]
from_flds = [d.fieldname for d in get(t['from_table']) if cint(d.no_copy) == 0 and d.docstatus != 2 and d.fieldname and d.fieldtype not in ('Table', 'Section Break', 'Column Break', 'HTML')]
to_flds = [d.fieldname for d in get(t['to_table']) if cint(d.no_copy) == 0 and d.docstatus != 2 and d.fieldname and d.fieldtype not in ('Table', 'Section Break', 'Column Break', 'HTML')]
from_flds = [d.fieldname for d in get(t['from_table']) \
if cint(d.no_copy) == 0 and d.docstatus != 2 and d.fieldname \
and d.fieldtype not in ('Table', 'Section Break', 'Column Break', 'HTML')]
similar_flds = [[d, d, 'Yes'] for d in from_flds if d in to_flds and d not in exception_flds]
to_flds = [d.fieldname for d in get(t['to_table']) \
if cint(d.no_copy) == 0 and d.docstatus != 2 and d.fieldname \
and d.fieldtype not in ('Table', 'Section Break', 'Column Break', 'HTML')]
similar_flds = [[d, d, 'Yes'] for d in from_flds \
if d in to_flds and d not in exception_flds]
return similar_flds
@ -187,7 +194,6 @@ class DocType:
self.check_fields_in_dt()
#---------------------------------------------------------------------------
def check_fields_in_dt(self):
"""
Check if any wrong fieldname entered in mapper
@ -201,14 +207,13 @@ class DocType:
for d in getlist(self.doclist, 'field_mapper_details'):
# Default fields like name, parent, owner does not exists in DocField
if d.from_field not in flds[cstr(d.match_id)][1] and d.from_field not in default_fields:
msgprint('"' + cstr(d.from_field) + '" does not exists in DocType "' + cstr(flds[cstr(d.match_id)][0]) + '"')
msgprint("'%s' does not exists in DocType: '%s'" % (cstr(d.from_field), cstr(flds[cstr(d.match_id)][0])))
if d.to_field not in flds[cstr(d.match_id)][3] and d.to_field not in default_fields:
msgprint('"' + cstr(d.to_field) + '" does not exists in DocType "' + cstr(flds[cstr(d.match_id)][2]) + '"')
msgprint("'%s' does not exists in DocType: '%s'" % (cstr(d.to_field), cstr(flds[cstr(d.match_id)][2])))
# Check consistency of value with reference document
#---------------------------------------------------
def validate_reference_value(self, obj, to_docname):
""" Check consistency of value with reference document"""
for t in getlist(self.doclist, 'table_mapper_details'):
# Reference key is the fieldname which will relate to the from_table
if t.reference_doctype_key:
@ -216,68 +221,76 @@ class DocType:
if d.fields[t.reference_doctype_key] == self.doc.from_doctype:
self.check_consistency(obj.doc, d, to_docname)
self.check_ref_docstatus()
# Make list of fields whose value will be consistent with prevdoc
#-----------------------------------------------------------------
def get_checklist(self):
""" Make list of fields whose value will be consistent with prevdoc """
checklist = []
for f in getlist(self.doclist, 'field_mapper_details'):
# Check which field's value will be compared
if f.checking_operator:
checklist.append([f.from_field, f.to_field, f.checking_operator, f.match_id])
checklist.append({'from_fld': f.from_field, 'to_fld': f.to_field, 'op': f.checking_operator, 'match_id': f.match_id})
return checklist
# Check consistency
#-------------------
def get_label_and_type(self, from_dt, to_dt):
"""get label, fieldtype"""
from_flds, to_flds = {}, {}
for d in get(from_dt):
from_flds[d.fieldname] = {'label': d.label, 'fieldtype': d.fieldtype}
for d in get(to_dt):
to_flds[d.fieldname] = {'label': d.label, 'fieldtype': d.fieldtype}
return from_flds, to_flds
def check_consistency(self, par_obj, child_obj, to_docname):
"""Check whether values between from_dt and to_dt are consistent"""
checklist = self.get_checklist()
self.ref_doc = ''
for t in getlist(self.doclist, 'table_mapper_details'):
if t.reference_key and child_obj.fields[t.reference_key]:
from_flds, to_flds = {}, {}
for d in get(t.from_table):
from_flds[d.fieldname] = [d.label, d.fieldtype]
for d in get(t.to_table):
to_flds[d.fieldname] = [d.label, d.fieldtype]
from_flds, to_flds = self.get_label_and_type(t.from_table, t.to_table)
for cl in checklist:
if cl[3] == t.match_id:
if cl['match_id'] == t.match_id:
if t.to_field:
cur_val = child_obj.fields[cl[1]]
cur_val = child_obj.fields[cl['to_fld']]
else:
cur_val = par_obj.fields[cl[1]]
cur_val = par_obj.fields[cl['to_fld']]
if to_flds[cl[1]][1] in ['Currency', 'Float']:
if to_flds[cl['to_fld']]['fieldtype'] in ['Currency', 'Float']:
cur_val = '%.2f' % flt(cur_val)
if cl[2] == '=' and to_flds[cl[1]][1] in ['Currency', '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]))
if cl['op'] == '=' and to_flds[cl['to_fld']]['fieldtype'] in ['Currency', 'Float']:
consistent = sql("select name, %s from `tab%s` where name = '%s' and '%s' - %s <= 0.5" \
% (cl['from_fld'], t.from_table, child_obj.fields[t.reference_key], flt(cur_val), cl['from_fld']))
else:
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],
to_flds[cl[1]][1] in ('Currency', 'Float', 'Int') and flt(cur_val) or cstr(cur_val), cl[2], cl[0]))
consistent = sql("select name, %s from `tab%s` where name = '%s' and '%s' %s ifnull(%s, '')" \
% (cl['from_fld'], t.from_table, child_obj.fields[t.reference_key], \
to_flds[cl['to_fld']]['fieldtype'] in ('Currency', 'Float', 'Int') and flt(cur_val) or cstr(cur_val), cl['op'], cl['from_fld']))
if not self.ref_doc:
det = sql("select name, parent from `tab%s` where name = '%s'" % (t.from_table, child_obj.fields[t.reference_key]))
self.ref_doc = det[0][1] and det[0][1] or det[0][0]
self.ref_doc = det[0][1] and det[0][1] or det[0][0]
if not consistent:
self.give_message(from_flds[cl[0]][0], to_flds[cl[1]][0], cl[2])
self.give_message(from_flds[cl['from_fld']]['label'], to_flds[cl['to_fld']]['label'], cl['op'])
# Gives message and raise exception
#-----------------------------------
def give_message(self, from_label, to_label, operator):
""" Gives message and raise exception"""
op_in_words = {'=':'equal to ', '>=':'greater than equal to ', '>':'greater than ', '<=':'less than equal to ', '<':'less than '}
msgprint(to_label + " should be " + op_in_words[operator] + from_label + " of " + self.doc.from_doctype + ": " + self.ref_doc, raise_exception=1)
msgprint("%s should be %s %s of %s: %s" % (to_label, op_in_words[operator], from_label, 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=1)
msgprint("%s: %s does not exists in the system" % (self.doc.from_doctype, self.ref_doc), 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=1)
msgprint("%s: %s is not submitted document." % (self.doc.from_doctype, self.ref_doc), raise_exception=1)
def on_update(self):
"""

View file

@ -149,7 +149,7 @@ def remove_file(path):
if e.args[0]!=2:
raise e
def connect(db_name=None):
def connect(db_name=None, password=None):
"""
Connect to this db (or db), if called from command prompt
"""
@ -158,7 +158,7 @@ def connect(db_name=None):
import webnotes.db
global conn
conn = webnotes.db.Database(user=db_name)
conn = webnotes.db.Database(user=db_name, password=password)
global session
session = {'user':'Administrator'}

View file

@ -57,12 +57,18 @@ def get_link_fields(doctype):
"""
Returns list of link fields for a doctype in tuple (fieldname, options, label)
"""
return webnotes.conn.sql("""
SELECT fieldname, options, label
FROM tabDocField
WHERE parent='%s'
and (fieldtype='Link' or (fieldtype='Select' and `options` like 'link:%%'))
and fieldname!='owner'""" % (doctype))
import webnotes.model.doctype
doclist = webnotes.model.doctype.get(doctype)
return [
(d.fields.get('fieldname'), d.fields.get('options'), d.fields.get('label'))
for d in doclist
if d.fields.get('doctype') == 'DocField' and d.fields.get('parent') == doctype
and d.fields.get('fieldname')!='owner'
and (d.fields.get('fieldtype') == 'Link' or
( d.fields.get('fieldtype') == 'Select'
and (d.fields.get('options') or '').startswith('link:'))
)
]
#=================================================================================

7
wnf.py
View file

@ -78,6 +78,8 @@ def setup_options():
parser.add_option("-d", "--db",
dest="db_name",
help="Apply the patches on given db")
parser.add_option("--password",
help="Password for given db", nargs=1)
# build
parser.add_option("-b", "--build", default=False, action="store_true",
@ -158,7 +160,10 @@ def run():
# connect
if options.db_name is not None:
webnotes.connect(options.db_name)
if options.password:
webnotes.connect(options.db_name, options.password)
else:
webnotes.connect(options.db_name)
elif not options.install:
webnotes.connect(conf.db_name)