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.
 
 
 
 
 
 

130 lines
4.0 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. import webnotes
  23. sql = webnotes.conn.sql
  24. # main function
  25. # -------------------------
  26. def archive_doc(doctype, name, restore=0):
  27. archive_record(doctype, name, restore)
  28. tables = sql("select options from tabDocField where parent=%s and fieldtype='Table'", doctype)
  29. for t in tables:
  30. try:
  31. rec_list = sql("select name from `%s%s` where parent=%s" % ((restore and 'arc' or 'tab') ,t[0], '%s'), name)
  32. except Exception,e:
  33. if e.args[0]==1146: # no child table
  34. rec_list = []
  35. else:
  36. raise e
  37. for r in rec_list:
  38. archive_record(t[0], r[0], restore)
  39. # archive individual record
  40. # -------------------------
  41. def archive_record(doctype, name, restore = 0):
  42. src_tab = (restore and 'arc' or 'tab') + doctype
  43. tar_tab = (restore and 'tab' or 'arc') + doctype
  44. # get the record
  45. try:
  46. rec = sql("select * from `%s` where name=%s for update" % (src_tab, '%s'), name, as_dict=1)[0]
  47. except Exception, e:
  48. if e.args[0]==1146:
  49. return # source table does not exist
  50. else:
  51. raise e
  52. # insert the record
  53. insert_record(doctype, tar_tab, name)
  54. # put it field by field (ignore missing columns)
  55. for field in rec.keys():
  56. if rec.get(field):
  57. update_value(src_tab, tar_tab, name, rec, field)
  58. # delete from main
  59. try:
  60. sql("delete from `%s` where name=%s limit 1" % (src_tab, '%s'), name)
  61. except Exception, e:
  62. if e.args[0]==1451:
  63. webnotes.msgprint("Cannot archive %s '%s' as it is referenced in another record. You must delete the referred record first" % (doctype, name))
  64. # delete from target, as it will create a double copy!
  65. sql("delete from `%s` where name=%s limit 1" % (tar_tab, '%s'), name)
  66. # insert the record
  67. # -------------------------
  68. def insert_record_name(tab, name):
  69. sql("insert ignore into `%s` (name) values (%s)" % (tab, '%s'), name)
  70. # insert record
  71. # -------------------------
  72. def insert_record(doctype, tar_tab, name):
  73. try:
  74. insert_record_name(tar_tab, name)
  75. except Exception, e:
  76. if e.args[0]==1146:
  77. # missing table - create it
  78. from webnotes.model.db_schema import updatedb
  79. updatedb(doctype, 1)
  80. # again
  81. insert_record_name(tar_tab, name)
  82. else:
  83. raise e
  84. # update single value
  85. # -------------------------
  86. def update_single_value(tab, field, value, name):
  87. sql("update `%s` set `%s`=%s where name=%s" % (tab, field, '%s', '%s'), (value, name))
  88. # update value
  89. # -------------------------
  90. def update_value(src_tab, tar_tab, name, rec, field):
  91. try:
  92. update_single_value(tar_tab, field, rec[field], name)
  93. except Exception, e:
  94. if e.args[0]==1054:
  95. # column missing.... add it?
  96. ftype = sql("show columns from `%s` like '%s'" % (src_tab, field))[0][1]
  97. webnotes.conn.commit() # causes implict commit
  98. sql("alter table `%s` add column `%s` %s" % (tar_tab, field, ftype))
  99. webnotes.conn.begin()
  100. # again
  101. update_single_value(tar_tab, field, rec[field], name)
  102. else:
  103. raise e