Explorar el Código

[buying] started cleanup, [mapper] started rewrite

version-14
Rushabh Mehta hace 12 años
padre
commit
7c174f60b3
Se han modificado 11 ficheros con 173 adiciones y 22 borrados
  1. +1
    -0
      core/doctype/doctype_mapper/doctype_mapper.py
  2. +15
    -0
      public/css/ui/common.css
  3. +1
    -1
      public/js/legacy/widgets/form/clientscriptAPI.js
  4. +1
    -1
      public/js/legacy/widgets/form/form.js
  5. +1
    -1
      public/js/wn/form/grid.js
  6. +32
    -17
      public/js/wn/model/create_new.js
  7. +3
    -1
      public/js/wn/model/sync.js
  8. +4
    -0
      webnotes/__init__.py
  9. +3
    -1
      webnotes/handler.py
  10. +48
    -0
      webnotes/model/create_new.py
  11. +64
    -0
      webnotes/model/mapper.py

+ 1
- 0
core/doctype/doctype_mapper/doctype_mapper.py Ver fichero

@@ -306,3 +306,4 @@ class DocType:
if hasattr(conf, 'developer_mode') and conf.developer_mode:
from webnotes.modules.export_file import export_to_files
export_to_files(record_list=[[self.doc.doctype, self.doc.name]])


+ 15
- 0
public/css/ui/common.css Ver fichero

@@ -22,6 +22,21 @@ a {
padding-bottom: 10px;
}

/* freeze */

div#freeze {
position: fixed;
display: none;
top: 0px;
left: 0px;
bottom: 0px;
right: 0px;
background-color: #aaa;
opacity: 0.6;
z-index: 1;
text-align: center;
}

/* listing */

