diff --git a/cgi-bin/webnotes/model/doclist.py b/cgi-bin/webnotes/model/doclist.py index c8df02660a..5e270a69cf 100644 --- a/cgi-bin/webnotes/model/doclist.py +++ b/cgi-bin/webnotes/model/doclist.py @@ -18,7 +18,7 @@ class DocList: self.to_docstatus = 0 if dt and dn: self.load_from_db(dt, dn) - + def load_from_db(self, dt, dn): """ Load doclist from dt @@ -34,15 +34,15 @@ class DocList: doclist = [doc,] for t in tablefields: doclist += getchildren(doc.name, t[0], t[1], dt, prefix=prefix) - + self.docs = docs - + def __iter__(self): """ Make this iterable """ return self.docs.__iter__() - + def from_compressed(self, data, docname): """ Expand called from client @@ -50,13 +50,13 @@ class DocList: from webnotes.model.utils import expand self.docs = expand(data) self.objectify(docname) - + def objectify(self, docname=None): """ Converts self.docs from a list of dicts to list of Documents """ from webnotes.model.doc import Document - + self.docs = [Document(fielddata=d) for d in self.docs] if not docname: self.doc, self.children = self.docs[0], self.docs[1:] @@ -69,21 +69,20 @@ class DocList: self.doc = d else: self.children.append(d) - # catch all if no self.doc if not self.doc: self.doc, self.children = self.docs[0], self.docs[1:] - + def make_obj(self): """ Create a DocType object """ if self.obj: return self.obj - + from webnotes.model.code import get_obj self.obj = get_obj(doc=self.doc, doclist=self.children) return self.obj - + def next(self): """ Next doc @@ -104,13 +103,13 @@ class DocList: if (not is_single(self.doc.doctype)) and (not cint(self.doc.fields.get('__islocal'))): tmp = webnotes.conn.sql(""" - SELECT modified FROM `tab%s` WHERE name="%s" for update""" + SELECT modified FROM `tab%s` WHERE name="%s" for update""" % (self.doc.doctype, self.doc.name)) if tmp and str(tmp[0][0]) != str(self.doc.modified): webnotes.msgprint(""" - Document has been modified after you have opened it. - To maintain the integrity of the data, you will not be able to save your changes. + Document has been modified after you have opened it. + To maintain the integrity of the data, you will not be able to save your changes. Please refresh this document. [%s/%s]""" % (tmp[0][0], self.doc.modified), raise_exception=1) def check_permission(self): @@ -119,7 +118,7 @@ class DocList: """ if not self.doc.check_perm(verbose=1): webnotes.msgprint("Not enough permission to save %s" % self.doc.doctype, raise_exception=1) - + def check_links(self): """ Checks integrity of links (throws exception if links are invalid) @@ -130,11 +129,11 @@ class DocList: ref[d.doctype] = d.make_link_list() err_list += d.validate_links(ref[d.doctype]) - + if err_list: - webnotes.msgprint("""[Link Validation] Could not find the following values: %s. + webnotes.msgprint("""[Link Validation] Could not find the following values: %s. Please correct and resave. Document Not Saved.""" % ', '.join(err_list), raise_exception=1) - + def update_timestamps_and_docstatus(self): """ Update owner, creation, modified_by, modified, docstatus @@ -142,17 +141,17 @@ class DocList: from webnotes.utils import now ts = now() user = webnotes.__dict__.get('session', {}).get('user') or 'Administrator' - + for d in self.docs: if self.doc.__islocal: d.owner = user d.creation = ts - + d.modified_by = user d.modified = ts if d.docstatus != 2: # don't update deleted d.docstatus = self.to_docstatus - + def prepare_for_save(self, check_links): """ Set owner, modified etc before saving @@ -175,7 +174,7 @@ class DocList: from webnotes.model.triggers import fire_event fire_event(self.doc, method) - + def save_main(self): """ Save the main doc @@ -184,7 +183,7 @@ class DocList: self.doc.save(cint(self.doc.__islocal)) except NameError, e: webnotes.msgprint('%s "%s" already exists' % (self.doc.doctype, self.doc.name)) - + # prompt if cancelled if webnotes.conn.get_value(self.doc.doctype, self.doc.name, 'docstatus')==2: webnotes.msgprint('[%s "%s" has been cancelled]' % (self.doc.doctype, self.doc.name)) @@ -197,7 +196,7 @@ class DocList: """ for d in self.children: deleted, local = d.fields.get('__deleted',0), d.fields.get('__islocal',0) - + if cint(local) and cint(deleted): pass @@ -206,7 +205,7 @@ class DocList: d.parent = self.doc.name # rename if reqd d.parenttype = self.doc.doctype - d.save(new = cint(local)) + d.save(new = cint(local)) def save(self, check_links=1): """ @@ -217,7 +216,7 @@ class DocList: self.save_main() self.save_children() self.run_method('on_update') - + def submit(self): """ Save & Submit - set docstatus = 1, run "on_submit" @@ -227,7 +226,7 @@ class DocList: self.to_docstatus = 1 self.save() self.run_method('on_submit') - + def cancel(self): """ Cancel - set docstatus 2, run "on_cancel" @@ -239,7 +238,7 @@ class DocList: self.save_main() self.save_children() self.run_method('on_cancel') - + def update_after_submit(self): """ Update after submit - some values changed after submit @@ -260,11 +259,11 @@ def getlist(doclist, parentfield): """ import webnotes.model.utils return webnotes.model.utils.getlist(doclist, parentfield) - + def copy_doclist(doclist, no_copy = []): """ Make a copy of the doclist """ import webnotes.model.utils return webnotes.model.utils.copy_doclist(doclist, no_copy) - + diff --git a/cgi-bin/webnotes/model/utils.py b/cgi-bin/webnotes/model/utils.py index 2d019273f1..b9892067d3 100644 --- a/cgi-bin/webnotes/model/utils.py +++ b/cgi-bin/webnotes/model/utils.py @@ -1,7 +1,7 @@ """ Model utilities, unclassified functions """ - + def expand(docs): """ Expand a doclist sent from the client side. (Internally used by the request handler) @@ -25,12 +25,12 @@ def compress(doclist): """ Compress a doclist before sending it to the client side. (Internally used by the request handler) - """ + """ if doclist and hasattr(doclist[0],'fields'): docs = [d.fields for d in doclist] else: docs = doclist - + kl, vl = {}, [] for d in docs: dt = d['doctype'] @@ -38,10 +38,10 @@ def compress(doclist): fl = d.keys() forbidden = ['server_code_compiled'] nl = ['doctype','localname','__oldparent','__unsaved'] - + # add client script for doctype, doctype due to ambiguity if dt=='DocType': nl.append('__client_script') - + for f in fl: if not (f in nl) and not (f in forbidden): nl.append(f) @@ -64,21 +64,24 @@ def compress(doclist): def getlist(doclist, field): """ Filter a list of records for a specific field from the full doclist - + Example:: - - # find all phone call details + + # find all phone call details dl = getlist(self.doclist, 'contact_updates') pl = [] for d in dl: if d.type=='Phone': pl.append(d) """ - + l = [] for d in doclist: if d.parent and (not d.parent.lower().startswith('old_parent:')) and d.parentfield == field: l.append(d) + + l.sort(lambda a, b: a.idx - b.idx) + return l # Copy doclist @@ -90,31 +93,31 @@ def copy_doclist(doclist, no_copy = []): Pass fields that are not to be copied in `no_copy` """ from webnotes.model.doc import Document - + cl = [] - + # main doc c = Document(fielddata = doclist[0].fields.copy()) - + # clear no_copy fields - for f in no_copy: + for f in no_copy: if c.fields.has_key(f): c.fields[f] = None - + c.name = None c.save(1) cl.append(c) - + # new parent name parent = c.name - + # children for d in doclist[1:]: c = Document(fielddata = d.fields.copy()) c.name = None - + # clear no_copy fields - for f in no_copy: + for f in no_copy: if c.fields.has_key(f): c.fields[f] = None @@ -138,18 +141,18 @@ def _make_html(doc, link_list): from webnotes.utils import cstr out = '
| %s | %s |