Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

69 linhas
2.5 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. """docfield utililtes"""
  23. import webnotes
  24. def rename(doctype, fieldname, newname):
  25. """rename docfield"""
  26. df = webnotes.conn.sql("""select * from tabDocField where parent=%s and fieldname=%s""",
  27. (doctype, fieldname), as_dict=1)
  28. if not df:
  29. return
  30. df = df[0]
  31. if webnotes.conn.get_value('DocType', doctype, 'issingle'):
  32. update_single(df, newname)
  33. else:
  34. update_table(df, newname)
  35. update_parent_field(df, newname)
  36. def update_single(f, new):
  37. """update in tabSingles"""
  38. webnotes.conn.begin()
  39. webnotes.conn.sql("""update tabSingles set field=%s where doctype=%s and field=%s""",
  40. (new, f['parent'], f['fieldname']))
  41. webnotes.conn.commit()
  42. def update_table(f, new):
  43. """update table"""
  44. query = get_change_column_query(f, new)
  45. if query:
  46. webnotes.conn.sql(query)
  47. def update_parent_field(f, new):
  48. """update 'parentfield' in tables"""
  49. if f['fieldtype']=='Table':
  50. webnotes.conn.begin()
  51. webnotes.conn.sql("""update `tab%s` set parentfield=%s where parentfield=%s""" \
  52. % (f['options'], '%s', '%s'), (new, f['fieldname']))
  53. webnotes.conn.commit()
  54. def get_change_column_query(f, new):
  55. """generate change fieldname query"""
  56. desc = webnotes.conn.sql("desc `tab%s`" % f['parent'])
  57. for d in desc:
  58. if d[0]== f['fieldname']:
  59. return 'alter table `tab%s` change `%s` `%s` %s' % \
  60. (f['parent'], f['fieldname'], new, d[1])