No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

332 líneas
9.4 KiB

  1. """
  2. Server side handler for "Form" events
  3. """
  4. import webnotes
  5. import webnotes.model.doc
  6. import webnotes.model.meta
  7. from webnotes.model.triggers import fire_event
  8. def getdoc():
  9. """
  10. Loads a doclist for a given document. This method is called directly from the client.
  11. Requries "doctype", "docname" as form variables. If "from_archive" is set, it will get from archive.
  12. Will also call the "onload" method on the document.
  13. """
  14. import webnotes
  15. from webnotes.utils import cint
  16. form = webnotes.form_dict
  17. doctype, docname = form.get('doctype'), form.get('name')
  18. prefix = cint(form.get('from_archive')) and 'arc' or 'tab'
  19. if not (doctype and docname):
  20. raise Exception, 'doctype and name required!'
  21. doclist = []
  22. # single
  23. doclist = load_single_doc(doctype, docname, (form.get('user') or webnotes.session['user']), prefix)
  24. # load doctype along with the doc
  25. if form.get('getdoctype'):
  26. import webnotes.model.doctype
  27. doclist += webnotes.model.doctype.get(doctype)
  28. # tag as archived
  29. if prefix == 'arc':
  30. doclist[0].__archived=1
  31. webnotes.response['docs'] = doclist
  32. #===========================================================================================
  33. def get_comments(doctype=None, docname=None, limit=5):
  34. nc, cl = 0, []
  35. if not doctype:
  36. doctype, docname, limit = webnotes.form_dict.get('dt'), webnotes.form_dict.get('dn'), webnotes.form_dict.get('limit')
  37. try:
  38. nc = int(webnotes.conn.sql("select count(*) from `tabComment Widget Record` where comment_doctype=%s and comment_docname=%s", (doctype, docname))[0][0])
  39. if nc:
  40. cl = webnotes.conn.sql("select comment, ifnull(comment_by_fullname, comment_by) AS 'comment_by_fullname', creation from `tabComment Widget Record` where comment_doctype=%s and comment_docname=%s order by creation desc limit %s" % ('%s','%s',limit), (doctype, docname), as_dict=1)
  41. except Exception, e:
  42. if e.args[0]==1146:
  43. # no table
  44. make_comment_table()
  45. else:
  46. raise e
  47. webnotes.response['n_comments'], webnotes.response['comment_list'] = nc, cl
  48. #
  49. # make comment table
  50. #
  51. def make_comment_table():
  52. "Make table for comments - if missing via import module"
  53. webnotes.conn.commit()
  54. from webnotes.modules import reload_doc
  55. reload_doc('core', 'doctype', 'comment_widget_record')
  56. webnotes.conn.begin()
  57. #===========================================================================================
  58. def add_comment():
  59. import time
  60. args = webnotes.form_dict
  61. if args.get('comment'):
  62. from webnotes.model.doc import Document
  63. from webnotes.utils import nowdate
  64. cmt = Document('Comment Widget Record')
  65. for arg in ['comment', 'comment_by', 'comment_by_fullname', 'comment_doctype', 'comment_docname']:
  66. cmt.fields[arg] = args[arg]
  67. cmt.comment_date = nowdate()
  68. cmt.comment_time = time.strftime('%H:%M')
  69. cmt.save(1)
  70. #===========================================================================================
  71. def remove_comment():
  72. args = webnotes.form_dict
  73. webnotes.conn.sql("delete from `tabComment Widget Record` where name=%s",args.get('id'))
  74. try:
  75. get_obj('Feed Control').upate_comment_in_feed(args['dt'], args['dn'])
  76. except: pass
  77. #===========================================================================================
  78. def getdoctype():
  79. # load parent doctype too
  80. import webnotes.model.doctype
  81. form, doclist = webnotes.form, []
  82. dt = form.getvalue('doctype')
  83. with_parent = form.getvalue('with_parent')
  84. # with parent (called from report builder)
  85. if with_parent:
  86. parent_dt = webnotes.model.meta.get_parent_dt(dt)
  87. if parent_dt:
  88. doclist = webnotes.model.doctype.get(parent_dt)
  89. webnotes.response['parent_dt'] = parent_dt
  90. if not doclist:
  91. doclist = webnotes.model.doctype.get(dt)
  92. # if single, send the record too
  93. if doclist[0].issingle:
  94. doclist += webnotes.model.doc.get(dt)
  95. # load search criteria for reports (all)
  96. doclist += webnotes.model.meta.get_search_criteria(dt)
  97. webnotes.response['docs'] = doclist
  98. #===========================================================================================
  99. def load_single_doc(dt, dn, user, prefix):
  100. import webnotes.model.code
  101. if not dn: dn = dt
  102. dl = webnotes.model.doc.get(dt, dn, prefix=prefix)
  103. # archive, done
  104. if prefix=='arc':
  105. return dl
  106. try:
  107. so, r = webnotes.model.code.get_server_obj(dl[0], dl), None
  108. if hasattr(so, 'onload'):
  109. r = webnotes.model.code.run_server_obj(so, 'onload')
  110. if hasattr(so, 'custom_onload'):
  111. r = webnotes.model.code.run_server_obj(so, 'custom_onload')
  112. if r:
  113. webnotes.msgprint(r)
  114. except Exception, e:
  115. webnotes.errprint(webnotes.utils.getTraceback())
  116. webnotes.msgprint('Error in script while loading')
  117. raise e
  118. if dl and not dn.startswith('_'):
  119. webnotes.user.update_recent(dt, dn)
  120. # load search criteria ---- if doctype
  121. if dt=='DocType':
  122. dl += webnotes.model.meta.get_search_criteria(dt)
  123. return dl
  124. # Check Guest Access
  125. #===========================================================================================
  126. def check_guest_access(doc):
  127. if webnotes.session['user']=='Guest' and not webnotes.conn.sql("select name from tabDocPerm where role='Guest' and parent=%s and ifnull(`read`,0)=1", doc.doctype):
  128. webnotes.msgprint("Guest not allowed to call this object")
  129. raise Exception
  130. # Runserverobj - run server calls from form
  131. #===========================================================================================
  132. def runserverobj():
  133. """
  134. Run server objects
  135. """
  136. import webnotes.model.code
  137. from webnotes.model.doclist import DocList
  138. from webnotes.utils import cint
  139. form = webnotes.form
  140. doclist = None
  141. method = form.getvalue('method')
  142. arg = form.getvalue('arg')
  143. dt = form.getvalue('doctype')
  144. dn = form.getvalue('docname')
  145. if dt: # not called from a doctype (from a page)
  146. if not dn: dn = dt # single
  147. so = webnotes.model.code.get_obj(dt, dn)
  148. else:
  149. doclist = DocList()
  150. doclist.from_compressed(form.getvalue('docs'), dn)
  151. so = doclist.make_obj()
  152. doclist.check_if_latest()
  153. check_guest_access(so.doc)
  154. if so:
  155. r = webnotes.model.code.run_server_obj(so, method, arg)
  156. if r:
  157. #build output as csv
  158. if cint(webnotes.form.getvalue('as_csv')):
  159. make_csv_output(r, so.doc.doctype)
  160. else:
  161. webnotes.response['message'] = r
  162. webnotes.response['docs'] =[so.doc] + so.doclist
  163. def make_csv_output(res, dt):
  164. import webnotes
  165. from cStringIO import StringIO
  166. import csv
  167. f = StringIO()
  168. writer = csv.writer(f)
  169. for r in res:
  170. writer.writerow(r)
  171. f.seek(0)
  172. webnotes.response['result'] = f.read()
  173. webnotes.response['type'] = 'csv'
  174. webnotes.response['doctype'] = dt.replace(' ','')
  175. def savedocs():
  176. try:
  177. from webnotes.model.doclist import DocList
  178. form = webnotes.form_dict
  179. doclist = DocList()
  180. doclist.from_compressed(form.get('docs'), form.get('docname'))
  181. # action
  182. action = form.get('action')
  183. if action=='Update': action='update_after_submit'
  184. getattr(doclist, action.lower())()
  185. # update recent documents
  186. webnotes.user.update_recent(doclist.doc.doctype, doclist.doc.name)
  187. # send updated docs
  188. webnotes.response['saved'] = '1'
  189. webnotes.response['main_doc_name'] = doclist.doc.name
  190. webnotes.response['docname'] = doclist.doc.name
  191. webnotes.response['docs'] = [doclist.doc] + doclist.children
  192. except Exception, e:
  193. webnotes.msgprint('Did not save')
  194. webnotes.errprint(webnotes.utils.getTraceback())
  195. raise e
  196. # Print Format
  197. #===========================================================================================
  198. def _get_print_format(match):
  199. name = match.group('name')
  200. return webnotes.model.meta.get_print_format_html(name)
  201. def get_print_format():
  202. import re
  203. import webnotes
  204. html = webnotes.model.meta.get_print_format_html(webnotes.form.getvalue('name'))
  205. p = re.compile('\$import\( (?P<name> [^)]*) \)', re.VERBOSE)
  206. out_html = ''
  207. if html:
  208. out_html = p.sub(_get_print_format, html)
  209. webnotes.response['message'] = out_html
  210. # remove attachment
  211. #===========================================================================================
  212. def remove_attach():
  213. import webnotes
  214. import webnotes.utils.file_manager
  215. fid = webnotes.form.getvalue('fid')
  216. webnotes.utils.file_manager.delete_file(fid, verbose=1)
  217. # remove from dt dn
  218. return str(webnotes.utils.file_manager.remove_file_list(webnotes.form.getvalue('dt'), webnotes.form.getvalue('dn'), fid))
  219. # Get Fields - Counterpart to $c_get_fields
  220. #===========================================================================================
  221. def get_fields():
  222. import webnotes
  223. r = {}
  224. args = {
  225. 'select':webnotes.form.getvalue('select')
  226. ,'from':webnotes.form.getvalue('from')
  227. ,'where':webnotes.form.getvalue('where')
  228. }
  229. ret = webnotes.conn.sql("select %(select)s from `%(from)s` where %(where)s limit 1" % args)
  230. if ret:
  231. fl, i = webnotes.form.getvalue('fields').split(','), 0
  232. for f in fl:
  233. r[f], i = ret[0][i], i+1
  234. webnotes.response['message']=r
  235. # validate link
  236. #===========================================================================================
  237. def validate_link():
  238. import webnotes
  239. import webnotes.utils
  240. value, options, fetch = webnotes.form.getvalue('value'), webnotes.form.getvalue('options'), webnotes.form.getvalue('fetch')
  241. # no options, don't validate
  242. if not options or options=='null' or options=='undefined':
  243. webnotes.response['message'] = 'Ok'
  244. return
  245. if webnotes.conn.sql("select name from `tab%s` where name=%s" % (options, '%s'), value):
  246. # get fetch values
  247. if fetch:
  248. webnotes.response['fetch_values'] = [webnotes.utils.parse_val(c) for c in webnotes.conn.sql("select %s from `tab%s` where name=%s" % (fetch, options, '%s'), value)[0]]
  249. webnotes.response['message'] = 'Ok'