Deprecate Select fields with options as link:

This commit is contained in:
Anand Doshi 2014-05-09 13:35:09 +05:30
parent b34afadd8d
commit 3c160b6554
16 changed files with 50 additions and 70 deletions

View file

@ -48,7 +48,7 @@ cur_frm.fields_dict['dt'].get_query = function(doc, dt, dn) {
cur_frm.cscript.fieldtype = function(doc, dt, dn) {
if(doc.fieldtype == 'Link') cur_frm.fields_dict['options_help'].disp_area.innerHTML = 'Please enter name of the document you want this field to be linked to in <b>Options</b>.<br> Eg.: Customer';
else if(doc.fieldtype == 'Select') cur_frm.fields_dict['options_help'].disp_area.innerHTML = 'Please enter values in <b>Options</b> separated by enter. <br>Eg.: <b>Field:</b> Country <br><b>Options:</b><br>China<br>India<br>United States<br><br><b> OR </b><br>You can also link it to existing Documents.<br>Eg.: <b>link:</b>Customer';
else if(doc.fieldtype == 'Select') cur_frm.fields_dict['options_help'].disp_area.innerHTML = 'Please enter values in <b>Options</b>, with each option on a new line. <br>Eg.: <b>Field:</b> Country <br><b>Options:</b><br>China<br>India<br>United States<br><br><b>';
else cur_frm.fields_dict['options_help'].disp_area.innerHTML = '';
}

View file

@ -31,7 +31,6 @@ class DocType(Document):
validate_permissions(self)
self.make_amendable()
self.check_link_replacement_error()
def change_modified_of_parent(self):
if frappe.flags.in_import:
@ -94,12 +93,6 @@ class DocType(Document):
module.on_doctype_update()
frappe.clear_cache(doctype=self.name)
def check_link_replacement_error(self):
for d in self.get("fields", {"fieldtype":"Select"}):
if (frappe.db.get_value("DocField", d.name, "options") or "").startswith("link:") \
and not d.options.startswith("link:"):
frappe.throw(_("'link:' type Select {0} getting replaced").format(d.label))
def on_trash(self):
frappe.db.sql("delete from `tabCustom Field` where dt = %s", self.name)
frappe.db.sql("delete from `tabCustom Script` where dt = %s", self.name)

View file

@ -1,17 +1,17 @@
{
"autoname": "EVP.#####",
"creation": "2013-02-22 01:27:33.000000",
"creation": "2013-02-22 01:27:33",
"docstatus": 0,
"doctype": "DocType",
"fields": [
{
"fieldname": "person",
"fieldtype": "Select",
"fieldtype": "Link",
"in_list_view": 1,
"label": "Person",
"oldfieldname": "person",
"oldfieldtype": "Select",
"options": "link:User",
"options": "User",
"permlevel": 0,
"print_width": "240px",
"search_index": 1,
@ -20,9 +20,12 @@
],
"idx": 1,
"istable": 1,
"modified": "2013-12-20 19:23:14.000000",
"modified": "2014-05-09 02:12:32.374008",
"modified_by": "Administrator",
"module": "Core",
"name": "Event User",
"owner": "Administrator"
"owner": "Administrator",
"permissions": [],
"sort_field": "modified",
"sort_order": "DESC"
}

View file

@ -3,21 +3,22 @@
"allow_copy": 0,
"allow_rename": 0,
"autoname": "Prompt",
"creation": "2013-01-23 19:54:43.000000",
"creation": "2013-01-23 19:54:43",
"docstatus": 0,
"doctype": "DocType",
"fields": [
{
"allow_on_submit": 0,
"fieldname": "module",
"fieldtype": "Select",
"fieldtype": "Link",
"hidden": 0,
"in_filter": 1,
"in_list_view": 1,
"label": "Module",
"no_copy": 0,
"oldfieldname": "module",
"oldfieldtype": "Select",
"options": "link:Module Def",
"options": "Module Def",
"permlevel": 0,
"print_hide": 0,
"report_hide": 0,
@ -27,10 +28,11 @@
{
"description": "Belongs to",
"fieldname": "doc_type",
"fieldtype": "Select",
"fieldtype": "Link",
"in_filter": 1,
"in_list_view": 1,
"label": "DocType",
"options": "link:DocType",
"options": "DocType",
"permlevel": 0,
"reqd": 0,
"search_index": 0
@ -47,6 +49,7 @@
"fieldtype": "Select",
"hidden": 0,
"in_filter": 1,
"in_list_view": 1,
"label": "Standard",
"no_copy": 1,
"oldfieldname": "standard",
@ -62,6 +65,7 @@
{
"fieldname": "print_format_type",
"fieldtype": "Select",
"in_list_view": 1,
"label": "Print Format Type",
"options": "Client\nServer",
"permlevel": 0
@ -99,7 +103,7 @@
"issingle": 0,
"istable": 0,
"max_attachments": 0,
"modified": "2014-01-20 17:49:00.000000",
"modified": "2014-05-09 02:12:39.540952",
"modified_by": "Administrator",
"module": "Core",
"name": "Print Format",
@ -158,5 +162,7 @@
}
],
"read_only": 0,
"read_only_onload": 0
"read_only_onload": 0,
"sort_field": "modified",
"sort_order": "DESC"
}

View file

@ -108,8 +108,6 @@ def get_template(doctype=None, parent_doctype=None, all_doctypes="No", with_data
if docfield.fieldtype == 'Select':
if not docfield.options:
return ''
elif docfield.options.startswith('link:'):
return 'Valid %s' % docfield.options[5:]
else:
return 'One of: %s' % ', '.join(filter(None, docfield.options.split('\n')))
elif docfield.fieldtype == 'Link':

View file

@ -259,9 +259,6 @@ class BaseDocument(object):
if not doctype:
frappe.throw(_("Options not set for link field {0}").format(df.fieldname))
elif doctype.lower().startswith("link:"):
doctype = doctype[5:]
docname = self.get(df.fieldname)
if docname and not frappe.db.get_value(doctype, docname):
invalid_links.append((df.fieldname, docname, get_msg(df, docname)))

View file

@ -63,8 +63,7 @@ def get_new_doc(doctype, parent_doc = None, parentfield = None):
elif d.fieldtype == "Time":
doc.set(d.fieldname, nowtime())
elif (d.fieldtype == "Select" and d.options and not d.options.startswith("link:")
and d.options != "[Select]"):
elif (d.fieldtype == "Select" and d.options and d.options != "[Select]"):
doc.set(d.fieldname, d.options.split("\n")[0])
return doc

View file

@ -59,11 +59,7 @@ class Meta(Document):
raise
def get_link_fields(self):
tmp = self.get("fields", {"fieldtype":"Link", "options":["!=", "[Select]"]})
for df in self.get("fields", {"fieldtype":"Select", "options": "^link:"}):
tmp.append(frappe._dict({"fieldname":df.fieldname, "label":df.label,
"fieldtype":"Link", "options": df.options[5:]}))
return tmp
return self.get("fields", {"fieldtype": "Link", "options":["!=", "[Select]"]})
def get_table_fields(self):
if not hasattr(self, "_table_fields"):

View file

@ -11,7 +11,7 @@ from frappe.model.naming import validate_name
def rename_doc(doctype, old, new, force=False, merge=False, ignore_permissions=False):
"""
Renames a doc(dt, old) to doc(dt, new) and
updates all linked fields of type "Link" or "Select" with "link:"
updates all linked fields of type "Link"
"""
if not frappe.db.exists(doctype, old):
return
@ -138,9 +138,7 @@ def get_link_fields(doctype):
where dt.name = df.parent) as issingle
from tabDocField df
where
df.parent not like "old%%%%" and df.parent != '0' and
((df.options=%s and df.fieldtype='Link') or
(df.options='link:%s' and df.fieldtype='Select'))""" \
df.options=%s and df.fieldtype='Link'""" \
% ('%s', doctype), (doctype,), as_dict=1)
# get link fields from tabCustom Field
@ -150,9 +148,7 @@ def get_link_fields(doctype):
where dt.name = df.dt) as issingle
from `tabCustom Field` df
where
df.dt not like "old%%%%" and df.dt != '0' and
((df.options=%s and df.fieldtype='Link') or
(df.options='link:%s' and df.fieldtype='Select'))""" \
df.options=%s and df.fieldtype='Link'""" \
% ('%s', doctype), (doctype,), as_dict=1)
# add custom link fields list to link fields list
@ -167,7 +163,7 @@ def get_link_fields(doctype):
where
ps.property_type='options' and
ps.field_name is not null and
(ps.value=%s or ps.value='link:%s')""" \
ps.value=%s""" \
% ('%s', doctype), (doctype,), as_dict=1)
link_fields += property_setter_link_fields
@ -199,10 +195,8 @@ def get_select_fields(old, new):
where dt.name = df.parent) as issingle
from tabDocField df
where
df.parent not like "old%%%%" and df.parent != '0' and
df.parent != %s and df.fieldtype = 'Select' and
df.options not like "link:%%%%" and
(df.options like "%%%%%s%%%%")""" \
df.options like "%%%%%s%%%%" """ \
% ('%s', old), (new,), as_dict=1)
# get link fields from tabCustom Field
@ -212,10 +206,8 @@ def get_select_fields(old, new):
where dt.name = df.dt) as issingle
from `tabCustom Field` df
where
df.dt not like "old%%%%" and df.dt != '0' and
df.dt != %s and df.fieldtype = 'Select' and
df.options not like "link:%%%%" and
(df.options like "%%%%%s%%%%")""" \
df.options like "%%%%%s%%%%" """ \
% ('%s', old), (new,), as_dict=1)
# add custom link fields list to link fields list
@ -231,8 +223,7 @@ def get_select_fields(old, new):
ps.doc_type != %s and
ps.property_type='options' and
ps.field_name is not null and
ps.value not like "link:%%%%" and
(ps.value like "%%%%%s%%%%")""" \
ps.value like "%%%%%s%%%%" """ \
% ('%s', old), (new,), as_dict=1)
select_fields += property_setter_select_fields
@ -243,16 +234,14 @@ def update_select_field_values(old, new):
frappe.db.sql("""\
update `tabDocField` set options=replace(options, %s, %s)
where
parent != %s and parent not like "old%%%%" and
fieldtype = 'Select' and options not like "link:%%%%" and
parent != %s and fieldtype = 'Select' and
(options like "%%%%\\n%s%%%%" or options like "%%%%%s\\n%%%%")""" % \
('%s', '%s', '%s', old, old), (old, new, new))
frappe.db.sql("""\
update `tabCustom Field` set options=replace(options, %s, %s)
where
dt != %s and dt not like "old%%%%" and
fieldtype = 'Select' and options not like "link:%%%%" and
dt != %s and fieldtype = 'Select' and
(options like "%%%%\\n%s%%%%" or options like "%%%%%s\\n%%%%")""" % \
('%s', '%s', '%s', old, old), (old, new, new))
@ -260,7 +249,7 @@ def update_select_field_values(old, new):
update `tabProperty Setter` set value=replace(value, %s, %s)
where
doc_type != %s and field_name is not null and
property='options' and value not like "link%%%%" and
property='options' and
(value like "%%%%\\n%s%%%%" or value like "%%%%%s\\n%%%%")""" % \
('%s', '%s', '%s', old, old), (old, new, new))

View file

@ -26,3 +26,4 @@ frappe.patches.4_0.update_datetime
frappe.patches.4_0.deprecate_control_panel
frappe.patches.4_0.file_manager_hooks
execute:frappe.get_doc("User", "Guest").save()
frappe.patches.4_0.deprecate_link_selects

View file

@ -0,0 +1,13 @@
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
from __future__ import unicode_literals
import frappe
def execute():
for name in frappe.db.sql_list("""select name from `tabCustom Field`
where fieldtype="Select" and options like "link:%" """):
custom_field = frappe.get_doc("Custom Field", name)
custom_field.fieldtype = "Link"
custom_field.options = custom_field.options[5:]
custom_field.save()

View file

@ -66,7 +66,7 @@ $.extend(frappe.model, {
doc[f.fieldname] = v;
updated.push(f.fieldname);
} else if(f.fieldtype == "Select" && f.options
&& f.options.substr(0, 5)!="link:" && f.options!="[Select]") {
&& f.options!="[Select]") {
doc[f.fieldname] = f.options.split("\n")[0];
}
}

View file

@ -114,8 +114,6 @@ def make_test_records(doctype, verbose=0):
frappe.connect()
for options in get_dependencies(doctype):
if options.startswith("link:"):
options = options[5:]
if options == "[Select]":
continue

View file

@ -196,7 +196,6 @@ def get_messages_from_doctype(name):
messages.extend([d.label, d.description])
if d.fieldtype=='Select' and d.options \
and not d.options.startswith("link:") \
and not d.options.startswith("attach_files:"):
options = d.options.split('\n')
if not "icon" in options[0]:

View file

@ -105,11 +105,7 @@ def check_record(d):
frappe.msgprint(_("{0} is required").format(docfield.label), raise_exception=1)
if docfield.fieldtype=='Select' and val and docfield.options:
if docfield.options.startswith('link:'):
link_doctype = docfield.options.split(':')[1]
if not frappe.db.exists(link_doctype, val):
frappe.throw(_("{0} {1} must be a valid {2}").format(_(docfield.lable), val, _(link_doctype)))
elif docfield.options == "attach_files:":
if docfield.options == "attach_files:":
pass
elif val not in docfield.options.split('\n'):

View file

@ -29,7 +29,6 @@ class FormMeta(Meta):
self.load_assets()
def load_assets(self):
self.expand_selects()
self.add_search_fields()
if not self.istable:
@ -88,13 +87,6 @@ class FormMeta(Meta):
content = frappe.get_jenv().from_string(content).render()
return content
def expand_selects(self):
for df in self.get("fields", {"fieldtype": "Select"}):
if df.options and df.options.startswith("link:"):
df.link_doctype = df.options.split("\n")[0][5:]
df.options = '\n'.join([''] + [o.name for o in frappe.db.sql("""select
name from `tab%s` where docstatus<2 order by name asc""" % df.link_doctype, as_dict=1)])
def add_search_fields(self):
"""add search fields found in the doctypes indicated by link fields' options"""
for df in self.get("fields", {"fieldtype": "Link", "options":["!=", "[Select]"]}):