Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

153 строки
3.9 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes, os
  5. def listfolders(path, only_name=0):
  6. """
  7. Returns the list of folders (with paths) in the given path,
  8. If only_name is set, it returns only the folder names
  9. """
  10. out = []
  11. for each in os.listdir(path):
  12. dirname = each.split(os.path.sep)[-1]
  13. fullpath = os.path.join(path, dirname)
  14. if os.path.isdir(fullpath) and not dirname.startswith('.'):
  15. out.append(only_name and dirname or fullname)
  16. return out
  17. def switch_module(dt, dn, to, frm=None, export=None):
  18. """
  19. Change the module of the given doctype, if export is true, then also export txt and copy
  20. code files from src
  21. """
  22. webnotes.conn.sql("update `tab"+dt+"` set module=%s where name=%s", (to, dn))
  23. if export:
  24. export_doc(dt, dn)
  25. # copy code files
  26. if dt in ('DocType', 'Page', 'Report'):
  27. from_path = os.path.join(get_module_path(frm), scrub(dt), scrub(dn), scrub(dn))
  28. to_path = os.path.join(get_module_path(to), scrub(dt), scrub(dn), scrub(dn))
  29. # make dire if exists
  30. os.system('mkdir -p %s' % os.path.join(get_module_path(to), scrub(dt), scrub(dn)))
  31. for ext in ('py','js','html','css'):
  32. os.system('cp %s %s')
  33. def commonify_doclist(doclist, with_comments=1):
  34. """
  35. Makes a doclist more readable by extracting common properties.
  36. This is used for printing Documents in files
  37. """
  38. from webnotes.utils import get_common_dict, get_diff_dict
  39. def make_common(doclist):
  40. c = {}
  41. if with_comments:
  42. c['##comment'] = 'These values are common in all dictionaries'
  43. for k in common_keys:
  44. c[k] = doclist[0][k]
  45. return c
  46. def strip_common_and_idx(d):
  47. for k in common_keys:
  48. if k in d: del d[k]
  49. if 'idx' in d: del d['idx']
  50. return d
  51. def make_common_dicts(doclist):
  52. common_dict = {} # one per doctype
  53. # make common dicts for all records
  54. for d in doclist:
  55. if not d['doctype'] in common_dict:
  56. d1 = d.copy()
  57. if d1.has_key("name"):
  58. del d1['name']
  59. common_dict[d['doctype']] = d1
  60. else:
  61. common_dict[d['doctype']] = get_common_dict(common_dict[d['doctype']], d)
  62. return common_dict
  63. common_keys = ['owner','docstatus','creation','modified','modified_by']
  64. common_dict = make_common_dicts(doclist)
  65. # make docs
  66. final = []
  67. for d in doclist:
  68. f = strip_common_and_idx(get_diff_dict(common_dict[d['doctype']], d))
  69. f['doctype'] = d['doctype'] # keep doctype!
  70. # strip name for child records (only an auto generated number!)
  71. if f['doctype'] != doclist[0]['doctype'] and f.has_key("name"):
  72. del f['name']
  73. if with_comments:
  74. f['##comment'] = d['doctype'] + ('name' in f and (', ' + f['name']) or '')
  75. final.append(f)
  76. # add commons
  77. commons = []
  78. for d in common_dict.values():
  79. d['name']='__common__'
  80. if with_comments:
  81. d['##comment'] = 'These values are common for all ' + d['doctype']
  82. commons.append(strip_common_and_idx(d))
  83. common_values = make_common(doclist)
  84. return [common_values]+commons+final
  85. def uncommonify_doclist(dl):
  86. """
  87. Expands an commonified doclist
  88. """
  89. # first one has common values
  90. common_values = dl[0]
  91. common_dict = webnotes._dict()
  92. final = []
  93. idx_dict = {}
  94. for d in dl[1:]:
  95. if 'name' in d and d['name']=='__common__':
  96. # common for a doctype -
  97. del d['name']
  98. common_dict[d['doctype']] = d
  99. else:
  100. dt = d['doctype']
  101. if not dt in idx_dict: idx_dict[dt] = 1;
  102. d1 = webnotes._dict(common_values.copy())
  103. # update from common and global
  104. d1.update(common_dict[dt])
  105. d1.update(d)
  106. # idx by sequence
  107. d1['idx'] = idx_dict[dt]
  108. # increment idx
  109. idx_dict[dt] += 1
  110. final.append(d1)
  111. return final
  112. def pprint_doclist(doclist, with_comments = 1):
  113. from json import dumps
  114. return dumps(commonify_doclist(doclist, False), indent=1, sort_keys=True)
  115. def peval_doclist(txt):
  116. from json import loads
  117. try:
  118. return uncommonify_doclist(loads(txt))
  119. except Exception, e:
  120. return uncommonify_doclist(eval(txt))