You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 11 години
преди 13 години
преди 12 години
преди 12 години
преди 13 години
преди 12 години
преди 12 години
преди 13 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. # model __init__.py
  4. from __future__ import unicode_literals
  5. import webnotes
  6. no_value_fields = ['Section Break', 'Column Break', 'HTML', 'Table', 'Button', 'Image']
  7. default_fields = ['doctype','name','owner','creation','modified','modified_by','parent','parentfield','parenttype','idx','docstatus']
  8. def insert(doclist):
  9. if not isinstance(doclist, list):
  10. doclist = [doclist]
  11. for d in doclist:
  12. if isinstance(d, dict):
  13. d["__islocal"] = 1
  14. else:
  15. d.fields["__islocal"] = 1
  16. wrapper = webnotes.bean(doclist)
  17. wrapper.save()
  18. return wrapper
  19. def rename(doctype, old, new, debug=False):
  20. import webnotes.model.rename_doc
  21. webnotes.model.rename_doc.rename_doc(doctype, old, new, debug)
  22. def copytables(srctype, src, srcfield, tartype, tar, tarfield, srcfields, tarfields=[]):
  23. import webnotes.model.doc
  24. if not tarfields:
  25. tarfields = srcfields
  26. l = []
  27. data = webnotes.model.doc.getchildren(src.name, srctype, srcfield)
  28. for d in data:
  29. newrow = webnotes.model.doc.addchild(tar, tarfield, tartype)
  30. newrow.idx = d.idx
  31. for i in range(len(srcfields)):
  32. newrow.fields[tarfields[i]] = d.fields[srcfields[i]]
  33. l.append(newrow)
  34. return l
  35. def db_exists(dt, dn):
  36. import webnotes
  37. return webnotes.conn.exists(dt, dn)
  38. def delete_fields(args_dict, delete=0):
  39. """
  40. Delete a field.
  41. * Deletes record from `tabDocField`
  42. * If not single doctype: Drops column from table
  43. * If single, deletes record from `tabSingles`
  44. args_dict = { dt: [field names] }
  45. """
  46. import webnotes.utils
  47. for dt in args_dict.keys():
  48. fields = args_dict[dt]
  49. if not fields: continue
  50. webnotes.conn.sql("""\
  51. DELETE FROM `tabDocField`
  52. WHERE parent=%s AND fieldname IN (%s)
  53. """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt)
  54. # Delete the data / column only if delete is specified
  55. if not delete: continue
  56. is_single = webnotes.conn.sql("select issingle from tabDocType where name = '%s'" % dt)
  57. is_single = is_single and webnotes.utils.cint(is_single[0][0]) or 0
  58. if is_single:
  59. webnotes.conn.sql("""\
  60. DELETE FROM `tabSingles`
  61. WHERE doctype=%s AND field IN (%s)
  62. """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt)
  63. else:
  64. existing_fields = webnotes.conn.sql("desc `tab%s`" % dt)
  65. existing_fields = existing_fields and [e[0] for e in existing_fields] or []
  66. query = "ALTER TABLE `tab%s` " % dt + \
  67. ", ".join(["DROP COLUMN `%s`" % f for f in fields if f in existing_fields])
  68. webnotes.conn.commit()
  69. webnotes.conn.sql(query)
  70. def rename_field(doctype, old_fieldname, new_fieldname):
  71. """This functions assumes that doctype is already synced"""
  72. doctype_list = webnotes.get_doctype(doctype)
  73. new_field = doctype_list.get_field(new_fieldname)
  74. if not new_field:
  75. print "rename_field: " + (new_fieldname) + " not found in " + doctype
  76. return
  77. if new_field.fieldtype == "Table":
  78. # change parentfield of table mentioned in options
  79. webnotes.conn.sql("""update `tab%s` set parentfield=%s
  80. where parentfield=%s""" % (new_field.options.split("\n")[0], "%s", "%s"),
  81. (new_fieldname, old_fieldname))
  82. elif new_field.fieldtype not in no_value_fields:
  83. if doctype_list[0].issingle:
  84. webnotes.conn.sql("""update `tabSingles` set field=%s
  85. where doctype=%s and field=%s""",
  86. (new_fieldname, doctype, old_fieldname))
  87. else:
  88. webnotes.conn.sql("""update `tab%s` set `%s`=`%s`""" % \
  89. (doctype, new_fieldname, old_fieldname))
  90. webnotes.conn.sql("""update `tabProperty Setter` set field_name = %s
  91. where doc_type=%s and field_name=%s""", (new_fieldname, doctype, old_fieldname))
  92. import json
  93. user_report_cols = webnotes.conn.sql("""select defkey, defvalue from `tabDefaultValue` where
  94. defkey like '_list_settings:%'""")
  95. for key, value in user_report_cols:
  96. new_columns = []
  97. columns_modified = False
  98. for field, field_doctype in json.loads(value):
  99. if field == old_fieldname and field_doctype == doctype:
  100. new_columns.append([field, field_doctype])
  101. columns_modified=True
  102. if columns_modified:
  103. webnotes.conn.sql("""update `tabDefaultValue` set defvalue=%s
  104. where defkey=%s""" % ('%s', '%s'), (json.dumps(new_columns), key))