Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

87 linhas
2.5 KiB

  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)