1
0

team.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. import json
  16. from flask import request, g, abort, render_template
  17. from rrd import app
  18. from rrd.model.team import Team
  19. @app.route("/team/<int:team_id>/users", methods=["GET",])
  20. def team_users(team_id):
  21. if request.method == "GET":
  22. try:
  23. ret = Team.get_team_users(team_id)
  24. except Exception as e:
  25. ret = {"msg":str(e)}
  26. return json.dumps(ret)
  27. @app.route("/team/<team_name>/users", methods=["GET",])
  28. def team_users_by_name(team_name):
  29. if request.method == "GET":
  30. try:
  31. ret = Team.get_team_users_by_name(team_name)
  32. except Exception as e:
  33. ret = {"msg":str(e)}
  34. return json.dumps(ret)
  35. @app.route("/team/list", methods=["GET",])
  36. def team_list():
  37. if request.method == "GET":
  38. query_term = request.args.get("query", "")
  39. teams = Team.get_teams(query_term, g.limit or 20, g.page or 1)
  40. return render_template("team/list.html", **locals())
  41. @app.route("/team/create", methods=["GET", "POST"])
  42. def team_create():
  43. if request.method == "GET":
  44. return render_template("team/create.html", **locals())
  45. if request.method == "POST":
  46. ret = {"msg":""}
  47. name = request.form.get("name", "").strip()
  48. resume = request.form.get("resume", "").strip()
  49. users = request.form.get("users", "")
  50. user_ids = users and users.split(",") or []
  51. user_ids = [int(x) for x in user_ids]
  52. if not name:
  53. ret["msg"] = "empty name"
  54. return json.dumps(ret)
  55. try:
  56. Team.create_team(name, resume, user_ids)
  57. except Exception as e:
  58. ret['msg'] = str(e)
  59. return json.dumps(ret)
  60. @app.route("/team/<int:team_id>/edit", methods=["GET", "POST"])
  61. def team_edit(team_id):
  62. if request.method == "GET":
  63. j = Team.get_team_users(team_id)
  64. team = Team(j['id'], j['name'], j['resume'], j['creator'], j['creator_name'], [])
  65. team_user_ids = ",".join([str(x['id']) for x in j['users']])
  66. return render_template("team/edit.html", **locals())
  67. if request.method == "POST":
  68. ret = {"msg":""}
  69. resume = request.form.get("resume", "").strip()
  70. users = request.form.get("users", "")
  71. user_ids = users and users.split(",") or []
  72. user_ids = [int(x) for x in user_ids]
  73. try:
  74. Team.update_team(team_id, resume, user_ids)
  75. except Exception as e:
  76. ret['msg'] = str(e)
  77. return json.dumps(ret)
  78. @app.route("/team/<int:team_id>/delete", methods=["POST"])
  79. def team_delete(team_id):
  80. if request.method == "POST":
  81. ret = {"msg": ""}
  82. try:
  83. Team.delete_team(team_id)
  84. except Exception as e:
  85. ret['msg'] = str(e)
  86. return json.dumps(ret)