You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

165 line
5.3 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. """
  23. Server side methods called from DocBrowser
  24. Needs to be refactored
  25. """
  26. import webnotes
  27. from webnotes.utils import cint, cstr
  28. sql = webnotes.conn.sql
  29. @webnotes.whitelist()
  30. def has_result():
  31. """return Yes if the given dt has any records"""
  32. return sql("select name from `tab%s` limit 1" % \
  33. webnotes.form_dict.get('dt')) and 'Yes' or 'No'
  34. def is_submittable(dt):
  35. return sql("select name from tabDocPerm where parent=%s and ifnull(submit,0)=1 and docstatus<1 limit 1", dt)
  36. def can_cancel(dt):
  37. 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())))
  38. def get_dt_trend(dt):
  39. ret = {}
  40. 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):
  41. ret[cint(r[0])] = cint(r[1])
  42. return ret
  43. def get_columns(out, sf, fl, dt, tag_fields):
  44. if not fl:
  45. fl = sf
  46. # subject
  47. subject = webnotes.conn.get_value('DocType', dt, 'subject')
  48. if subject:
  49. out['subject'] = subject
  50. # get fields from subject
  51. import re
  52. fl = re.findall('\%\( (?P<name> [^)]*) \)s', subject, re.VERBOSE)
  53. if tag_fields:
  54. fl += [t.strip() for t in tag_fields.split(',')]
  55. res = []
  56. for f in tuple(set(fl)):
  57. if f:
  58. 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))]
  59. return res
  60. # --------------------------------------------------------------
  61. # NOTE: THIS SHOULD BE CACHED IN DOCTYPE CACHE
  62. # --------------------------------------------------------------
  63. @webnotes.whitelist()
  64. def get_dt_details():
  65. """
  66. Returns details called by DocBrowser this includes:
  67. the filters, columns, subject and tag_fields
  68. also if the doctype is of type "submittable"
  69. """
  70. fl = eval(webnotes.form_dict.get('fl'))
  71. dt = webnotes.form_dict.get('dt')
  72. tag_fields, description = webnotes.conn.get_value('DocType', dt, ['tag_fields', 'description'])
  73. submittable = is_submittable(dt) and 1 or 0
  74. out = {
  75. 'submittable':(is_submittable(dt) and 1 or 0),
  76. 'can_cancel':(can_cancel(dt) and 1 or 0)
  77. }
  78. # filters
  79. # -------
  80. sf = sql("select search_fields from tabDocType where name=%s", dt)[0][0] or ''
  81. # get fields from in_filter (if not in search_fields)
  82. if not sf.strip():
  83. res = sql("select fieldname, label, fieldtype, options from tabDocField where parent=%s and `in_filter` = 1 and ifnull(fieldname,'') != ''", dt)
  84. sf = [s[0] for s in res]
  85. else:
  86. sf = [s.strip() for s in sf.split(',')]
  87. res = sql("select fieldname, label, fieldtype, options from tabDocField where parent='%s' and fieldname in (%s)" % (dt, '"'+'","'.join(sf)+'"'))
  88. # select "link" options
  89. res = [[c or '' for c in r] for r in res]
  90. for r in res:
  91. if r[2]=='Select' and r[3] and r[3].startswith('link:'):
  92. tdt = r[3][5:]
  93. ol = sql("select name from `tab%s` where docstatus!=2 order by name asc" % tdt)
  94. r[3] = "\n".join([''] + [o[0] for o in ol])
  95. if not res:
  96. out['filters'] = [['name', 'ID', 'Data', '']]
  97. else:
  98. out['filters'] = [['name', 'ID', 'Data', '']] + res
  99. # columns
  100. # -------
  101. res = get_columns(out, sf, fl, dt, tag_fields)
  102. from webnotes.widgets.tags import check_user_tags
  103. check_user_tags(dt)
  104. out['columns'] = [['name', 'ID', 'Link', dt], ['modified', 'Modified', 'Data', ''], ['_user_tags', 'Tags', 'Data', '']] + res
  105. out['tag_fields'] = tag_fields
  106. out['description'] = description
  107. return out
  108. @webnotes.whitelist()
  109. def get_trend():
  110. return {'trend': get_dt_trend(webnotes.form_dict.get('dt'))}
  111. @webnotes.whitelist()
  112. def delete_items():
  113. """delete selected items"""
  114. il = eval(webnotes.form_dict.get('items'))
  115. from webnotes.model import delete_doc
  116. from webnotes.model.code import get_obj
  117. for d in il:
  118. dt_obj = get_obj(d[0], d[1])
  119. if hasattr(dt_obj, 'on_trash'):
  120. dt_obj.on_trash()
  121. delete_doc(d[0], d[1])
  122. @webnotes.whitelist()
  123. def archive_items():
  124. """archinve selected items"""
  125. il = eval(webnotes.form_dict.get('items'))
  126. from webnotes.utils.archive import archive_doc
  127. for d in il:
  128. archive_doc(d[0], d[1], webnotes.form_dict.get('action')=='Restore' and 1 or 0)