.list-row {


+ 1
- 1
public/js/legacy/widgets/form/clientscriptAPI.js Ver fichero

@@ -165,7 +165,7 @@ _f.Frm.prototype.get_doclist = function() {
}

_f.Frm.prototype.field_map = function(fnames, fn) {
if(typeof fnames=='string') {
if(typeof fnames==='string') {
if(fnames == '*') {
fnames = keys(this.fields_dict);
} else {


+ 1
- 1
public/js/legacy/widgets/form/form.js Ver fichero

@@ -151,8 +151,8 @@ _f.Frm.prototype.watch_model_updates = function() {
if(doc.name===me.docname) {
me.fields_dict[fieldname]
&& me.fields_dict[fieldname].refresh(fieldname);
me.script_manager.trigger(fieldname, doc.doctype, doc.name);
me.refresh_dependency();
me.script_manager.trigger(fieldname, doc.doctype, doc.name);
}
})


+ 1
- 1
public/js/wn/form/grid.js Ver fichero

@@ -163,7 +163,7 @@ wn.ui.form.GridRow = Class.extend({
<button class="btn btn-success pull-right" \
title="'+wn._("Close")+'"\
style="margin-left: 7px;">\
<i class="icon-ok"></i></button>\
<i class="icon-chevron-up"></i></button>\
<button class="btn btn-default pull-right grid-insert-row" \
title="'+wn._("Insert Row")+'"\
style="margin-left: 7px;">\


+ 32
- 17
public/js/wn/model/create_new.js Ver fichero

@@ -4,7 +4,7 @@ $.extend(wn.model, {
new_names: {},
new_name_count: {},

get_new_doc: function(doctype) {
get_new_doc: function(doctype, parent_doc) {
wn.provide("locals." + doctype);
var doc = {
docstatus: 0,
@@ -14,10 +14,10 @@ $.extend(wn.model, {
__unsaved: 1,
owner: user
};
wn.model.set_default_values(doc);
wn.model.set_default_values(doc, parent_doc);
locals[doctype][doc.name] = doc;
wn.provide("wn.model.docinfo." + doctype + "." + doc.name);
return doc;
return doc;
},
make_new_doc_and_get_name: function(doctype) {
@@ -32,7 +32,7 @@ $.extend(wn.model, {
return 'New '+ doctype + ' ' + cnt[doctype];
},
set_default_values: function(doc) {
set_default_values: function(doc, parent_doc) {
var doctype = doc.doctype;
var docfields = wn.meta.docfield_list[doctype] || [];
var updated = [];
@@ -40,7 +40,7 @@ $.extend(wn.model, {
for(var fid=0;fid<docfields.length;fid++) {
var f = docfields[fid];
if(!in_list(wn.model.no_value_type, f.fieldtype) && doc[f.fieldname]==null) {
var v = wn.model.get_default_value(f, doc);
var v = wn.model.get_default_value(f, doc, parent_doc);
if(v) {
if(in_list(["Int", "Check"], f.fieldtype))
v = cint(v);
@@ -55,32 +55,31 @@ $.extend(wn.model, {
return updated;
},
get_default_value: function(df, doc) {
get_default_value: function(df, doc, parent_doc) {
var def_vals = {
"_Login": user,
"__user": user,
"Today": dateutil.get_today(),
"__today": dateutil.get_today(),
"Now": dateutil.get_cur_time()
}
if(def_vals[df["default"]])
if(wn.defaults.get_user_default(df.fieldname))
return wn.defaults.get_user_default(df.fieldname);
else if(df["default"] && df["default"][0]===":")
return wn.model.get_default_from_boot_docs(df, doc, parent_doc);
else if(def_vals[df["default"]])
return def_vals[df["default"]];
else if(df.fieldtype=="Time" && (!df["default"]))
return dateutil.get_cur_time()
else if(df["default"] && df["default"][0]!==":")
return df["default"];
else if(wn.defaults.get_user_default(df.fieldname))
return wn.defaults.get_user_default(df.fieldname);
else if(df["default"] && df["default"][0]===":")
return wn.model.get_default_from_boot_docs(df, doc);
},
get_default_from_boot_docs: function(df, doc) {
get_default_from_boot_docs: function(df, doc, parent_doc) {
// set default from partial docs passed during boot like ":Profile"
if(wn.model.get(df["default"]).length > 0) {
var ref_fieldname = df["default"].slice(1).toLowerCase().replace(" ", "_");
var ref_value = (doc && doc[ref_fieldname]) || (cur_frm && cur_frm.doc[ref_fieldname]);
var ref_value = parent_doc ?
parent_doc[ref_fieldname] :
wn.defaults.get_user_default(ref_fieldname);
var ref_doc = ref_value ? wn.model.get_doc(df["default"], ref_value) : null;
if(ref_doc && ref_doc[df.fieldname]) {
@@ -96,7 +95,7 @@ $.extend(wn.model, {
wn.model.get_children(doctype, parent_doc.name, parentfield,
parent_doc.doctype).length + 1;

var d = wn.model.get_new_doc(doctype);
var d = wn.model.get_new_doc(doctype, parent_doc);
$.extend(d, {
parent: parent_doc.name,
parentfield: parentfield,
@@ -129,4 +128,20 @@ $.extend(wn.model, {
return newdoc;
},
open_mapped_doc: function(opts) {
wn.call({
type: "GET",
method: opts.method,
args: {
"source_name": opts.source_name
},
callback: function(r) {
if(!r.exc) {
var doclist = wn.model.sync(r.message);
wn.set_route("Form", doclist[0].doctype, doclist[0].name);
}
}
})
}
})

+ 3
- 1
public/js/wn/model/sync.js Ver fichero

@@ -36,8 +36,10 @@ $.extend(wn.model, {
wn.model.clear_doclist(doclist[0].doctype, doclist[0].name)

$.each(doclist, function(i, d) {
if(!d.name) // get name (local if required)
if(!d.name) { // get name (local if required)
d.name = wn.model.get_new_name(d.doctype);
wn.provide("wn.model.docinfo." + d.doctype + "." + d.name);
}
if(!locals[d.doctype])
locals[d.doctype] = {};


+ 4
- 0
webnotes/__init__.py Ver fichero

@@ -300,6 +300,10 @@ def doc(doctype=None, name=None, fielddata=None):
from webnotes.model.doc import Document
return Document(doctype, name, fielddata)

def new_doc(doctype):
from webnotes.model.create_new import get_new_doc
return get_new_doc(doctype)

def doclist(lst=None):
from webnotes.model.doclist import DocList
return DocList(lst)


+ 3
- 1
webnotes/handler.py Ver fichero

@@ -91,7 +91,9 @@ def dt_map():
from_to_list = form_dict.get('from_to_list')
dm = get_obj('DocType Mapper', from_doctype +'-' + to_doctype)
dl = dm.dt_map(from_doctype, to_doctype, from_docname, Document(fielddata = dt_list[0]), (len(dt_list) > 1) and Bean(dt_list).doclist or [], from_to_list)
dl = dm.dt_map(from_doctype, to_doctype, from_docname,
Document(fielddata = dt_list[0]),
(len(dt_list) > 1) and Bean(dt_list).doclist or [], from_to_list)
webnotes.response['docs'] = dl



+ 48
- 0
webnotes/model/create_new.py Ver fichero

@@ -0,0 +1,48 @@
from __future__ import unicode_literals
"""
Create a new document with defaults set
"""

import webnotes
from webnotes.utils import nowdate, nowtime, cint, flt

def get_new_doc(doctype, parent_doc = None):
doc = webnotes.doc({
"doctype": doctype,
"__islocal": 1
})
meta = webnotes.get_doctype(doctype)
for d in meta.get({"doctype":"DocField"}):
default = webnotes.conn.get_default(d.fieldname)
if default:
doc.fields[d.fieldname] = default
elif d.fields.get("default"):
if d.default == "__user":
doc.fields[d.fieldname] = webnotes.session.user
elif d.default == "Today":
doc.fields[d.fieldname] = nowdate()

elif d.default.startswith(":"):
ref_fieldname = d.default[1:].lower().replace(" ", "_")
if parent_doc:
ref_docname = parent_doc.fields[ref_fieldname]
else:
ref_docname = webnotes.conn.get_default(ref_fieldname)
doc.fields[d.fieldname] = webnotes.conn.get_value(d.default[1:],
ref_docname, d.fieldname)

else:
doc.fields[d.fieldname] = d.default
# convert type of default
if d.fieldtype in ("Int", "Check"):
doc.fields[d.fieldname] = cint(doc.fields[d.fieldname])
elif d.fieldtype in ("Float", "Currency"):
doc.fields[d.fieldname] = flt(doc.fields[d.fieldname])
elif d.fieldtype == "Time":
doc.fields[d.fieldname] = nowtime()
return doc

+ 64
- 0
webnotes/model/mapper.py Ver fichero

@@ -0,0 +1,64 @@
# Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
#
# MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

from __future__ import unicode_literals
import webnotes

def get_mapped_doclist(from_doctype, from_docname, table_maps, target_doclist=[]):
if not webnotes.has_permission(from_doctype, from_docname):
webnotes.msgprint("No Permission", raise_exception=webnotes.PermissionError)

from webnotes.model import default_fields
source = webnotes.bean(from_doctype, from_docname)
# main
if target_doclist:
target_doc = webnotes.doc(target_doclist[0])
else:
target_doc = webnotes.new_doc(table_maps[from_doctype]["doctype"])
no_copy_fields = set(get_no_copy_fields(from_doctype) \
+ get_no_copy_fields(table_maps[from_doctype]["doctype"]) \
+ default_fields)

# map same fields
for key in target_doc.fields:
if key not in no_copy_fields:
val = source.doc.fields.get(key)
if val not in (None, ""):
target_doc.fields[key] = val

# map other fields
for source_key, target_key in table_maps[from_doctype].get("field_map", {}).items():
val = source.doc.fields.get(source_key)
if val not in (None, ""):
target_doc.fields[target_key] = val

return [target_doc]

def get_no_copy_fields(doctype):
meta = webnotes.get_doctype(doctype)
no_copy_fields = []
for d in meta.get({"doctype":"DocField", "no_copy": 1}):
no_copy_fields.append(d.fieldname)
return no_copy_fields

Cargando…
Cancelar
Guardar