25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

69 lines
2.1 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. # model __init__.py
  4. from __future__ import unicode_literals
  5. import frappe
  6. import json
  7. no_value_fields = ('Section Break', 'Column Break', 'HTML', 'Table', 'Button', 'Image',
  8. 'Fold', 'Heading')
  9. display_fieldtypes = ('Section Break', 'Column Break', 'HTML', 'Button', 'Image', 'Fold', 'Heading')
  10. default_fields = ('doctype','name','owner','creation','modified','modified_by',
  11. 'parent','parentfield','parenttype','idx','docstatus')
  12. optional_fields = ("_user_tags", "_comments", "_assign", "_liked_by", "_seen")
  13. def copytables(srctype, src, srcfield, tartype, tar, tarfield, srcfields, tarfields=[]):
  14. if not tarfields:
  15. tarfields = srcfields
  16. l = []
  17. data = src.get(srcfield)
  18. for d in data:
  19. newrow = tar.append(tarfield)
  20. newrow.idx = d.idx
  21. for i in range(len(srcfields)):
  22. newrow.set(tarfields[i], d.get(srcfields[i]))
  23. l.append(newrow)
  24. return l
  25. def db_exists(dt, dn):
  26. return frappe.db.exists(dt, dn)
  27. def delete_fields(args_dict, delete=0):
  28. """
  29. Delete a field.
  30. * Deletes record from `tabDocField`
  31. * If not single doctype: Drops column from table
  32. * If single, deletes record from `tabSingles`
  33. args_dict = { dt: [field names] }
  34. """
  35. import frappe.utils
  36. for dt in args_dict.keys():
  37. fields = args_dict[dt]
  38. if not fields: continue
  39. frappe.db.sql("""\
  40. DELETE FROM `tabDocField`
  41. WHERE parent=%s AND fieldname IN (%s)
  42. """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt)
  43. # Delete the data / column only if delete is specified
  44. if not delete: continue
  45. if frappe.db.get_value("DocType", dt, "issingle"):
  46. frappe.db.sql("""\
  47. DELETE FROM `tabSingles`
  48. WHERE doctype=%s AND field IN (%s)
  49. """ % ('%s', ", ".join(['"' + f + '"' for f in fields])), dt)
  50. else:
  51. existing_fields = frappe.db.sql("desc `tab%s`" % dt)
  52. existing_fields = existing_fields and [e[0] for e in existing_fields] or []
  53. query = "ALTER TABLE `tab%s` " % dt + \
  54. ", ".join(["DROP COLUMN `%s`" % f for f in fields if f in existing_fields])
  55. frappe.db.commit()
  56. frappe.db.sql(query)