Anand Doshi | a30fbd0 | 2012-03-27 19:31:38 +0530 | [diff] [blame] | 1 | # do not run this patch |
Anand Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 2 | from __future__ import unicode_literals |
Anand Doshi | a30fbd0 | 2012-03-27 19:31:38 +0530 | [diff] [blame] | 3 | def execute(): |
| 4 | import webnotes |
| 5 | import webnotes.modules |
| 6 | forbidden = ['%', "'", '"', '#', '*', '?', '`', '(', ')', '<', '>', '-', |
| 7 | '\\', '/', '.', '&', '!', '@', '$', '^', '+'] |
| 8 | doctype_list = webnotes.conn.sql("SELECT name, module FROM `tabDocType`") |
| 9 | for doctype, module in doctype_list: |
| 10 | docfield_list = webnotes.conn.sql("""\ |
| 11 | SELECT name, label, fieldtype FROM `tabDocField` |
| 12 | WHERE parent = %s AND IFNULL(fieldname, '') = ''""", doctype) |
| 13 | field_type_count = {} |
| 14 | count = 0 |
| 15 | for name, label, fieldtype in docfield_list: |
| 16 | fieldname = None |
| 17 | if label: |
| 18 | temp_label = label |
| 19 | if len(temp_label)==1: |
| 20 | temp_label = fieldtype + temp_label |
| 21 | |
| 22 | fieldname = temp_label.lower().replace(' ', '_') |
| 23 | if "<" in fieldname: |
| 24 | count = field_type_count.setdefault(fieldtype, 0) |
| 25 | fieldname = fieldtype.lower().replace(' ', '_') + str(count) |
| 26 | field_type_count[fieldtype] = count + 1 |
| 27 | elif fieldtype: |
| 28 | count = field_type_count.setdefault(fieldtype, 0) |
| 29 | fieldname = fieldtype.lower().replace(' ', '_') + str(count) |
| 30 | field_type_count[fieldtype] = count + 1 |
| 31 | |
| 32 | if fieldname: |
| 33 | for f in forbidden: fieldname = fieldname.replace(f, '') |
| 34 | fieldname = fieldname.replace('__', '_') |
| 35 | if fieldname.endswith('_'): |
| 36 | fieldname = fieldname[:-1] |
| 37 | if fieldname.startswith('_'): |
| 38 | fieldname = fieldname[1:] |
| 39 | #print fieldname |
| 40 | webnotes.conn.sql("""\ |
| 41 | UPDATE `tabDocField` SET fieldname = %s |
| 42 | WHERE name = %s""", (fieldname, name)) |
| 43 | webnotes.modules.export_doc('DocType', doctype) |