選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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