25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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