From 7bb7221364c963c04931bac99c4399501bf863b7 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 8 Jul 2013 18:48:46 +0530 Subject: [PATCH 1/2] [bean] [mandatory check] --- templates/js/login.js | 2 +- webnotes/__init__.py | 21 +++++++++++++---- webnotes/defaults.py | 6 ++++- webnotes/model/bean.py | 40 +++++++++++++++++++++++++++++++-- webnotes/model/create_new.py | 5 ++++- webnotes/model/mapper.py | 2 ++ webnotes/model/meta.py | 16 +++++++++++-- webnotes/modules/import_file.py | 1 + webnotes/test_runner.py | 5 ++++- 9 files changed, 86 insertions(+), 12 deletions(-) diff --git a/templates/js/login.js b/templates/js/login.js index 33d15a520c..54ea047537 100644 --- a/templates/js/login.js +++ b/templates/js/login.js @@ -95,7 +95,7 @@ login.show_login = function() { } window.is_login = true; - window.is_signup = false; + window.is_sign_up = false; } login.show_sign_up = function() { diff --git a/webnotes/__init__.py b/webnotes/__init__.py index 642b42f78c..e27c978257 100644 --- a/webnotes/__init__.py +++ b/webnotes/__init__.py @@ -68,6 +68,7 @@ print_messages = False user_lang = False lang = 'en' in_import = False +in_test = False # memcache @@ -88,6 +89,7 @@ class SessionStopped(Exception): pass class MappingMismatchError(ValidationError): pass class InvalidStatusError(ValidationError): pass class DoesNotExistError(ValidationError): pass +class MandatoryError(ValidationError): pass def getTraceback(): import utils @@ -303,7 +305,7 @@ def doc(doctype=None, name=None, fielddata=None): def new_doc(doctype, parent_doc=None, parentfield=None): from webnotes.model.create_new import get_new_doc return get_new_doc(doctype, parent_doc, parentfield) - + def doclist(lst=None): from webnotes.model.doclist import DocList return DocList(lst) @@ -390,11 +392,22 @@ def get_application_home_page(user='Guest'): def copy_doclist(in_doclist): new_doclist = [] - for d in in_doclist: + parent_doc = None + for i, d in enumerate(in_doclist): + is_dict = False if isinstance(d, dict): - new_doclist.append(d.copy()) + is_dict = True + values = _dict(d.copy()) else: - new_doclist.append(doc(d.fields.copy())) + values = _dict(d.fields.copy()) + + newd = new_doc(values.doctype, parent_doc=(None if i==0 else parent_doc), parentfield=values.parentfield) + newd.fields.update(values) + + if i==0: + parent_doc = newd + + new_doclist.append(newd.fields if is_dict else newd) return doclist(new_doclist) diff --git a/webnotes/defaults.py b/webnotes/defaults.py index 065110aa22..f10ba941d2 100644 --- a/webnotes/defaults.py +++ b/webnotes/defaults.py @@ -24,7 +24,11 @@ def get_defaults(user=None): if user: userd = get_defaults_for(user) - userd.update({"user": user, "owner": user}) + + if user in ["__global", "Control Panel"]: + userd.update({"user": webnotes.session.user, "owner": webnotes.session.user}) + else: + userd.update({"user": user, "owner": user}) else: userd = {} diff --git a/webnotes/model/bean.py b/webnotes/model/bean.py index f244c8e183..2f9ed548fc 100644 --- a/webnotes/model/bean.py +++ b/webnotes/model/bean.py @@ -29,7 +29,7 @@ Group actions like save, etc are performed on doclists """ import webnotes -from webnotes import _ +from webnotes import _, msgprint from webnotes.utils import cint, cstr from webnotes.model.doc import Document @@ -47,6 +47,7 @@ class Bean: self.ignore_check_links = False self.ignore_validate = False self.ignore_fields = False + self.ignore_mandatory = False if isinstance(dt, basestring) and not dn: dn = dt @@ -194,8 +195,10 @@ class Bean: def prepare_for_save(self, method): self.check_if_latest(method) + if method != "cancel": self.check_links() + self.update_timestamps_and_docstatus() self.update_parent_info() @@ -272,6 +275,11 @@ class Bean: def insert(self): self.doc.fields["__islocal"] = 1 + + if webnotes.in_test: + if webnotes.get_doctype(self.doc.doctype).get_field("naming_series"): + self.doc.naming_series = "_T-" + self.doc.doctype + "-" + return self.save() def has_read_perm(self): @@ -283,6 +291,8 @@ class Bean: self.prepare_for_save("save") if not self.ignore_validate: self.run_method('validate') + if not self.ignore_mandatory: + self.check_mandatory() self.save_main() self.save_children() self.run_method('on_update') @@ -296,6 +306,7 @@ class Bean: self.to_docstatus = 1 self.prepare_for_save("submit") self.run_method('validate') + self.check_mandatory() self.save_main() self.save_children() self.run_method('on_update') @@ -344,8 +355,33 @@ class Bean: def check_no_back_links_exist(self): from webnotes.model.utils import check_if_doc_is_linked check_if_doc_is_linked(self.doc.doctype, self.doc.name, method="Cancel") + + def check_mandatory(self): + missing = [] + from webnotes.model.meta import get_mandatory_fields + for doc in self.doclist: + for fieldname, label, fieldtype in get_mandatory_fields(doc.doctype): + msg = "" + if fieldtype == "Table": + if not self.doclist.get({"parentfield": fieldname}): + msg = _("Error") + ": " + _("Data missing in table") + ": " + _(label) + + elif doc.fields.get(fieldname) is None: + msg = _("Error") + ": " + if doc.parentfield: + msg += _("Row") + (" # %d: " % doc.idx) + + msg += _("Value missing for") + ": " + _(label) + + if msg: + missing.append([msg, fieldname]) + + if missing: + for msg, fieldname in missing: + msgprint(msg) - + raise webnotes.MandatoryError, ", ".join([fieldname for msg, fieldname in missing]) + def clone(source_wrapper): """ make a clone of a document""" if isinstance(source_wrapper, list): diff --git a/webnotes/model/create_new.py b/webnotes/model/create_new.py index d5d79e3833..53acfda5eb 100644 --- a/webnotes/model/create_new.py +++ b/webnotes/model/create_new.py @@ -5,6 +5,7 @@ Create a new document with defaults set import webnotes from webnotes.utils import nowdate, nowtime, cint, flt +import webnotes.defaults def get_new_doc(doctype, parent_doc = None, parentfield = None): doc = webnotes.doc({ @@ -17,10 +18,12 @@ def get_new_doc(doctype, parent_doc = None, parentfield = None): if parent_doc: doc.parent = parent_doc.name doc.parenttype = parent_doc.doctype + + if parentfield: doc.parentfield = parentfield for d in meta.get({"doctype":"DocField", "parent": doctype}): - default = webnotes.conn.get_default(d.fieldname) + default = webnotes.defaults.get_user_default(d.fieldname) if default: doc.fields[d.fieldname] = default elif d.fields.get("default"): diff --git a/webnotes/model/mapper.py b/webnotes/model/mapper.py index 4953afe0ef..ddc0d8a2e3 100644 --- a/webnotes/model/mapper.py +++ b/webnotes/model/mapper.py @@ -66,6 +66,8 @@ def get_mapped_doclist(from_doctype, from_docname, table_maps, target_doclist=[] map_doc(source_d, target_d, table_map, source_meta, target_meta, source.doclist[0]) doclist.append(target_d) + doclist = webnotes.doclist(doclist) + if postprocess: postprocess(source, doclist) diff --git a/webnotes/model/meta.py b/webnotes/model/meta.py index 143ce703ac..ce481018d7 100644 --- a/webnotes/model/meta.py +++ b/webnotes/model/meta.py @@ -24,7 +24,7 @@ from __future__ import unicode_literals import webnotes -from webnotes.utils import cstr +from webnotes.utils import cstr, cint def get_dt_values(doctype, fields, as_dict = 0): return webnotes.conn.sql('SELECT %s FROM tabDocType WHERE name="%s"' % (fields, doctype), as_dict = as_dict) @@ -112,4 +112,16 @@ def get_field_precision(df, doc): decimal_str, comma_str, precision = get_number_format_info(number_format or \ webnotes.conn.get_default("number_format") or "#,###.##") - return precision \ No newline at end of file + return precision + +doctype_mandatory_fields = {} +def get_mandatory_fields(doctype, parenttype=None): + if not doctype_mandatory_fields.get(doctype): + doctype_mandatory_fields[doctype] = [] + + meta = webnotes.get_doctype(parenttype or doctype) + for df in meta.get({"doctype": "DocField", "parent": doctype}): + if cint(df.reqd): + doctype_mandatory_fields[doctype].append((df.fieldname, df.label, df.fieldtype)) + + return doctype_mandatory_fields[doctype] \ No newline at end of file diff --git a/webnotes/modules/import_file.py b/webnotes/modules/import_file.py index 13e38bb94b..79f0af5cfc 100644 --- a/webnotes/modules/import_file.py +++ b/webnotes/modules/import_file.py @@ -107,6 +107,7 @@ def import_doclist(doclist): new_bean.ignore_check_links = True new_bean.ignore_validate = True new_bean.ignore_permissions = True + new_bean.ignore_mandatory = True if doctype=="DocType" and name in ["DocField", "DocType"]: new_bean.ignore_fields = True diff --git a/webnotes/test_runner.py b/webnotes/test_runner.py index 7a57062bb7..05215ecec3 100644 --- a/webnotes/test_runner.py +++ b/webnotes/test_runner.py @@ -71,7 +71,8 @@ def make_test_objects(doctype, test_records, verbose=None): for doclist in test_records: if not "doctype" in doclist[0]: doclist[0]["doctype"] = doctype - d = webnotes.bean((webnotes.doclist(doclist)).copy()) + d = webnotes.bean(copy=doclist) + if webnotes.test_objects.get(d.doc.doctype): # do not create test records, if already exists return [] @@ -186,6 +187,8 @@ if __name__=="__main__": args = parser.parse_args() webnotes.print_messages = args.verbose + webnotes.in_test = True + if not webnotes.conn: webnotes.connect() From 42adcb85d81693855bb9761aea61d9eef5567f9c Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 8 Jul 2013 19:46:40 +0530 Subject: [PATCH 2/2] [bean] [run_method] call set_doclist so that if doclist is modified, it makes sure that doclist is of type DocList --- webnotes/model/bean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webnotes/model/bean.py b/webnotes/model/bean.py index 2f9ed548fc..d194d28b3c 100644 --- a/webnotes/model/bean.py +++ b/webnotes/model/bean.py @@ -228,7 +228,7 @@ class Bean: notify(self.controller, method) - self.doclist = self.controller.doclist + self.set_doclist(self.controller.doclist) def get_method(self, method): self.make_controller()