Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

182 рядки
5.4 KiB

  1. """
  2. Server side methods called from DocBrowser
  3. """
  4. import webnotes
  5. from webnotes.utils import cint, cstr
  6. sql = webnotes.conn.sql
  7. def get_menu_items():
  8. """
  9. Returns a list of items to show in `Options` of the Web Notes Toolbar
  10. List contains Pages and Single DocTypes
  11. """
  12. import webnotes.utils
  13. rl = webnotes.user.get_roles() + [webnotes.session['user']]
  14. role_options = ["role = '"+r+"'" for r in rl]
  15. sql = webnotes.conn.sql
  16. menuitems = []
  17. # pages
  18. pages = sql("select distinct parent from `tabPage Role` where docstatus!=2 and (%s)" % (' OR '.join(role_options)))
  19. for p in pages:
  20. tmp = sql("select icon, parent_node, menu_index, show_in_menu from tabPage where name = '%s'" % p[0])
  21. if tmp and tmp[0][3]:
  22. menuitems.append(['Page', p[0] or '', tmp[0][1] or '', tmp[0][0] or '', webnotes.utils.cint(tmp[0][2])])
  23. # singles
  24. tmp = sql("select smallicon, parent_node, menu_index, name from tabDocType where (show_in_menu = 1 and show_in_menu is not null)")
  25. singles = {}
  26. for t in tmp: singles[t[3]] = t
  27. for p in webnotes.user.can_read:
  28. tmp = singles.get(p, None)
  29. if tmp: menuitems.append([p, p, tmp[1] or '', tmp[0] or '', int(tmp[2] or 0)])
  30. return menuitems
  31. # --------------------------------------------------------------
  32. def has_result():
  33. return sql("select name from `tab%s` limit 1" % webnotes.form_dict.get('dt')) and 'Yes' or 'No'
  34. # --------------------------------------------------------------
  35. def is_submittable(dt):
  36. return sql("select name from tabDocPerm where parent=%s and ifnull(submit,0)=1 and docstatus<1 limit 1", dt)
  37. # --------------------------------------------------------------
  38. def can_cancel(dt):
  39. return sql('select name from tabDocPerm where parent="%s" and ifnull(cancel,0)=1 and docstatus<1 and role in ("%s") limit 1' % (dt, '", "'.join(webnotes.user.get_roles())))
  40. # --------------------------------------------------------------
  41. def get_dt_trend(dt):
  42. ret = {}
  43. for r in sql("select datediff(now(),modified), count(*) from `tab%s` where datediff(now(),modified) between 0 and 30 group by date(modified)" % dt):
  44. ret[cint(r[0])] = cint(r[1])
  45. return ret
  46. # --------------------------------------------------------------
  47. def get_columns(out, sf, fl, dt, tag_fields):
  48. if not fl:
  49. fl = sf
  50. # subject
  51. subject = webnotes.conn.get_value('DocType', dt, 'subject')
  52. if subject:
  53. out['subject'] = subject
  54. # get fields from subject
  55. import re
  56. fl = re.findall('\%\( (?P<name> [^)]*) \)s', subject, re.VERBOSE)
  57. if tag_fields:
  58. fl += [t.strip() for t in tag_fields.split(',')]
  59. res = []
  60. for f in tuple(set(fl)):
  61. if f:
  62. res += [[c or '' for c in r] for r in sql("select fieldname, label, fieldtype, options from tabDocField where parent='%s' and fieldname='%s'" % (dt, f))]
  63. return res
  64. # --------------------------------------------------------------
  65. # NOTE: THIS SHOULD BE CACHED IN DOCTYPE CACHE
  66. # --------------------------------------------------------------
  67. def get_dt_details():
  68. """
  69. Returns details called by DocBrowser this includes:
  70. the filters, columns, subject and tag_fields
  71. also if the doctype is of type "submittable"
  72. """
  73. fl = eval(webnotes.form_dict.get('fl'))
  74. dt = webnotes.form_dict.get('dt')
  75. tag_fields, description = webnotes.conn.get_value('DocType', dt, ['tag_fields', 'description'])
  76. submittable = is_submittable(dt) and 1 or 0
  77. out = {
  78. 'submittable':(is_submittable(dt) and 1 or 0),
  79. 'can_cancel':(can_cancel(dt) and 1 or 0)
  80. }
  81. # filters
  82. # -------
  83. sf = sql("select search_fields from tabDocType where name=%s", dt)[0][0] or ''
  84. # get fields from in_filter (if not in search_fields)
  85. if not sf.strip():
  86. res = sql("select fieldname, label, fieldtype, options from tabDocField where parent=%s and `in_filter` = 1 and ifnull(fieldname,'') != ''", dt)
  87. sf = [s[0] for s in res]
  88. else:
  89. sf = [s.strip() for s in sf.split(',')]
  90. res = sql("select fieldname, label, fieldtype, options from tabDocField where parent='%s' and fieldname in (%s)" % (dt, '"'+'","'.join(sf)+'"'))
  91. # select "link" options
  92. res = [[c or '' for c in r] for r in res]
  93. for r in res:
  94. if r[2]=='Select' and r[3] and r[3].startswith('link:'):
  95. tdt = r[3][5:]
  96. ol = sql("select name from `tab%s` where docstatus!=2 order by name asc" % tdt)
  97. r[3] = "\n".join([''] + [o[0] for o in ol])
  98. if not res:
  99. out['filters'] = [['name', 'ID', 'Data', '']]
  100. else:
  101. out['filters'] = [['name', 'ID', 'Data', '']] + res
  102. # columns
  103. # -------
  104. res = get_columns(out, sf, fl, dt, tag_fields)
  105. from webnotes.widgets.tags import check_user_tags
  106. check_user_tags(dt)
  107. out['columns'] = [['name', 'ID', 'Link', dt], ['modified', 'Modified', 'Data', ''], ['_user_tags', 'Tags', 'Data', '']] + res
  108. out['tag_fields'] = tag_fields
  109. out['description'] = description
  110. return out
  111. # --------------------------------------------------------------
  112. def get_trend():
  113. return {'trend': get_dt_trend(webnotes.form_dict.get('dt'))}
  114. #
  115. # delete and archive in docbrowser
  116. #
  117. def delete_items():
  118. il = eval(webnotes.form_dict.get('items'))
  119. from webnotes.model import delete_doc
  120. from webnotes.model.code import get_obj
  121. for d in il:
  122. dt_obj = get_obj(d[0], d[1])
  123. if hasattr(dt_obj, 'on_trash'):
  124. dt_obj.on_trash()
  125. delete_doc(d[0], d[1])
  126. # --------------------------------------------------------------
  127. def archive_items():
  128. il = eval(webnotes.form_dict.get('items'))
  129. from webnotes.utils.archive import archive_doc
  130. for d in il:
  131. archive_doc(d[0], d[1], webnotes.form_dict.get('action')=='Restore' and 1 or 0)