This commit is contained in:
Anand Doshi 2014-04-08 20:09:24 +05:30
parent 48dfc29548
commit a134be1d81
4 changed files with 83 additions and 85 deletions

View file

@ -1,5 +1,5 @@
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
# MIT License. See license.txt
from __future__ import unicode_literals
import frappe, os
@ -14,26 +14,26 @@ class PrintFormat(Document):
def validate(self):
if self.standard=="Yes" and frappe.session.user != "Administrator":
frappe.msgprint("Standard Print Format cannot be updated.", raise_exception=1)
# old_doc_type is required for clearing item cache
self.old_doc_type = frappe.db.get_value('Print Format',
self.name, 'doc_type')
def on_update(self):
if hasattr(self, 'old_doc_type') and self.old_doc_type:
frappe.clear_cache(doctype=self.old_doc_type)
frappe.clear_cache(doctype=self.old_doc_type)
if self.doc_type:
frappe.clear_cache(doctype=self.doc_type)
self.export_doc()
def export_doc(self):
# export
if self.standard == 'Yes' and (frappe.conf.get('developer_mode') or 0) == 1:
from frappe.modules.export_file import export_to_files
export_to_files(record_list=[['Print Format', self.name]],
record_module=self.module)
export_to_files(record_list=[['Print Format', self.name]],
record_module=self.module)
def on_trash(self):
if self.doc_type:
frappe.clear_cache(doctype=self.doc_type)
@ -47,7 +47,7 @@ def get_args():
<p>Parameters doctype, name and format required</p>
<pre>%s</pre>""" % repr(frappe.form_dict)
}
doc = frappe.get_doc(frappe.form_dict.doctype, frappe.form_dict.name)
for ptype in ("read", "print"):
if not frappe.has_permission(doc.doctype, ptype, doc):
@ -55,7 +55,7 @@ def get_args():
"body": """<h1>Error</h1>
<p>No {ptype} permission</p>""".format(ptype=ptype)
}
return {
"body": get_html(doc),
"css": get_print_style(frappe.form_dict.style),
@ -64,14 +64,14 @@ def get_args():
def get_html(doc, name=None, print_format=None):
from jinja2 import Environment
if isinstance(doc, basestring) and isinstance(name, basestring):
doc = frappe.get_doc(doc, name)
template = Environment().from_string(get_print_format_name(doc.doctype,
template = Environment().from_string(get_print_format_name(doc.doctype,
print_format or frappe.form_dict.format))
doctype = frappe.get_meta(doc.doctype)
meta = frappe.get_meta(doc.doctype)
args = {
"doc": doc,
"meta": meta,
@ -84,9 +84,9 @@ def get_html(doc, name=None, print_format=None):
def get_print_format_name(doctype, format_name):
if format_name==standard_format:
return format_name
# server, find template
path = os.path.join(get_doc_path(frappe.db.get_value("DocType", doctype, "module"),
path = os.path.join(get_doc_path(frappe.db.get_value("DocType", doctype, "module"),
"Print Format", format_name), format_name + ".html")
if os.path.exists(path):
with open(path, "r") as pffile:
@ -101,7 +101,7 @@ def get_print_format_name(doctype, format_name):
def get_print_style(style=None):
if not style:
style = frappe.db.get_default("print_style") or "Standard"
path = os.path.join(get_doc_path("Core", "DocType", "Print Format"), "styles",
path = os.path.join(get_doc_path("Core", "DocType", "Print Format"), "styles",
style.lower() + ".css")
if not os.path.exists(path):
if style!="Standard":
@ -111,4 +111,3 @@ def get_print_style(style=None):
else:
with open(path, 'r') as sfile:
return sfile.read()

View file

@ -9,47 +9,47 @@ from frappe.model.naming import validate_name
@frappe.whitelist()
def rename_doc(doctype, old, new, force=False, merge=False, ignore_permissions=False):
"""
Renames a doc(dt, old) to doc(dt, new) and
Renames a doc(dt, old) to doc(dt, new) and
updates all linked fields of type "Link" or "Select" with "link:"
"""
if not frappe.db.exists(doctype, old):
return
force = cint(force)
merge = cint(merge)
meta = frappe.get_meta(doctype)
# call before_rename
out = frappe.get_doc(doctype, old).run_method("before_rename", old, new, merge) or {}
new = out.get("new") or new
new = (out.get("new") or new) if isinstance(out, dict) else new
new = validate_rename(doctype, new, meta, merge, force, ignore_permissions)
if not merge:
rename_parent_and_child(doctype, old, new, meta)
# update link fields' values
link_fields = get_link_fields(doctype)
update_link_field_values(link_fields, old, new, doctype)
if doctype=='DocType':
rename_doctype(doctype, old, new, force)
update_attachments(doctype, old, new)
if merge:
frappe.delete_doc(doctype, old)
# call after_rename
frappe.get_doc(doctype, new).run_method("after_rename", old, new, merge)
rename_versions(doctype, old, new)
# update restrictions
frappe.db.sql("""update tabDefaultValue set defvalue=%s where parenttype='Restriction'
frappe.db.sql("""update tabDefaultValue set defvalue=%s where parenttype='Restriction'
and defkey=%s and defvalue=%s""", (new, doctype, old))
frappe.clear_cache()
return new
def update_attachments(doctype, old, new):
@ -58,10 +58,10 @@ def update_attachments(doctype, old, new):
where attached_to_name=%s and attached_to_doctype=%s""", (new, old, doctype))
except Exception, e:
if e.args[0]!=1054: # in patch?
raise
raise
def rename_versions(doctype, old, new):
frappe.db.sql("""update tabVersion set docname=%s where ref_doctype=%s and docname=%s""",
frappe.db.sql("""update tabVersion set docname=%s where ref_doctype=%s and docname=%s""",
(new, doctype, old))
def rename_parent_and_child(doctype, old, new, meta):
@ -76,7 +76,7 @@ def validate_rename(doctype, new, meta, merge, force, ignore_permissions):
if merge and not exists:
frappe.msgprint("%s: %s does not exist, select a new target to merge." % (doctype, new), raise_exception=1)
if (not merge) and exists:
frappe.msgprint("%s: %s exists, select a new, new name." % (doctype, new), raise_exception=1)
@ -85,7 +85,7 @@ def validate_rename(doctype, new, meta, merge, force, ignore_permissions):
if not force and not meta.allow_rename:
frappe.msgprint("%s cannot be renamed" % doctype, raise_exception=1)
# validate naming like it's done in doc.py
new = validate_name(doctype, new, merge=merge)
@ -94,28 +94,28 @@ def validate_rename(doctype, new, meta, merge, force, ignore_permissions):
def rename_doctype(doctype, old, new, force=False):
# change options for fieldtype Table
update_parent_of_fieldtype_table(old, new)
# change options where select options are hardcoded i.e. listed
select_fields = get_select_fields(old, new)
update_link_field_values(select_fields, old, new, doctype)
update_select_field_values(old, new)
# change parenttype for fieldtype Table
update_parenttype_values(old, new)
# rename comments
frappe.db.sql("""update tabComment set comment_doctype=%s where comment_doctype=%s""",
(new, old))
def update_child_docs(old, new, meta):
# update "parent"
for child in meta.get_table_fields():
for df in meta.get_table_fields():
frappe.db.sql("update `tab%s` set parent=%s where parent=%s" \
% (child, '%s', '%s'), (new, old))
% (df.options, '%s', '%s'), (new, old))
def update_link_field_values(link_fields, old, new, doctype):
update_list = []
# update values
for field in link_fields:
# if already updated, do not do it again
@ -135,7 +135,7 @@ def update_link_field_values(link_fields, old, new, doctype):
% (field['parent'], field['fieldname'], '%s',
field['fieldname'], '%s'),
(new, old))
def get_link_fields(doctype):
# get link fields from tabDocField
link_fields = frappe.db.sql("""\
@ -148,7 +148,7 @@ def get_link_fields(doctype):
((df.options=%s and df.fieldtype='Link') or
(df.options='link:%s' and df.fieldtype='Select'))""" \
% ('%s', doctype), (doctype,), as_dict=1)
# get link fields from tabCustom Field
custom_link_fields = frappe.db.sql("""\
select dt as parent, fieldname,
@ -160,10 +160,10 @@ def get_link_fields(doctype):
((df.options=%s and df.fieldtype='Link') or
(df.options='link:%s' and df.fieldtype='Select'))""" \
% ('%s', doctype), (doctype,), as_dict=1)
# add custom link fields list to link fields list
link_fields += custom_link_fields
# remove fields whose options have been changed using property setter
property_setter_link_fields = frappe.db.sql("""\
select ps.doc_type as parent, ps.field_name as fieldname,
@ -175,24 +175,24 @@ def get_link_fields(doctype):
ps.field_name is not null and
(ps.value=%s or ps.value='link:%s')""" \
% ('%s', doctype), (doctype,), as_dict=1)
link_fields += property_setter_link_fields
return link_fields
def update_parent_of_fieldtype_table(old, new):
frappe.db.sql("""\
update `tabDocField` set options=%s
where fieldtype='Table' and options=%s""", (new, old))
frappe.db.sql("""\
update `tabCustom Field` set options=%s
where fieldtype='Table' and options=%s""", (new, old))
frappe.db.sql("""\
update `tabProperty Setter` set value=%s
where property='options' and value=%s""", (new, old))
def get_select_fields(old, new):
"""
get select type fields where doctype's name is hardcoded as
@ -210,7 +210,7 @@ def get_select_fields(old, new):
df.options not like "link:%%%%" and
(df.options like "%%%%%s%%%%")""" \
% ('%s', old), (new,), as_dict=1)
# get link fields from tabCustom Field
custom_select_fields = frappe.db.sql("""\
select dt as parent, fieldname,
@ -223,10 +223,10 @@ def get_select_fields(old, new):
df.options not like "link:%%%%" and
(df.options like "%%%%%s%%%%")""" \
% ('%s', old), (new,), as_dict=1)
# add custom link fields list to link fields list
select_fields += custom_select_fields
# remove fields whose options have been changed using property setter
property_setter_select_fields = frappe.db.sql("""\
select ps.doc_type as parent, ps.field_name as fieldname,
@ -240,11 +240,11 @@ def get_select_fields(old, new):
ps.value not like "link:%%%%" and
(ps.value like "%%%%%s%%%%")""" \
% ('%s', old), (new,), as_dict=1)
select_fields += property_setter_select_fields
return select_fields
def update_select_field_values(old, new):
frappe.db.sql("""\
update `tabDocField` set options=replace(options, %s, %s)
@ -269,7 +269,7 @@ def update_select_field_values(old, new):
property='options' and value not like "link%%%%" and
(value like "%%%%\\n%s%%%%" or value like "%%%%%s\\n%%%%")""" % \
('%s', '%s', '%s', old, old), (old, new, new))
def update_parenttype_values(old, new):
child_doctypes = frappe.db.sql("""\
select options, fieldname from `tabDocField`
@ -281,18 +281,18 @@ def update_parenttype_values(old, new):
child_doctypes += custom_child_doctypes
fields = [d['fieldname'] for d in child_doctypes]
property_setter_child_doctypes = frappe.db.sql("""\
select value as options from `tabProperty Setter`
where doc_type=%s and property='options' and
field_name in ("%s")""" % ('%s', '", "'.join(fields)),
(new,))
child_doctypes += property_setter_child_doctypes
child_doctypes = (d['options'] for d in child_doctypes)
for doctype in child_doctypes:
frappe.db.sql("""\
update `tab%s` set parenttype=%s
where parenttype=%s""" % (doctype, '%s', '%s'),
(new, old))
(new, old))

