group.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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, jsonify, render_template
  18. from rrd.model.portal.host_group import HostGroup
  19. from rrd.model.portal.grp_tpl import GrpTpl
  20. from rrd.service import group_service
  21. @app.route('/portal/group/create', methods=['POST'])
  22. def group_create_post():
  23. grp_name = request.form['grp_name'].strip()
  24. if not grp_name:
  25. return jsonify(msg="group name is blank")
  26. grp_id = HostGroup.create(grp_name, g.user.name, 1)
  27. if grp_id > 0:
  28. return jsonify(msg='')
  29. else:
  30. return jsonify(msg='grp_name has already existent')
  31. @app.route('/portal/group/delete/<group_id>')
  32. def group_delete_get(group_id):
  33. group_id = int(group_id)
  34. group = HostGroup.read(where='id = %s', params=[group_id])
  35. if not group:
  36. return jsonify(msg='no such group')
  37. if not group.writable(g.user):
  38. return jsonify(msg='no permission')
  39. return jsonify(msg=group_service.delete_group(group_id))
  40. @app.route('/portal/group/update/<group_id>', methods=['POST'])
  41. def group_update_post(group_id):
  42. group_id = int(group_id)
  43. new_name = request.form['new_name'].strip()
  44. if not new_name:
  45. return jsonify(msg='new name is blank')
  46. group = HostGroup.read(where='id = %s', params=[group_id])
  47. if not group:
  48. return jsonify(msg='no such group')
  49. if not group.writable(g.user):
  50. return jsonify(msg='no permission')
  51. HostGroup.update_dict({'grp_name': new_name}, 'id=%s', [group_id])
  52. return jsonify(msg='')
  53. @app.route('/portal/group/advanced')
  54. def group_advanced_get():
  55. return render_template('portal/group/advanced.html')
  56. @app.route('/portal/group/rename', methods=['POST'])
  57. def group_rename_post():
  58. old_str = request.form['old_str'].strip()
  59. new_str = request.form['new_str'].strip()
  60. if not old_str:
  61. return jsonify(msg='old is blank')
  62. return jsonify(msg=group_service.rename(old_str, new_str, g.user.name))
  63. @app.route('/portal/group/templates/<grp_id>')
  64. def group_templates_get(grp_id):
  65. grp_id = int(grp_id)
  66. grp = HostGroup.read(where='id = %s', params=[grp_id])
  67. if not grp:
  68. return jsonify(msg='no such group')
  69. ts = GrpTpl.tpl_list(grp_id)
  70. return render_template('portal/group/templates.html', group=grp, ts=ts)
  71. @app.route('/portal/group/bind/template')
  72. def group_bind_template_get():
  73. tpl_id = request.args.get('tpl_id', '').strip()
  74. grp_id = request.args.get('grp_id', '').strip()
  75. if not tpl_id:
  76. return jsonify(msg="tpl id is blank")
  77. if not grp_id:
  78. return jsonify(msg="grp id is blank")
  79. GrpTpl.bind(grp_id, tpl_id, g.user.name)
  80. return jsonify(msg='')