team.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 rrd import corelib
  17. from rrd import config
  18. from rrd.model.user import User
  19. from rrd.utils.logger import logging
  20. log = logging.getLogger(__file__)
  21. class Team(object):
  22. def __init__(self, id, name, resume, creator, creator_name, users=[]):
  23. self.id = id
  24. self.name = name
  25. self.resume = resume
  26. self.creator = creator #creator id
  27. self.creator_name = creator_name
  28. self.users = users
  29. def __repr__(self):
  30. return "<Team id=%s, name=%s>" % (self.id, self.name)
  31. __str__ = __repr__
  32. def dict(self):
  33. return {
  34. 'id': self.id,
  35. 'name': self.name,
  36. 'resume': self.resume,
  37. 'creator': self.creator,
  38. 'creator_name': self.creator_name,
  39. "users": [u.dict() for u in self.users],
  40. }
  41. @classmethod
  42. def get_team_users(cls, team_id):
  43. h = {"Content-type": "application/json"}
  44. r = corelib.auth_requests("GET", "%s/team/t/%s" \
  45. %(config.API_ADDR, team_id), headers=h)
  46. log.debug("%s:%s" %(r.status_code, r.text))
  47. if r.status_code != 200:
  48. raise Exception("%s %s" %(r.status_code, r.text))
  49. return r.json()
  50. @classmethod
  51. def get_team_users_by_name(cls, team_name):
  52. h = {"Content-type": "application/json"}
  53. r = corelib.auth_requests("GET", "%s/team/name/%s" \
  54. %(config.API_ADDR, team_name), headers=h)
  55. log.debug("%s:%s" %(r.status_code, r.text))
  56. if r.status_code != 200:
  57. raise Exception("%s %s" %(r.status_code, r.text))
  58. return r.json()
  59. @classmethod
  60. def get_teams(cls, query_term, limit=20, page=1):
  61. if not query_term:
  62. query_term = "."
  63. d = {
  64. "q": query_term,
  65. "limit": limit,
  66. "page": page,
  67. }
  68. h = {"Content-type": "application/json"}
  69. r = corelib.auth_requests("GET", "%s/team" \
  70. %(config.API_ADDR,), params=d, headers=h)
  71. log.debug("%s:%s" %(r.status_code, r.text))
  72. if r.status_code != 200:
  73. raise Exception("%s %s" %(r.status_code, r.text))
  74. teams = []
  75. for j in r.json():
  76. users = [User(x["id"], x["name"], x["cnname"], x["email"], x["phone"], x["im"], x["qq"], x["role"]) for x in j['users']]
  77. t = Team(j["team"]["id"], j["team"]["name"], j["team"]["resume"], j["team"]["creator"], j['creator_name'], users)
  78. teams.append(t)
  79. return teams
  80. @classmethod
  81. def create_team(cls, name, resume, user_ids=[]):
  82. h = {"Content-type": "application/json"}
  83. d = {
  84. "team_name": name, "resume": resume, "users": user_ids,
  85. }
  86. r = corelib.auth_requests("POST", "%s/team" %(config.API_ADDR,), \
  87. data=json.dumps(d), headers=h)
  88. log.debug("%s:%s" %(r.status_code, r.text))
  89. if r.status_code != 200:
  90. raise Exception("%s %s" %(r.status_code, r.text))
  91. return r.text
  92. @classmethod
  93. def update_team(cls, team_id, resume, user_ids=[]):
  94. h = {"Content-type": "application/json"}
  95. d = {
  96. "team_id": team_id, "resume": resume, "users": user_ids,
  97. }
  98. r = corelib.auth_requests("PUT", "%s/team" %(config.API_ADDR,), \
  99. data=json.dumps(d), headers=h)
  100. log.debug("%s:%s" %(r.status_code, r.text))
  101. if r.status_code != 200:
  102. raise Exception("%s %s" %(r.status_code, r.text))
  103. return r.text
  104. @classmethod
  105. def delete_team(cls, team_id):
  106. h = {"Content-type": "application/json"}
  107. r = corelib.auth_requests("DELETE", "%s/team/%s" \
  108. %(config.API_ADDR, team_id), headers=h)
  109. log.debug("%s:%s" %(r.status_code, r.text))
  110. if r.status_code != 200:
  111. raise Exception("%s %s" %(r.status_code, r.text))
  112. return r.text