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.
 
 
 
 
 
 

123 lines
4.2 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', 'FlexTable', 'Button', 'Image', 'Graph']
  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 get_search_criteria(dt):
  43. import webnotes.model.doc
  44. # load search criteria for reports (all)
  45. dl = []
  46. try: # bc
  47. sc_list = webnotes.conn.sql("select name from `tabSearch Criteria` where doc_type = '%s' or parent_doc_type = '%s' and (disabled!=1 OR disabled IS NULL)" % (dt, dt))
  48. for sc in sc_list:
  49. dl += webnotes.model.doc.get('Search Criteria', sc[0])
  50. except Exception, e:
  51. pass # no search criteria
  52. return dl
  53. def rename(doctype, old, new, debug=1):
  54. import webnotes.model.rename_doc
  55. webnotes.model.rename_doc.rename_doc(doctype, old, new, debug)
  56. def copytables(srctype, src, srcfield, tartype, tar, tarfield, srcfields, tarfields=[]):
  57. import webnotes.model.doc
  58. if not tarfields:
  59. tarfields = srcfields
  60. l = []
  61. data = webnotes.model.doc.getchildren(src.name, srctype, srcfield)
  62. for d in data:
  63. newrow = webnotes.model.doc.addchild(tar, tarfield, tartype)
  64. newrow.idx = d.idx
  65. for i in range(len(srcfields)):
  66. newrow.fields[tarfields[i]] = d.fields[srcfields[i]]
  67. l.append(newrow)
  68. return l
  69. def db_exists(dt, dn):
  70. import webnotes
  71. return webnotes.conn.exists(dt, dn)
  72. def delete_fields(args_dict, delete=0):
  73. """
  74. Delete a field.
  75. * Deletes record from `tabDocField`
  76. * If not single doctype: Drops column from table
  77. * If single, deletes record from `tabSingles`
  78. args_dict = { dt: [field names] }
  79. """
  80. import webnotes.utils
  81. for dt in args_dict.keys():
  82. fields = args_dict[dt]
  83. if not fields: continue
  84. webnotes.conn.sql("""\
  85. DELETE FROM `tabDocField`
  86. WHERE parent=%s AND fieldname IN (%s)
  87. """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt)
  88. # Delete the data / column only if delete is specified
  89. if not delete: continue
  90. is_single = webnotes.conn.sql("select issingle from tabDocType where name = '%s'" % dt)
  91. is_single = is_single and webnotes.utils.cint(is_single[0][0]) or 0
  92. if is_single:
  93. webnotes.conn.sql("""\
  94. DELETE FROM `tabSingles`
  95. WHERE doctype=%s AND field IN (%s)
  96. """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt)
  97. else:
  98. existing_fields = webnotes.conn.sql("desc `tab%s`" % dt)
  99. existing_fields = existing_fields and [e[0] for e in existing_fields] or []
  100. query = "ALTER TABLE `tab%s` " % dt + \
  101. ", ".join(["DROP COLUMN `%s`" % f for f in fields if f in existing_fields])
  102. webnotes.conn.commit()
  103. webnotes.conn.sql(query)