您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

111 行
3.8 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. # model __init__.py
  23. from __future__ import unicode_literals
  24. import webnotes
  25. no_value_fields = ['Section Break', 'Column Break', 'HTML', 'Table', 'Button', 'Image']
  26. default_fields = ['doctype','name','owner','creation','modified','modified_by','parent','parentfield','parenttype','idx','docstatus']
  27. def insert(doclist):
  28. if not isinstance(doclist, list):
  29. doclist = [doclist]
  30. for d in doclist:
  31. if isinstance(d, dict):
  32. d["__islocal"] = 1
  33. else:
  34. d.fields["__islocal"] = 1
  35. wrapper = webnotes.bean(doclist)
  36. wrapper.save()
  37. return wrapper
  38. @webnotes.whitelist()
  39. def delete_doc(doctype=None, name=None, doclist = None, force=0):
  40. import webnotes.model.utils
  41. return webnotes.model.utils.delete_doc(doctype, name, doclist, force)
  42. def rename(doctype, old, new, debug=False):
  43. import webnotes.model.rename_doc
  44. webnotes.model.rename_doc.rename_doc(doctype, old, new, debug)
  45. def copytables(srctype, src, srcfield, tartype, tar, tarfield, srcfields, tarfields=[]):
  46. import webnotes.model.doc
  47. if not tarfields:
  48. tarfields = srcfields
  49. l = []
  50. data = webnotes.model.doc.getchildren(src.name, srctype, srcfield)
  51. for d in data:
  52. newrow = webnotes.model.doc.addchild(tar, tarfield, tartype)
  53. newrow.idx = d.idx
  54. for i in range(len(srcfields)):
  55. newrow.fields[tarfields[i]] = d.fields[srcfields[i]]
  56. l.append(newrow)
  57. return l
  58. def db_exists(dt, dn):
  59. import webnotes
  60. return webnotes.conn.exists(dt, dn)
  61. def delete_fields(args_dict, delete=0):
  62. """
  63. Delete a field.
  64. * Deletes record from `tabDocField`
  65. * If not single doctype: Drops column from table
  66. * If single, deletes record from `tabSingles`
  67. args_dict = { dt: [field names] }
  68. """
  69. import webnotes.utils
  70. for dt in args_dict.keys():
  71. fields = args_dict[dt]
  72. if not fields: continue
  73. webnotes.conn.sql("""\
  74. DELETE FROM `tabDocField`
  75. WHERE parent=%s AND fieldname IN (%s)
  76. """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt)
  77. # Delete the data / column only if delete is specified
  78. if not delete: continue
  79. is_single = webnotes.conn.sql("select issingle from tabDocType where name = '%s'" % dt)
  80. is_single = is_single and webnotes.utils.cint(is_single[0][0]) or 0
  81. if is_single:
  82. webnotes.conn.sql("""\
  83. DELETE FROM `tabSingles`
  84. WHERE doctype=%s AND field IN (%s)
  85. """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt)
  86. else:
  87. existing_fields = webnotes.conn.sql("desc `tab%s`" % dt)
  88. existing_fields = existing_fields and [e[0] for e in existing_fields] or []
  89. query = "ALTER TABLE `tab%s` " % dt + \
  90. ", ".join(["DROP COLUMN `%s`" % f for f in fields if f in existing_fields])
  91. webnotes.conn.commit()
  92. webnotes.conn.sql(query)