瀏覽代碼

[fixes] frappe.db.set_value, added frm.events for callable methods

version-14
Rushabh Mehta 10 年之前
父節點
當前提交
08d527786f
共有 3 個檔案被更改,包括 39 行新增31 行删除
  1. +17
    -12
      frappe/database.py
  2. +4
    -0
      frappe/public/js/frappe/form/script_manager.js
  3. +18
    -19
      frappe/public/js/legacy/form.js

+ 17
- 12
frappe/database.py 查看文件

@@ -524,9 +524,10 @@ class Database:
"""Update multiple values. Alias for `set_value`."""
return self.set_value(*args, **kwargs)

def set_value(self, dt, dn, field, val, modified=None, modified_by=None):
def set_value(self, dt, dn, field, val, modified=None, modified_by=None,
update_modified=True, debug=False):
"""Set a single value in the database, do not call the ORM triggers
but update the modified timestamp.
but update the modified timestamp (unless specified not to).

**Warning:** this function will not call Document events and should be avoided in normal cases.

@@ -536,6 +537,8 @@ class Database:
:param value: Value to be updated.
:param modified: Use this as the `modified` timestamp.
:param modified_by: Set this user as `modified_by`.
:param update_modified: default True. Set as false, if you don't want to update the timestamp.
:param debug: Print the query in the developer / js console.
"""
if not modified:
modified = now()
@@ -545,20 +548,22 @@ class Database:
if dn and dt!=dn:
conditions, values = self.build_conditions(dn)

values.update({"val": val, "modified": modified, "modified_by": modified_by})
if update_modified:
values.update({"val": val, "modified": modified, "modified_by": modified_by})

self.sql("""update `tab{0}` set `{1}`=%(val)s, modified=%(modified)s, modified_by=%(modified_by)s where
{2}""".format(dt, field, conditions), values, debug=debug)
else:
self.sql("""update `tab{0}` set `{1}`=%s where
{2}""".format(dt, field, conditions), val, debug=debug)

self.sql("""update `tab{0}` set `{1}`=%(val)s, modified=%(modified)s, modified_by=%(modified_by)s where
{2}""".format(dt, field, conditions), values)

else:
if self.sql("select value from tabSingles where field=%s and doctype=%s", (field, dt)):
self.sql("""update tabSingles set value=%s where field=%s and doctype=%s""",
(val, field, dt))
else:
self.sql("""insert into tabSingles(doctype, field, value)
values (%s, %s, %s)""", (dt, field, val))
self.sql("delete from tabSingles where field=%s and doctype=%s", (field, dt))
self.sql("insert into tabSingles(doctype, field, value) values (%s, %s, %s)",
(dt, field, val), debug=debug)

if field not in ("modified", "modified_by"):
if update_modified and (field not in ("modified", "modified_by")):
self.set_value(dt, dn, "modified", modified)
self.set_value(dt, dn, "modified_by", modified_by)



+ 4
- 0
frappe/public/js/frappe/form/script_manager.js 查看文件

@@ -12,6 +12,10 @@ frappe.ui.form.on = frappe.ui.form.on_change = function(doctype, fieldname, hand
frappe.ui.form.handlers[doctype][fieldname] = [];
}
frappe.ui.form.handlers[doctype][fieldname].push(handler);

// add last handler to events so it can be called as
// frm.events.handler(frm)
cur_frm.events[fieldname] = handler;
}

if (!handler && $.isPlainObject(fieldname)) {


+ 18
- 19
frappe/public/js/legacy/form.js 查看文件

@@ -37,6 +37,7 @@ _f.Frm = function(doctype, parent, in_form) {
this.sections = [];
this.grids = [];
this.cscript = new frappe.ui.form.Controller({frm:this});
this.events = {};
this.pformat = {};
this.fetch_dict = {};
this.parent = parent;
@@ -505,26 +506,20 @@ _f.Frm.prototype.runscript = function(scriptname, callingfield, onrefresh) {
if(callingfield)
$(callingfield.input).set_working();

return $c('runserverobj', {'docs':this.doc, 'method':scriptname },
function(r, rtxt) {
// run refresh
if(onrefresh)
onrefresh(r,rtxt);

// fields
me.refresh_fields();

// enable button
if(callingfield)
$(callingfield.input).done_working();
},
// error
function() {
// enable button
if(callingfield)
$(callingfield.input).done_working();
frappe.call({
method: "runserverobj",
args: {'docs':this.doc, 'method':scriptname },
btn: callingfield.$input,
callback: function(r) {
if(!r.exc) {
if(onrefresh) {
onrefresh(r);
}

me.refresh_fields();
}
}
);
});
}
}

@@ -795,3 +790,7 @@ _f.Frm.prototype.validate_form_action = function(action) {
frappe.throw (__("No permission to '{0}' {1}", [__(action), __(this.doc.doctype)]));
}
};

_f.Frm.prototype.get_handlers = function(fieldname, doctype, docname) {
return this.script_manager.get_handlers(fieldname, doctype || this.doctype, docname || this.docname)
}

Loading…
取消
儲存