From a17bd79ed80c9c440c3c4dfa62ae61210fe9f2c6 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Thu, 11 Jul 2013 12:16:30 +0530 Subject: [PATCH] [feature] Linked With now shows parent Documents --- public/js/wn/form/layout.js | 6 +++--- public/js/wn/form/linked_with.js | 19 +++++++++++++------ public/js/wn/misc/utils.js | 6 ++++++ public/js/wn/ui/listing.js | 16 ++++++++-------- webnotes/model/doctype.py | 28 ++++++++++++++++++++-------- webnotes/widgets/form/load.py | 2 +- 6 files changed, 51 insertions(+), 26 deletions(-) diff --git a/public/js/wn/form/layout.js b/public/js/wn/form/layout.js index 7d9f267037..b4c4880b9f 100644 --- a/public/js/wn/form/layout.js +++ b/public/js/wn/form/layout.js @@ -187,9 +187,9 @@ wn.ui.form.Layout = Class.extend({ }, focus_on_next_field: function(start_idx, fields) { // loop to find next eligible fields - for(var ii= start_idx + 1, len = fields.length; ii < len; ii++) { - if(fields[ii].disp_status==="Write") { - this.set_focus(fields[ii]); + for(var i= start_idx + 1, len = fields.length; i < len; i++) { + if(fields[i].disp_status==="Write" && !in_list(wn.model.no_value_type, fields[i].df.fieldtype)) { + this.set_focus(fields[i]); break; } } diff --git a/public/js/wn/form/linked_with.js b/public/js/wn/form/linked_with.js index 4946f45e94..e5b51465ec 100644 --- a/public/js/wn/form/linked_with.js +++ b/public/js/wn/form/linked_with.js @@ -17,10 +17,16 @@ wn.ui.form.LinkedWith = Class.extend({ make_dialog: function() { var me = this; this.linked_with = this.frm.meta.__linked_with; - var links = $.map(keys(this.linked_with), function(v) { - return in_list(wn.boot.profile.can_get_report, v) ? {value:v, label:wn._(v)} : null - }).sort(function(a, b) { return a.label > b.label ? 1 : -1 }); + + var links = []; + $.each(this.linked_with, function(doctype, tmp) { + if(wn.model.can_get_report(doctype)) { + links.push({label: wn._(doctype), value: doctype}); + } + }); + links = wn.utils.sort(links, "label"); + this.dialog = new wn.ui.Dialog({ width: 700, hide_on_page_refresh: true, @@ -50,8 +56,6 @@ wn.ui.form.LinkedWith = Class.extend({ this.dialog.get_input("list_by").change(function() { me.doctype = me.dialog.get_input("list_by").val(); - me.is_table = (!in_list(wn.boot.profile.can_read, me.doctype) && - in_list(wn.boot.profile.can_get_report, me.doctype)) wn.model.with_doctype(me.doctype, function(r) { me.make_listing(); @@ -97,7 +101,10 @@ wn.ui.form.LinkedWith = Class.extend({ }); me.lst.filter_list.show_filters(true); me.lst.filter_list.clear_filters(); - me.lst.set_filter(me.linked_with[me.doctype], me.frm.doc.name); + + var link_doctype = me.linked_with[me.doctype].child_doctype || me.doctype; + + me.lst.set_filter(me.linked_with[me.doctype].fieldname, me.frm.doc.name, link_doctype); me.lst.listview = me.listview; } }); \ No newline at end of file diff --git a/public/js/wn/misc/utils.js b/public/js/wn/misc/utils.js index 8f13ace8a8..b9a208b672 100644 --- a/public/js/wn/misc/utils.js +++ b/public/js/wn/misc/utils.js @@ -153,6 +153,9 @@ wn.utils = { }, sort: function(list, key, compare_type, reverse) { + if(list.length < 2) + return list; + var sort_fn = { "string": function(a, b) { return cstr(a[key]).localeCompare(cstr(b[key])); @@ -161,6 +164,9 @@ wn.utils = { return flt(a[key]) - flt(b[key]); } }; + + if(!compare_type) + compare_type = typeof list[0][key]==="string" ? "string" : "number"; list.sort(sort_fn[compare_type]); diff --git a/public/js/wn/ui/listing.js b/public/js/wn/ui/listing.js index a9d536960d..93726953d3 100644 --- a/public/js/wn/ui/listing.js +++ b/public/js/wn/ui/listing.js @@ -349,9 +349,9 @@ wn.ui.Listing = Class.extend({ query += ' LIMIT ' + this.start + ',' + (this.page_length+1); return query }, - set_filter: function(fieldname, label) { + set_filter: function(fieldname, label, doctype) { + if(!doctype) doctype = this.doctype; var filter = this.filter_list.get_filter(fieldname); - //this.filter_list.show_filters(true); if(filter) { var v = filter.field.get_parsed_value(); if(v.indexOf(label)!=-1) { @@ -361,21 +361,21 @@ wn.ui.Listing = Class.extend({ // second filter set for this field if(fieldname=='_user_tags') { // and for tags - this.filter_list.add_filter(this.doctype, fieldname, + this.filter_list.add_filter(doctype, fieldname, 'like', '%' + label); } else { // or for rest using "in" - filter.set_values(this.doctype, fieldname, 'in', v + ', ' + label); + filter.set_values(doctype, fieldname, 'in', v + ', ' + label); } } } else { // no filter for this item, // setup one - if(fieldname=='_user_tags') { - this.filter_list.add_filter(this.doctype, fieldname, - 'like', '%' + label); + if(fieldname==='_user_tags') { + this.filter_list.add_filter(doctype, fieldname, + 'like', '%' + label); } else { - this.filter_list.add_filter(this.doctype, fieldname, '=', label); + this.filter_list.add_filter(doctype, fieldname, '=', label); } } } diff --git a/webnotes/model/doctype.py b/webnotes/model/doctype.py index 1b4b53e262..f1932bcd29 100644 --- a/webnotes/model/doctype.py +++ b/webnotes/model/doctype.py @@ -48,7 +48,6 @@ def get(doctype, processed=False, cached=True): doclist = from_cache(doctype, processed) if doclist: if processed: - add_linked_with(doclist) update_language(doclist) return DocTypeDocList(doclist) @@ -70,16 +69,13 @@ def get(doctype, processed=False, cached=True): add_print_formats(doclist) add_search_fields(doclist) add_workflows(doclist) - - # add validators - #add_validators(doctype, doclist) + add_linked_with(doclist) to_cache(doctype, processed, doclist) if processed: - add_linked_with(doclist) update_language(doclist) - + return DocTypeDocList(doclist) def load_docfield_types(): @@ -188,11 +184,27 @@ def add_linked_with(doclist): links = webnotes.conn.sql("""select parent, fieldname from tabDocField where (fieldtype="Link" and options=%s) or (fieldtype="Select" and options=%s)""", (doctype, "link:"+ doctype)) - links += webnotes.conn.sql("""select dt, fieldname from `tabCustom Field` + links += webnotes.conn.sql("""select dt as parent, fieldname from `tabCustom Field` where (fieldtype="Link" and options=%s) or (fieldtype="Select" and options=%s)""", (doctype, "link:"+ doctype)) + + links = dict(links) + + ret = {} + + for dt in links: + ret[dt] = { "fieldname": links[dt] } + + for grand_parent, options in webnotes.conn.sql("""select parent, options from tabDocField + where fieldtype="Table" + and options in (select name from tabDocType + where istable=1 and name in (%s))""" % ", ".join(["%s"] * len(links)) ,tuple(links)): + + ret[grand_parent] = {"child_doctype": options, "fieldname": links[options] } + if options in ret: + del ret[options] - doclist[0].fields["__linked_with"] = dict(list(set(links))) + doclist[0].fields["__linked_with"] = ret def from_cache(doctype, processed): """ load doclist from cache. diff --git a/webnotes/widgets/form/load.py b/webnotes/widgets/form/load.py index a83414f43d..2947b12d86 100644 --- a/webnotes/widgets/form/load.py +++ b/webnotes/widgets/form/load.py @@ -118,7 +118,7 @@ def add_assignments(dt, dn): }) return cl - + @webnotes.whitelist() def get_badge_info(doctypes, filters): filters = json.loads(filters)