Merge pull request #19 from tobrahma/master
util.getlist returns sorted (by idx) list by default
This commit is contained in:
commit
96ce94ca01
2 changed files with 67 additions and 65 deletions
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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 = '<table class="simpletable">'
|
||||
for k in doc.fields.keys():
|
||||
if k!='server_code_compiled':
|
||||
if k!='server_code_compiled':
|
||||
v = cstr(doc.fields[k])
|
||||
|
||||
|
||||
# link field
|
||||
if v and (k in link_list.keys()):
|
||||
dt = link_list[k]
|
||||
if type(dt)==str and dt.startswith('link:'):
|
||||
dt = dt[5:]
|
||||
v = '<a href="index.cgi?page=Form/%s/%s">%s</a>' % (dt, v, v)
|
||||
|
||||
v = '<a href="index.cgi?page=Form/%s/%s">%s</a>' % (dt, v, v)
|
||||
|
||||
out += '\t<tr><td>%s</td><td>%s</td></tr>\n' % (cstr(k), v)
|
||||
|
||||
|
||||
out += '</table>'
|
||||
return out
|
||||
|
||||
|
|
@ -159,13 +162,13 @@ def to_html(doclist):
|
|||
"""
|
||||
out = ''
|
||||
link_lists = {}
|
||||
|
||||
|
||||
for d in doclist:
|
||||
if not link_lists.get(d.doctype):
|
||||
link_lists[d.doctype] = d.make_link_list()
|
||||
|
||||
out += _make_html(d, link_lists[d.doctype])
|
||||
|
||||
|
||||
return out
|
||||
|
||||
def commonify_doclist(doclist, with_comments=1):
|
||||
|
|
@ -184,14 +187,14 @@ def commonify_doclist(doclist, with_comments=1):
|
|||
return c
|
||||
|
||||
def strip_common(d):
|
||||
for k in common_keys:
|
||||
for k in common_keys:
|
||||
if k in d: del d[k]
|
||||
return d
|
||||
|
||||
def make_common_dicts(doclist):
|
||||
|
||||
|
||||
common_dict = {} # one per doctype
|
||||
|
||||
|
||||
# make common dicts for all records
|
||||
for d in doclist:
|
||||
if not d['doctype'] in common_dict:
|
||||
|
|
@ -206,15 +209,15 @@ def commonify_doclist(doclist, with_comments=1):
|
|||
common_dict = make_common_dicts(doclist)
|
||||
|
||||
# make docs
|
||||
final = []
|
||||
final = []
|
||||
for d in doclist:
|
||||
f = strip_common(get_diff_dict(common_dict[d['doctype']], d))
|
||||
f['doctype'] = d['doctype'] # keep doctype!
|
||||
|
||||
|
||||
# strip name for child records (only an auto generated number!)
|
||||
if f['doctype'] != doclist[0]['doctype']:
|
||||
del f['name']
|
||||
|
||||
|
||||
if with_comments:
|
||||
f['##comment'] = d['doctype'] + ('name' in f and (', ' + f['name']) or '')
|
||||
final.append(f)
|
||||
|
|
@ -226,10 +229,10 @@ def commonify_doclist(doclist, with_comments=1):
|
|||
if with_comments:
|
||||
d['##comment'] = 'These values are common for all ' + d['doctype']
|
||||
commons.append(strip_common(d))
|
||||
|
||||
|
||||
common_values = make_common(doclist)
|
||||
return [common_values]+commons+final
|
||||
|
||||
|
||||
def uncommonify_doclist(dl):
|
||||
"""
|
||||
Expands an commonified doclist
|
||||
|
|
@ -249,13 +252,13 @@ def uncommonify_doclist(dl):
|
|||
final.append(d1)
|
||||
|
||||
return final
|
||||
|
||||
|
||||
def pprint_doclist(doclist, with_comments = 1):
|
||||
"""
|
||||
Pretty Prints a doclist with common keys separated and comments
|
||||
"""
|
||||
from webnotes.utils import pprint_dict
|
||||
|
||||
|
||||
dictlist =[pprint_dict(d) for d in commonify_doclist(doclist, with_comments)]
|
||||
title = '# '+doclist[0]['doctype']+', '+doclist[0]['name']
|
||||
return title + '\n[\n' + ',\n'.join(dictlist) + '\n]'
|
||||
|
|
@ -268,5 +271,5 @@ def peval_doclist(txt):
|
|||
return uncommonify_doclist(eval(txt))
|
||||
else:
|
||||
return eval(txt)
|
||||
|
||||
|
||||
return uncommonify_doclist(eval(txt))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue