1
0

expression.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # -*- coding:utf-8 -*-
  2. # Copyright 2017 Xiaomi, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. __author__ = 'Ulric Qin'
  16. from rrd import app
  17. from flask import request, g, render_template, jsonify
  18. from rrd.model.portal.expression import Expression
  19. from rrd.model.portal.action import Action
  20. from rrd.utils.params import required_chk
  21. @app.route('/portal/expression')
  22. def expressions_get():
  23. page = int(request.args.get('p', 1))
  24. limit = int(request.args.get('limit', 6))
  25. query = request.args.get('q', '').strip()
  26. mine = request.args.get('mine', '1')
  27. me = g.user.name if mine == '1' else None
  28. vs, total = Expression.query(page, limit, query, me)
  29. for v in vs:
  30. v.action = Action.get(v.action_id)
  31. return render_template(
  32. 'portal/expression/list.html',
  33. data={
  34. 'vs': vs,
  35. 'total': total,
  36. 'query': query,
  37. 'limit': limit,
  38. 'page': page,
  39. 'mine': mine,
  40. }
  41. )
  42. @app.route('/portal/expression/delete/<expression_id>')
  43. def expression_delete_get(expression_id):
  44. expression_id = int(expression_id)
  45. Expression.delete_one(expression_id)
  46. return jsonify(msg='')
  47. @app.route('/portal/expression/add')
  48. def expression_add_get():
  49. a = None
  50. o = Expression.get(int(request.args.get('id', '0').strip()))
  51. if o:
  52. a = Action.get(o.action_id)
  53. return render_template('portal/expression/add.html',
  54. data={'action': a, 'expression': o})
  55. @app.route('/portal/expression/update', methods=['POST'])
  56. def expression_update_post():
  57. expression_id = request.form['expression_id'].strip()
  58. expression = request.form['expression'].strip()
  59. func = request.form['func'].strip()
  60. op = request.form['op'].strip()
  61. right_value = request.form['right_value'].strip()
  62. uic_groups = request.form['uic'].strip()
  63. max_step = request.form['max_step'].strip()
  64. priority = int(request.form['priority'].strip())
  65. note = request.form['note'].strip()
  66. url = request.form['url'].strip()
  67. callback = request.form['callback'].strip()
  68. before_callback_sms = request.form['before_callback_sms']
  69. before_callback_mail = request.form['before_callback_mail']
  70. after_callback_sms = request.form['after_callback_sms']
  71. after_callback_mail = request.form['after_callback_mail']
  72. msg = required_chk({
  73. 'expression': expression,
  74. 'func': func,
  75. 'op': op,
  76. 'right_value': right_value,
  77. })
  78. if msg:
  79. return jsonify(msg=msg)
  80. if not max_step:
  81. max_step = 3
  82. if not priority:
  83. priority = 0
  84. return jsonify(msg=Expression.save_or_update(
  85. expression_id,
  86. expression,
  87. func,
  88. op,
  89. right_value,
  90. uic_groups,
  91. max_step,
  92. priority,
  93. note,
  94. url,
  95. callback,
  96. before_callback_sms,
  97. before_callback_mail,
  98. after_callback_sms,
  99. after_callback_mail,
  100. g.user.name,
  101. ))
  102. @app.route('/portal/expression/pause')
  103. def expression_pause_get():
  104. expression_id = request.args.get("id", '')
  105. pause = request.args.get('pause', '')
  106. if not expression_id:
  107. return jsonify(msg='id is blank')
  108. if not pause:
  109. return jsonify(msg='pause is blank')
  110. e = Expression.get(expression_id)
  111. if not e:
  112. return jsonify('no such expression %s' % expression_id)
  113. Expression.update_dict({'pause': pause}, 'id=%s', [expression_id])
  114. return jsonify(msg='')
  115. @app.route('/portal/expression/view/<eid>')
  116. def expression_view_get(eid):
  117. eid = int(eid)
  118. a = None
  119. o = Expression.get(eid)
  120. if o:
  121. a = Action.get(o.action_id)
  122. else:
  123. return 'no such expression'
  124. return render_template('portal/expression/view.html', data={'action': a, 'expression': o})