No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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