浏览代码

[bean] [mandatory check]

version-14
Anand Doshi 12 年前
父节点
当前提交
7bb7221364
共有 9 个文件被更改,包括 86 次插入12 次删除
  1. +1
    -1
      templates/js/login.js
  2. +17
    -4
      webnotes/__init__.py
  3. +5
    -1
      webnotes/defaults.py
  4. +38
    -2
      webnotes/model/bean.py
  5. +4
    -1
      webnotes/model/create_new.py
  6. +2
    -0
      webnotes/model/mapper.py
  7. +14
    -2
      webnotes/model/meta.py
  8. +1
    -0
      webnotes/modules/import_file.py
  9. +4
    -1
      webnotes/test_runner.py

+ 1
- 1
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() {


+ 17
- 4
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)



+ 5
- 1
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 = {}


+ 38
- 2
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):


+ 4
- 1
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"):


+ 2
- 0
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)


+ 14
- 2
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
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]

+ 1
- 0
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


+ 4
- 1
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()


正在加载...
取消
保存