Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

222 řádky
7.1 KiB

  1. # model __init__.py
  2. import webnotes
  3. no_value_fields = ['Section Break', 'Column Break', 'HTML', 'Table', 'FlexTable', 'Button', 'Image', 'Graph']
  4. default_fields = ['doctype','name','owner','creation','modified','modified_by','parent','parentfield','parenttype','idx','docstatus']
  5. #=================================================================================
  6. def check_if_doc_is_linked(dt, dn):
  7. """
  8. Raises excption if the given doc(dt, dn) is linked in another record.
  9. """
  10. sql = webnotes.conn.sql
  11. ll = get_link_fields(dt)
  12. for l in ll:
  13. link_dt, link_field = l
  14. issingle = sql("select issingle from tabDocType where name = '%s'" % link_dt)
  15. # no such doctype (?)
  16. if not issingle: continue
  17. if issingle[0][0]:
  18. item = sql("select doctype from `tabSingles` where field='%s' and value = '%s' and doctype = '%s' " % (link_field, dn, l[0]))
  19. if item:
  20. webnotes.msgprint("Cannot delete %s <b>%s</b> because it is linked in <b>%s</b>" % (dt, dn, item[0][0]), raise_exception=1)
  21. else:
  22. item = None
  23. try:
  24. item = sql("select name from `tab%s` where `%s`='%s' and docstatus!=2 limit 1" % (link_dt, link_field, dn))
  25. except Exception, e:
  26. if e.args[0]==1146: pass
  27. else: raise e
  28. if item:
  29. webnotes.msgprint("Cannot delete %s <b>%s</b> because it is linked in %s <b>%s</b>" % (dt, dn, link_dt, item[0][0]), raise_exception=1)
  30. def delete_doc(doctype=None, name=None, doclist = None, force=0):
  31. """
  32. Deletes a doc(dt, dn) and validates if it is not submitted and not linked in a live record
  33. """
  34. import webnotes.model.meta
  35. sql = webnotes.conn.sql
  36. # get from form
  37. if not doctype:
  38. doctype = webnotes.form_dict.get('dt')
  39. name = webnotes.form_dict.get('dn')
  40. if not doctype:
  41. webnotes.msgprint('Nothing to delete!', raise_exception =1)
  42. # already deleted..?
  43. if not webnotes.conn.exists(doctype, name):
  44. return
  45. tablefields = webnotes.model.meta.get_table_fields(doctype)
  46. # check if submitted
  47. d = webnotes.conn.sql("select docstatus from `tab%s` where name=%s" % (doctype, '%s'), name)
  48. if d and int(d[0][0]) == 1:
  49. webnotes.msgprint("Submitted Record '%s' '%s' cannot be deleted" % (doctype, name))
  50. raise Exception
  51. # call on_trash if required
  52. from webnotes.model.code import get_obj
  53. if doclist:
  54. obj = get_obj(doclist=doclist)
  55. else:
  56. obj = get_obj(doctype, name)
  57. if hasattr(obj,'on_trash'):
  58. obj.on_trash()
  59. # check if links exist
  60. if not force:
  61. check_if_doc_is_linked(doctype, name)
  62. # remove tags
  63. from webnotes.widgets.tags import clear_tags
  64. clear_tags(doctype, name)
  65. try:
  66. webnotes.conn.sql("delete from `tab%s` where name='%s' limit 1" % (doctype, name))
  67. for t in tablefields:
  68. webnotes.conn.sql("delete from `tab%s` where parent = %s" % (t[0], '%s'), name)
  69. except Exception, e:
  70. if e.args[0]==1451:
  71. webnotes.msgprint("Cannot delete %s '%s' as it is referenced in another record. You must delete the referred record first" % (doctype, name))
  72. raise e
  73. return 'okay'
  74. #=================================================================================
  75. # new feature added 9-Jun-10 to allow doctypes to have labels
  76. def get_dt_labels():
  77. d = {}
  78. try:
  79. res = webnotes.conn.sql("select name, dt_label from `tabDocType Label`")
  80. except:
  81. return {}
  82. for r in res:
  83. d[r[0]] = r[1]
  84. return d
  85. #=================================================================================
  86. def get_search_criteria(dt):
  87. import webnotes.model.doc
  88. # load search criteria for reports (all)
  89. dl = []
  90. try: # bc
  91. 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))
  92. for sc in sc_list:
  93. dl += webnotes.model.doc.get('Search Criteria', sc[0])
  94. except Exception, e:
  95. pass # no search criteria
  96. return dl
  97. # Rename Doc
  98. #=================================================================================
  99. def get_link_fields(dt):
  100. """
  101. Returns linked fields for dt as a tuple of (linked_doctype, linked_field)
  102. """
  103. return webnotes.conn.sql("select parent, fieldname from tabDocField where parent not like 'old%%' and ((options = '%s' and fieldtype='Link') or (options = 'link:%s' and fieldtype='Select'))" % (dt, dt))
  104. def rename(dt, old, new, is_doctype = 0):
  105. """
  106. Renames a doc(dt, old) to doc(dt, new) and updates all linked fields of type "Link" or "Select" with "link:"
  107. """
  108. sql = webnotes.conn.sql
  109. # rename doc
  110. sql("update `tab%s` set name='%s' where name='%s'" % (dt, new, old))
  111. # get child docs
  112. ct = sql("select options from tabDocField where parent = '%s' and fieldtype='Table'" % dt)
  113. for c in ct:
  114. sql("update `tab%s` set parent='%s' where parent='%s'" % (c[0], new, old))
  115. # get links (link / select)
  116. ll = get_link_fields(dt)
  117. for l in ll:
  118. is_single = sql("select issingle from tabDocType where name = '%s'" % l[0])
  119. is_single = is_single and webnotes.utils.cint(is_single[0][0]) or 0
  120. if is_single:
  121. sql("update `tabSingles` set value='%s' where field='%s' and value = '%s' and doctype = '%s' " % (new, l[1], old, l[0]))
  122. else:
  123. sql("update `tab%s` set `%s`='%s' where `%s`='%s'" % (l[0], l[1], new, l[1], old))
  124. # doctype
  125. if is_doctype:
  126. sql("RENAME TABLE `tab%s` TO `tab%s`" % (old, new))
  127. # get child docs (update parenttype)
  128. ct = sql("select options from tabDocField where parent = '%s' and fieldtype='Table'" % new)
  129. for c in ct:
  130. sql("update `tab%s` set parenttype='%s' where parenttype='%s'" % (c[0], new, old))
  131. #=================================================================================
  132. def clear_recycle_bin():
  133. """
  134. Clears temporary records that have been deleted
  135. """
  136. sql = webnotes.conn.sql
  137. tl = sql('show tables')
  138. total_deleted = 0
  139. for t in tl:
  140. fl = [i[0] for i in sql('desc `%s`' % t[0])]
  141. if 'name' in fl:
  142. total_deleted += sql("select count(*) from `%s` where name like '__overwritten:%%'" % t[0])[0][0]
  143. sql("delete from `%s` where name like '__overwritten:%%'" % t[0])
  144. if 'parent' in fl:
  145. total_deleted += sql("select count(*) from `%s` where parent like '__oldparent:%%'" % t[0])[0][0]
  146. sql("delete from `%s` where parent like '__oldparent:%%'" % t[0])
  147. total_deleted += sql("select count(*) from `%s` where parent like 'oldparent:%%'" % t[0])[0][0]
  148. sql("delete from `%s` where parent like 'oldparent:%%'" % t[0])
  149. total_deleted += sql("select count(*) from `%s` where parent like 'old_parent:%%'" % t[0])[0][0]
  150. sql("delete from `%s` where parent like 'old_parent:%%'" % t[0])
  151. webnotes.msgprint("%s records deleted" % str(int(total_deleted)))
  152. # Make Table Copy
  153. #=================================================================================
  154. def copytables(srctype, src, srcfield, tartype, tar, tarfield, srcfields, tarfields=[]):
  155. import webnotes.model.doc
  156. if not tarfields:
  157. tarfields = srcfields
  158. l = []
  159. data = webnotes.model.doc.getchildren(src.name, srctype, srcfield)
  160. for d in data:
  161. newrow = webnotes.model.doc.addchild(tar, tarfield, tartype, local = 1)
  162. newrow.idx = d.idx
  163. for i in range(len(srcfields)):
  164. newrow.fields[tarfields[i]] = d.fields[srcfields[i]]
  165. l.append(newrow)
  166. return l
  167. # DB Exists
  168. #=================================================================================
  169. def db_exists(dt, dn):
  170. import webnotes
  171. return webnotes.conn.exists(dt, dn)