From ca6b6bd06a3171ae91229c7cd4c585d743b07149 Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 2 Jan 2024 17:07:47 +0800 Subject: [PATCH 01/14] api team list --- server/model/team.py | 25 +++++++++++++++++++++++++ server/routes/team.py | 4 +++- server/utils/auth.py | 2 +- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 server/model/team.py diff --git a/server/model/team.py b/server/model/team.py new file mode 100644 index 00000000..f3c22002 --- /dev/null +++ b/server/model/team.py @@ -0,0 +1,25 @@ +from sqlalchemy import and_, or_ + +from .schema import Team, TeamMember, db + + +def query_one_page(query, page, size): + offset = (page - 1) * int(size) + return ( + query.offset(offset if offset > 0 else 0).limit(size if size > 0 else 0).all() + ) + + +def get_team_list_by_user_id(app_id, page=1, size=20): + query = db.session.query(Team).filter( + or_( + Team.user_id == user_id, # 管理员 + and_( + TeamMember.team_id == Team.id, + TeamMember.code_user_id == user_id, # 属于某个team的员工 + TeamMember.status == 0, + ), + Team.status == 0, + ) + ) + return query_one_page(query, page, size), total diff --git a/server/routes/team.py b/server/routes/team.py index f32ed6da..0ed9df45 100644 --- a/server/routes/team.py +++ b/server/routes/team.py @@ -2,6 +2,7 @@ from app import app from flask import Blueprint, abort, jsonify, redirect, request +from model.team import get_team_list_by_user_id from utils.auth import authenticated bp = Blueprint("team", __name__, url_prefix="/api/team") @@ -14,7 +15,8 @@ def get_team_list(): get team list TODO """ - return jsonify({"code": 0, "msg": "success", "data": [], "total": 0}) + data, total = get_team_list_by_user_id(session["user_id"]) + return jsonify({"code": 0, "msg": "success", "data": data, "total": total}) app.register_blueprint(bp) diff --git a/server/utils/auth.py b/server/utils/auth.py index e008d6e5..f0ecf01b 100644 --- a/server/utils/auth.py +++ b/server/utils/auth.py @@ -6,7 +6,7 @@ def authenticated(func): @wraps(func) def wrapper(*args, **kwargs): - if "user_id" not in session: + if not session.get("user_id"): return abort(401) return func(*args, **kwargs) From 265cd7c86482da81ec94ab8b265e789b6ee2985d Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 2 Jan 2024 18:31:51 +0800 Subject: [PATCH 02/14] api account api --- server/model/team.py | 24 ++++++++++++++++-------- server/model/user.py | 14 ++++++++++++++ server/routes/__init__.py | 1 + server/routes/user.py | 35 +++++++++++++++++++++++++++++++++++ server/utils/utils.py | 5 +++++ 5 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 server/model/user.py create mode 100644 server/routes/user.py create mode 100644 server/utils/utils.py diff --git a/server/model/team.py b/server/model/team.py index f3c22002..d435385c 100644 --- a/server/model/team.py +++ b/server/model/team.py @@ -1,16 +1,10 @@ from sqlalchemy import and_, or_ +from utils.utils import query_one_page from .schema import Team, TeamMember, db -def query_one_page(query, page, size): - offset = (page - 1) * int(size) - return ( - query.offset(offset if offset > 0 else 0).limit(size if size > 0 else 0).all() - ) - - -def get_team_list_by_user_id(app_id, page=1, size=20): +def get_team_list_by_user_id(user_id, page=1, size=100): query = db.session.query(Team).filter( or_( Team.user_id == user_id, # 管理员 @@ -23,3 +17,17 @@ def get_team_list_by_user_id(app_id, page=1, size=20): ) ) return query_one_page(query, page, size), total + + +def is_team_admin(team_id, user_id): + return ( + True + if db.session.query(Team.id) + .filter( + Team.user_id == user_id, + Team.status == 0, + ) + .limit(1) + .scalar() + else False + ) diff --git a/server/model/user.py b/server/model/user.py new file mode 100644 index 00000000..d83efa6c --- /dev/null +++ b/server/model/user.py @@ -0,0 +1,14 @@ +from sqlalchemy import and_, or_ + +from .schema import User + + +def get_user_by_id(user_id): + return ( + db.session.query(User) + .filter( + User.id == user_id, + User.status == 0, + ) + .first() + ) diff --git a/server/routes/__init__.py b/server/routes/__init__.py index a7105778..031d8f3d 100644 --- a/server/routes/__init__.py +++ b/server/routes/__init__.py @@ -1,3 +1,4 @@ from .github import * from .lark import * from .team import * +from .user import * diff --git a/server/routes/user.py b/server/routes/user.py new file mode 100644 index 00000000..917cd0e9 --- /dev/null +++ b/server/routes/user.py @@ -0,0 +1,35 @@ +import logging + +from app import app +from flask import Blueprint, abort, jsonify, redirect, request +from model.team import get_team_list_by_user_id, is_team_admin +from model.user import get_user_by_id +from utils.auth import authenticated + +bp = Blueprint("user", __name__, url_prefix="/api") + + +@bp.route("/account", methods=["GET"]) +@authenticated +def get_account(): + current_user = session["user_id"] + user = get_user_by_id(current_user) + teams, _ = get_team_list_by_user_id(current_user) + current_team = session.get("team_id") + if not current_team and len(teams) > 0: + current_team = teams[0].id + is_admin = is_team_admin(current_team, current_user) if current_team else False + return jsonify( + { + "code": 0, + "msg": "success", + "data": { + "user": user, + "current_team": current_team, + "is_team_admin": is_admin, + }, + } + ) + + +app.register_blueprint(bp) diff --git a/server/utils/utils.py b/server/utils/utils.py new file mode 100644 index 00000000..6e2d621f --- /dev/null +++ b/server/utils/utils.py @@ -0,0 +1,5 @@ +def query_one_page(query, page, size): + offset = (page - 1) * int(size) + return ( + query.offset(offset if offset > 0 else 0).limit(size if size > 0 else 0).all() + ) From aba640251e7d603cb65b8cd5e97fa834fa3264e1 Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 2 Jan 2024 19:30:24 +0800 Subject: [PATCH 03/14] custom json encoder --- server/model/schema.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/server/model/schema.py b/server/model/schema.py index ae3cd27f..dbad8bf4 100644 --- a/server/model/schema.py +++ b/server/model/schema.py @@ -1,6 +1,7 @@ import json import logging from datetime import datetime +from time import time import bson import click @@ -318,6 +319,20 @@ class ChatGroup(Base): ) +class JSONEncoder(json.JSONEncoder): + def default(self, value): + if isinstance(value, Decimal): + return int(value) + if isinstance(value, datetime): + return value.strftime("%Y-%m-%d %H:%M:%S") + if isinstance(value, time): + return value.isoformat() + return json.JSONEncoder.default(self, value) + + +app.json_encoder = JSONEncoder + + # create command function @click.command(name="create") @with_appcontext From 022ceb35087eda22e8b94d749de72bcab4a82e22 Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 2 Jan 2024 20:05:04 +0800 Subject: [PATCH 04/14] custom json encoder --- server/model/schema.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/server/model/schema.py b/server/model/schema.py index dbad8bf4..7feb6513 100644 --- a/server/model/schema.py +++ b/server/model/schema.py @@ -7,6 +7,7 @@ import click from app import app, db from flask.cli import with_appcontext +from flask.json.provider import DefaultJSONProvider from sqlalchemy import BINARY, ForeignKey, String, text @@ -319,18 +320,21 @@ class ChatGroup(Base): ) -class JSONEncoder(json.JSONEncoder): - def default(self, value): - if isinstance(value, Decimal): - return int(value) - if isinstance(value, datetime): - return value.strftime("%Y-%m-%d %H:%M:%S") - if isinstance(value, time): - return value.isoformat() - return json.JSONEncoder.default(self, value) +class CustomJsonProvider(DefaultJSONProvider): + def dumps(self, value, **kw): + if isinstance(value, db.Model): + value = value.__dict__ + elif isinstance(value, Decimal): + value = int(value) + elif isinstance(value, datetime): + value = value.strftime("%Y-%m-%d %H:%M:%S") + elif isinstance(value, time): + value = value.isoformat() + return super().dumps(value, **kw) # Delegate to the default dumps -app.json_encoder = JSONEncoder +app.json_provider_class = CustomJsonProvide +app.json = CustomJsonProvider(app) # create command function From 869b13f9d12293de36ff3931cf7835635cd77f5b Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 2 Jan 2024 20:19:33 +0800 Subject: [PATCH 05/14] custom json encoder --- server/model/schema.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/server/model/schema.py b/server/model/schema.py index 7feb6513..4018983d 100644 --- a/server/model/schema.py +++ b/server/model/schema.py @@ -320,17 +320,20 @@ class ChatGroup(Base): ) +def _default(value): + if isinstance(value, db.Model): + return value.__dict__ + elif isinstance(value, Decimal): + return int(value) + elif isinstance(value, datetime): + return value.strftime("%Y-%m-%d %H:%M:%S") + elif isinstance(value, time): + return value.isoformat() + return str(value) + + class CustomJsonProvider(DefaultJSONProvider): - def dumps(self, value, **kw): - if isinstance(value, db.Model): - value = value.__dict__ - elif isinstance(value, Decimal): - value = int(value) - elif isinstance(value, datetime): - value = value.strftime("%Y-%m-%d %H:%M:%S") - elif isinstance(value, time): - value = value.isoformat() - return super().dumps(value, **kw) # Delegate to the default dumps + default = _default app.json_provider_class = CustomJsonProvide From 6ff046a0f6effb9ef5a613532f92fe02930e3754 Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 2 Jan 2024 20:27:22 +0800 Subject: [PATCH 06/14] merge code from server --- server/model/schema.py | 22 ++++++++-------------- server/model/team.py | 3 +++ server/model/user.py | 2 +- server/routes/user.py | 2 +- server/utils/user.py | 2 +- 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/server/model/schema.py b/server/model/schema.py index 4018983d..6214167c 100644 --- a/server/model/schema.py +++ b/server/model/schema.py @@ -320,23 +320,17 @@ class ChatGroup(Base): ) -def _default(value): - if isinstance(value, db.Model): - return value.__dict__ - elif isinstance(value, Decimal): - return int(value) - elif isinstance(value, datetime): - return value.strftime("%Y-%m-%d %H:%M:%S") - elif isinstance(value, time): - return value.isoformat() - return str(value) - - class CustomJsonProvider(DefaultJSONProvider): - default = _default + @staticmethod + def default(value): + if isinstance(value, db.Model): + return value.__dict__ + elif isinstance(value, datetime): + return value.strftime("%Y-%m-%d %H:%M:%S") + return str(value) -app.json_provider_class = CustomJsonProvide +app.json_provider_class = CustomJsonProvider app.json = CustomJsonProvider(app) diff --git a/server/model/team.py b/server/model/team.py index d435385c..d69b5cf9 100644 --- a/server/model/team.py +++ b/server/model/team.py @@ -16,6 +16,9 @@ def get_team_list_by_user_id(user_id, page=1, size=100): Team.status == 0, ) ) + total = query.count() + if total == 0: + return [], 0 return query_one_page(query, page, size), total diff --git a/server/model/user.py b/server/model/user.py index d83efa6c..cc630f8c 100644 --- a/server/model/user.py +++ b/server/model/user.py @@ -1,6 +1,6 @@ from sqlalchemy import and_, or_ -from .schema import User +from .schema import User, db def get_user_by_id(user_id): diff --git a/server/routes/user.py b/server/routes/user.py index 917cd0e9..d140484a 100644 --- a/server/routes/user.py +++ b/server/routes/user.py @@ -1,7 +1,7 @@ import logging from app import app -from flask import Blueprint, abort, jsonify, redirect, request +from flask import Blueprint, jsonify, session from model.team import get_team_list_by_user_id, is_team_admin from model.user import get_user_by_id from utils.auth import authenticated diff --git a/server/utils/user.py b/server/utils/user.py index 36ff98fa..50edc4e4 100644 --- a/server/utils/user.py +++ b/server/utils/user.py @@ -35,7 +35,7 @@ def register(code: str) -> str | None: ) db.session.add(new_user) - db.session.commit() + db.session.flush() new_bind_user = BindUser( id=ObjID.new_id(), From f6a07acb00b54325e86de1c66efb73d98bd27044 Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 2 Jan 2024 20:39:00 +0800 Subject: [PATCH 07/14] add team detail api --- server/app.py | 10 +++++++++- server/model/team.py | 22 ++++++++++++++++++++++ server/routes/team.py | 8 ++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/server/app.py b/server/app.py index 6f503943..1546050c 100644 --- a/server/app.py +++ b/server/app.py @@ -1,7 +1,7 @@ import logging import os -from flask import Flask, session +from flask import Flask, jsonify from flask_cors import CORS from flask_sqlalchemy import SQLAlchemy @@ -13,6 +13,14 @@ app, allow_headers=["Authorization", "X-Requested-With"], supports_credentials=True ) + +@app.errorhandler(404) +def custom404(error): + response = jsonify({"code": -1, "msg": error.description}) + response.status_code = 404 + return response + + gunicorn_logger = logging.getLogger("gunicorn.error") app.logger.handlers = gunicorn_logger.handlers app.logger.setLevel(gunicorn_logger.level) diff --git a/server/model/team.py b/server/model/team.py index d69b5cf9..f9eb2960 100644 --- a/server/model/team.py +++ b/server/model/team.py @@ -1,3 +1,4 @@ +from flask import abort from sqlalchemy import and_, or_ from utils.utils import query_one_page @@ -34,3 +35,24 @@ def is_team_admin(team_id, user_id): .scalar() else False ) + + +def get_team_by_id(team_id, user_id): + team = ( + db.session.query(Team) + .filter( + or_( + Team.user_id == user_id, # 管理员 + and_( + TeamMember.team_id == Team.id, + TeamMember.code_user_id == user_id, # 属于某个team的员工 + TeamMember.status == 0, + ), + Team.status == 0, + ) + ) + .first() + ) + if not team: + return abort(404, "can not found team by id") + return team diff --git a/server/routes/team.py b/server/routes/team.py index 0ed9df45..aceb9a32 100644 --- a/server/routes/team.py +++ b/server/routes/team.py @@ -19,4 +19,12 @@ def get_team_list(): return jsonify({"code": 0, "msg": "success", "data": data, "total": total}) +@app.route("/") +@bp.route("/", methods=["GET"]) +@authenticated +def get_team_detail(team_id): + data = get_team_by_id(team_id, session["user_id"]) + return jsonify({"code": 0, "msg": "success", "data": data}) + + app.register_blueprint(bp) From a2bf8ab4fdf2e9564ddabb68021c16b6ff4c47bc Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 2 Jan 2024 20:40:13 +0800 Subject: [PATCH 08/14] add team detail api --- server/routes/lark.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/routes/lark.py b/server/routes/lark.py index c1664e69..4e868a4f 100644 --- a/server/routes/lark.py +++ b/server/routes/lark.py @@ -1,10 +1,11 @@ import logging import os -from app import app, session +from app import app from connectai.lark.oauth import Server as OauthServerBase from connectai.lark.sdk import Bot, MarketBot from connectai.lark.webhook import LarkServer as LarkServerBase +from flask import session from model.lark import get_bot_by_app_id From 8e1e42689a9ed175b898d5152c1e01cb69026cd3 Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 2 Jan 2024 20:40:55 +0800 Subject: [PATCH 09/14] add team detail api --- server/routes/team.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/routes/team.py b/server/routes/team.py index aceb9a32..98dcb99c 100644 --- a/server/routes/team.py +++ b/server/routes/team.py @@ -1,7 +1,7 @@ import logging from app import app -from flask import Blueprint, abort, jsonify, redirect, request +from flask import Blueprint, abort, jsonify, redirect, request, session from model.team import get_team_list_by_user_id from utils.auth import authenticated From 1a3b50b7ff78c2e1f3d8b6719892ec7f53fb54e0 Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 2 Jan 2024 20:41:57 +0800 Subject: [PATCH 10/14] add team detail api --- server/routes/team.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/routes/team.py b/server/routes/team.py index 98dcb99c..acda6fe3 100644 --- a/server/routes/team.py +++ b/server/routes/team.py @@ -2,7 +2,7 @@ from app import app from flask import Blueprint, abort, jsonify, redirect, request, session -from model.team import get_team_list_by_user_id +from model.team import get_team_by_id, get_team_list_by_user_id from utils.auth import authenticated bp = Blueprint("team", __name__, url_prefix="/api/team") From 01605e9321347ed0f0d2f5c962be72f4412de0e9 Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 2 Jan 2024 21:03:28 +0800 Subject: [PATCH 11/14] add team member api --- server/app.py | 9 ++++++++- server/model/team.py | 33 +++++++++++++++++++++++++++++---- server/routes/team.py | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 7 deletions(-) diff --git a/server/app.py b/server/app.py index 1546050c..f79b7c91 100644 --- a/server/app.py +++ b/server/app.py @@ -15,12 +15,19 @@ @app.errorhandler(404) -def custom404(error): +def page_not_found(error): response = jsonify({"code": -1, "msg": error.description}) response.status_code = 404 return response +@app.errorhandler(400) +def bad_request(error): + response = jsonify({"code": -1, "msg": error.description}) + response.status_code = 400 + return response + + gunicorn_logger = logging.getLogger("gunicorn.error") app.logger.handlers = gunicorn_logger.handlers app.logger.setLevel(gunicorn_logger.level) diff --git a/server/model/team.py b/server/model/team.py index f9eb2960..7075ec11 100644 --- a/server/model/team.py +++ b/server/model/team.py @@ -14,8 +14,8 @@ def get_team_list_by_user_id(user_id, page=1, size=100): TeamMember.code_user_id == user_id, # 属于某个team的员工 TeamMember.status == 0, ), - Team.status == 0, - ) + ), + Team.status == 0, ) total = query.count() if total == 0: @@ -48,11 +48,36 @@ def get_team_by_id(team_id, user_id): TeamMember.code_user_id == user_id, # 属于某个team的员工 TeamMember.status == 0, ), - Team.status == 0, - ) + ), + Team.status == 0, ) .first() ) if not team: return abort(404, "can not found team by id") return team + + +def get_team_member(team_id, user_id, page=1, size=20): + query = db.session.query(TeamMember).filter( + TeamMember.team_id == team_id, + TeamMember.status == 0, + ) + # admin can get all users in current_team + if not is_team_admin(team_id, user_id): + query = query.filter( + TeamMember.code_user_id == user_id, + ) + total = query.count() + if total == 0: + return [], 0 + return query_one_page(query, page, size), total + + +def set_team_member(team_id, code_user_id, im_user_id): + db.session.query(TeamMember).filter( + TeamMember.team_id == team_id, + TeamMember.code_user_id == code_user_id, + TeamMember.status == 0, + ).update(dict(im_user_id=im_user_id)) + db.session.commit() diff --git a/server/routes/team.py b/server/routes/team.py index acda6fe3..ce6eb6ee 100644 --- a/server/routes/team.py +++ b/server/routes/team.py @@ -2,7 +2,13 @@ from app import app from flask import Blueprint, abort, jsonify, redirect, request, session -from model.team import get_team_by_id, get_team_list_by_user_id +from model.team import ( + get_team_by_id, + get_team_list_by_user_id, + get_team_member, + is_team_admin, + set_team_member, +) from utils.auth import authenticated bp = Blueprint("team", __name__, url_prefix="/api/team") @@ -19,7 +25,6 @@ def get_team_list(): return jsonify({"code": 0, "msg": "success", "data": data, "total": total}) -@app.route("/") @bp.route("/", methods=["GET"]) @authenticated def get_team_detail(team_id): @@ -27,4 +32,32 @@ def get_team_detail(team_id): return jsonify({"code": 0, "msg": "success", "data": data}) +@bp.route("//member", methods=["GET"]) +@authenticated +def get_team_member_by_team_id(team_id): + page = request.args.get("page", default=1, type=int) + size = request.args.get("size", default=20, type=int) + + current_user = session["user_id"] + data, total = get_team_member(team_id, current_user, page, size) + return jsonify({"code": 0, "msg": "success", "data": data, "total": total}) + + +@bp.route("//member", methods=["POST"]) +@authenticated +def save_team_member_by_team_id(team_id): + code_user_id = request.json.get("code_user_id") + im_user_id = request.json.get("im_user_id") + if not code_user_id or not im_user_id: + return abort(400, "params error") + + current_user = session["user_id"] + is_admin = is_team_admin(team_id, current_user) + if current_user != code_user_id and not is_admin: + return abort(400, "permission error") + + set_team_member(team_id, code_user_id, im_user_id) + return jsonify({"code": 0, "msg": "success"}) + + app.register_blueprint(bp) From ed10bbb0bf9cfafb320c87ac3b09da64d90e91fa Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 2 Jan 2024 21:20:05 +0800 Subject: [PATCH 12/14] add team detail api --- server/model/team.py | 5 +++++ server/routes/team.py | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/server/model/team.py b/server/model/team.py index 7075ec11..ad3c5c1c 100644 --- a/server/model/team.py +++ b/server/model/team.py @@ -58,6 +58,11 @@ def get_team_by_id(team_id, user_id): return team +def get_platform_info_by_team_id(team_id): + # TODO + return None, None + + def get_team_member(team_id, user_id, page=1, size=20): query = db.session.query(TeamMember).filter( TeamMember.team_id == team_id, diff --git a/server/routes/team.py b/server/routes/team.py index ce6eb6ee..8c2fd6d7 100644 --- a/server/routes/team.py +++ b/server/routes/team.py @@ -3,6 +3,7 @@ from app import app from flask import Blueprint, abort, jsonify, redirect, request, session from model.team import ( + get_platform_info_by_team_id, get_team_by_id, get_team_list_by_user_id, get_team_member, @@ -28,8 +29,19 @@ def get_team_list(): @bp.route("/", methods=["GET"]) @authenticated def get_team_detail(team_id): - data = get_team_by_id(team_id, session["user_id"]) - return jsonify({"code": 0, "msg": "success", "data": data}) + team = get_team_by_id(team_id, session["user_id"]) + code_platform, im_platform = get_platform_info_by_team_id(team_id) + return jsonify( + { + "code": 0, + "msg": "success", + "data": { + "team": team, + "code_platform": code_platform, + "im_platform": im_platform, + }, + } + ) @bp.route("//member", methods=["GET"]) From fbf5ea57c76257cfdb82a58b1c58c33c3f3e434d Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 2 Jan 2024 21:30:48 +0800 Subject: [PATCH 13/14] add im platform user list api --- server/model/team.py | 25 +++++++++++++++++++++++++ server/routes/team.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/server/model/team.py b/server/model/team.py index ad3c5c1c..9e7c9aa2 100644 --- a/server/model/team.py +++ b/server/model/team.py @@ -78,6 +78,31 @@ def get_team_member(team_id, user_id, page=1, size=20): return [], 0 return query_one_page(query, page, size), total + platform_id = db.Column( + ObjID(12), ForeignKey("im_platform.id"), nullable=True, comment="平台" + ) + + +def get_im_user_by_team_id(team_id, page=1, size=20): + query = ( + db.session.query(BindUser) + .join( + IMPlatform, + IMPlatform.id == BindUser.platform_id, + ) + .filter( + IMPlatform.team_id == team_id, + IMPlatform.status == 0, + BindUser.status == 0, + ) + ) + total = query.count() + if total == 0: + return [], 0 + return query_one_page(query, page, size), total + + data, total = get_im_user_by_team_id(team_id, page, size) + def set_team_member(team_id, code_user_id, im_user_id): db.session.query(TeamMember).filter( diff --git a/server/routes/team.py b/server/routes/team.py index 8c2fd6d7..00739547 100644 --- a/server/routes/team.py +++ b/server/routes/team.py @@ -3,6 +3,7 @@ from app import app from flask import Blueprint, abort, jsonify, redirect, request, session from model.team import ( + get_im_user_by_team_id, get_platform_info_by_team_id, get_team_by_id, get_team_list_by_user_id, @@ -55,6 +56,35 @@ def get_team_member_by_team_id(team_id): return jsonify({"code": 0, "msg": "success", "data": data, "total": total}) +@bp.route("///user", methods=["GET"]) +@authenticated +def get_im_user_by_team_id_and_platform(team_id, platform): + page = request.args.get("page", default=1, type=int) + size = request.args.get("size", default=20, type=int) + + if platform not in ["lark"]: # TODO lark/slack... + return abort(400, "params error") + + current_user = session["user_id"] + data, total = get_im_user_by_team_id(team_id, page, size) + return jsonify( + { + "code": 0, + "msg": "success", + "data": [ + { + "value": i.user_id, + "label": i.name or i.email, + "email": i.email, + "avatar": i.avatar, + } + for i in data + ], + "total": total, + } + ) + + @bp.route("//member", methods=["POST"]) @authenticated def save_team_member_by_team_id(team_id): From 9a07909685920c8b725e992e1315b16086d86daa Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Tue, 2 Jan 2024 21:40:46 +0800 Subject: [PATCH 14/14] hotfix --- server/model/team.py | 4 ---- server/routes/lark.py | 1 - server/routes/team.py | 2 -- server/routes/user.py | 2 -- 4 files changed, 9 deletions(-) diff --git a/server/model/team.py b/server/model/team.py index 9e7c9aa2..69b8f85d 100644 --- a/server/model/team.py +++ b/server/model/team.py @@ -78,10 +78,6 @@ def get_team_member(team_id, user_id, page=1, size=20): return [], 0 return query_one_page(query, page, size), total - platform_id = db.Column( - ObjID(12), ForeignKey("im_platform.id"), nullable=True, comment="平台" - ) - def get_im_user_by_team_id(team_id, page=1, size=20): query = ( diff --git a/server/routes/lark.py b/server/routes/lark.py index 4e868a4f..d40c9183 100644 --- a/server/routes/lark.py +++ b/server/routes/lark.py @@ -1,4 +1,3 @@ -import logging import os from app import app diff --git a/server/routes/team.py b/server/routes/team.py index 00739547..13f72ff7 100644 --- a/server/routes/team.py +++ b/server/routes/team.py @@ -1,5 +1,3 @@ -import logging - from app import app from flask import Blueprint, abort, jsonify, redirect, request, session from model.team import ( diff --git a/server/routes/user.py b/server/routes/user.py index d140484a..bf9d6724 100644 --- a/server/routes/user.py +++ b/server/routes/user.py @@ -1,5 +1,3 @@ -import logging - from app import app from flask import Blueprint, jsonify, session from model.team import get_team_list_by_user_id, is_team_admin