View file

@ -133,11 +133,13 @@ def get_dependencies(doctype):
for df in meta.get_table_fields():
link_fields.extend(frappe.get_meta(df.options).get_link_fields())
options_list = list(set([df.options for df in link_fields] + [doctype]))
options_list = [df.options for df in link_fields] + [doctype]
if hasattr(test_module, "test_dependencies"):
options_list += test_module.test_dependencies
options_list = list(set(options_list))
if hasattr(test_module, "test_ignore"):
for doctype_name in test_module.test_ignore:
if doctype_name in options_list:
@ -165,7 +167,6 @@ def make_test_records_for_doctype(doctype, verbose=0):
elif verbose:
print_mandatory_fields(doctype)
def make_test_objects(doctype, test_records, verbose=None):
records = []

View file

@ -1,5 +1,5 @@
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
# MIT License. See license.txt
from __future__ import unicode_literals
import frappe, json
@ -35,46 +35,44 @@ def validate_link():
"""validate link when updated by user"""
import frappe
import frappe.utils
value, options, fetch = frappe.form_dict.get('value'), frappe.form_dict.get('options'), frappe.form_dict.get('fetch')
# no options, don't validate
if not options or options=='null' or options=='undefined':
frappe.response['message'] = 'Ok'
return
if frappe.db.sql("select name from `tab%s` where name=%s" % (options, '%s'), (value,)):
# get fetch values
if fetch:
frappe.response['fetch_values'] = [frappe.utils.parse_val(c) \
for c in frappe.db.sql("select %s from `tab%s` where name=%s" \
% (fetch, options, '%s'), (value,))[0]]
frappe.response['message'] = 'Ok'
@frappe.whitelist()
def add_comment(doclist):
"""allow any logged user to post a comment"""
doclist = json.loads(doclist)
doclist[0]["__islocal"] = 1
doclistobj = frappe.get_doc(doclist)
doclistobj.ignore_permissions = True
doclistobj.save()
return doclist.as_dict()
return save(doclist)
return doclistobj.as_dict()
@frappe.whitelist()
def get_next(doctype, name, prev):
import frappe.widgets.reportview
prev = int(prev)
field = "`tab%s`.name" % doctype
res = frappe.widgets.reportview.execute(doctype,
fields = [field],
fields = [field],
filters = [[doctype, "name", "<" if prev else ">", name]],
order_by = field + " " + ("desc" if prev else "asc"),
limit_start=0, limit_page_length=1, as_list=True)
@ -95,24 +93,24 @@ def get_linked_docs(doctype, name, metadata_loaded=None):
link["doctype"] = dt
linkmeta = frappe.widgets.form.meta.get_meta(dt)
if not linkmeta.get("issingle"):
fields = [d.fieldname for d in linkmeta.get("fields", {"in_list_view":1,
fields = [d.fieldname for d in linkmeta.get("fields", {"in_list_view":1,
"fieldtype": ["not in", ["Image", "HTML", "Button", "Table"]]})] \
+ ["name", "modified", "docstatus"]
fields = ["`tab{dt}`.`{fn}`".format(dt=dt, fn=sf.strip()) for sf in fields if sf]
if link.get("child_doctype"):
ret = frappe.get_list(doctype=dt, fields=fields,
ret = frappe.get_list(doctype=dt, fields=fields,
filters=[[link.get('child_doctype'), link.get("fieldname"), '=', name]])
else:
ret = frappe.get_list(doctype=dt, fields=fields,
ret = frappe.get_list(doctype=dt, fields=fields,
filters=[[dt, link.get("fieldname"), '=', name]])
if ret:
if ret:
results[dt] = ret
if not dt in metadata_loaded:
frappe.local.response.docs.append(linkmeta)
return results
return results