def post(self): group_id = int(self.get_argument("group_id")) group_name = self.get_argument("group_name") query = TeamMemberGroup.select().where( TeamMemberGroup.team == self.current_team, TeamMemberGroup.name == group_name) if group_id > 0: query = query.where(TeamMemberGroup.id != group_id) if query.exists(): raise HttpBadRequestError("分组名称已存在") if group_id == 0: try: group = TeamMemberGroup.create(team=self.current_team, name=group_name) except peewee.IntegrityError: raise HttpForbiddenError("该分组已经存在") else: TeamMemberGroup.update(name=group_name).where( TeamMemberGroup.id == group_id, TeamMemberGroup.team == self.current_team).execute() group = TeamMemberGroup.get_or_404(id=group_id) self.write({"group": group.info})
def validate_group_id(self, team, group_id): """校验分组是否存在""" try: group = TeamMemberGroup.get(id=group_id, team=team) except TeamMemberGroup.DoesNotExist: logging.debug("{0} {1}".format(group_id, team.groups)) raise ApiException(400, "分组不存在") return group
def test_patch_group(self): url = self.OBJECT_PATH.format(team_id=self.team.id, group_id=self.default_group.id) body = {"name": "new name"} self.auth_user = self.team_owner response = self.fetch(url, method="PATCH", body=json.dumps(body)) self.assertEqual(204, response.code, response.body.decode()) updated = TeamMemberGroup.get(id=self.default_group.id) self.assertEqual("new name", updated.name, "修改分组名失败了")
def get(self): parteam = Parteam(self.settings["parteam_api_url"]) team = self.current_team groups = TeamMemberGroup.select().where(TeamMemberGroup.team == team) query = TeamMember.select(TeamMember).\ where(TeamMember.team == team) # 按备注名字搜索 kw = self.get_argument("kw", None) if kw: query = query.where((TeamMember.nick.contains(kw)) | (User.mobile == kw)) sort = self.get_argument("sort", "") if sort in self._VALID_SORT_TYPE: if sort == "-name": query = query.order_by(TeamMember.nick.desc()) elif sort == "-credit": query = query.order_by(TeamMember.credit.desc()) elif sort == "credit": query = query.order_by(TeamMember.credit.asc()) filter_by_role = self.get_argument("role", None) if filter_by_role in self._VALID_ROLE: if filter_by_role == "member": pass elif filter_by_role == "vip": query = query.where(TeamMember.is_vip == True) elif filter_by_role == "black_list": pass # 分 group 查询 group_name = self.get_argument("group", None) if group_name: query = query.where(TeamMember.group_name == group_name) members = self.paginate_query(query) # user_ids = map(lambda member: member.parteam_user_id, members) user_ids = [ member.parteam_user_id for member in members if member.parteam_user_id ] if user_ids: user_infos = parteam.parteam_user(user_ids) for member in members: member.user_info = user_infos.get(member.parteam_user_id) self.render("member/parteam_members_list.html", groups=groups, groups_json=[g.info for g in groups], members=members)
def patch(self, team_id, group_id): """ 修改分组 Args: team_id: group_id: Returns: """ team = Team.get_or_404(id=team_id) TeamMemberGroup.get_or_404(id=group_id, team=team) self.has_read_permission(team) TeamMemberGroup\ .update(**self.validated_arguments)\ .where(TeamMemberGroup.team == team, TeamMemberGroup.id == group_id)\ .execute() self.set_status(204)
def test_create_group(self): url = self.MODEL_PATH.format(team_id=self.team.id) body = {"name": "group name"} self.auth_user = self.team_owner response = self.fetch(url, method="POST", body=json.dumps(body)) self.assertEqual(201, response.code, response.body.decode()) result = json.loads(response.body.decode()) self.assertEqual("group name", result["name"], result) self.assertEqual(self.team.id, result["team_id"], result) group = TeamMemberGroup.get_or_none(team=self.team, id=result["id"]) self.assertIsNotNone(group, "新建分组失败") self.assertEqual(0, group.members.count())
def get(self, team_id): """ 获取俱乐部分组 Args: team_id: int """ team = Team.get_or_404(id=team_id) self.has_read_permission(team) query = TeamMemberGroup.select().where(TeamMemberGroup.team == team) page = self.paginate_query(query) data = self.get_paginated_data(page=page, alias="groups") self.write(data)
def test_patch_with_group(self): group = TeamMemberGroup.create(team=self.team, name="test group") self.team.add_member(user_id=self.user.id) member = TeamMember.get(team=self.team, user=self.user) url = self.MEMBER_DETAIL.format(team_id=self.team.id, user_id=self.user.id) body = { "group_id": group.id, } self.auth_user = self.team_owner response = self.fetch(url, method="PATCH", body=json.dumps(body)) self.assertEqual(204, response.code, response.body) updated = TeamMember.get(user=self.user.id) self.assertEqual(updated.group_name, group.name, updated) self.assertEqual(updated.groups.first(), group, updated.groups.first())
def delete(self, team_id, group_id): """ 删除分组 Args: team_id: group_id: """ team = Team.get_or_404(id=team_id) group = TeamMemberGroup.get_or_404(id=group_id, team=team) self.has_delete_permission(team) self.group_allow_delete(group) self.set_status(204)
def delete(self): group_id = self.get_argument("group_id") group = TeamMemberGroup.get_or_404(id=group_id) if group.team != self.current_team: raise HttpForbiddenError() if TeamMember.select().where(TeamMember.team == self.current_team, TeamMember.group_name == group.name).count() > 0: raise HttpForbiddenError("此分组有会员,不能删除") group.delete_instance() self.write_success()
def post(self, team_id): """ 新建俱乐部分组 Args: team_id: Returns: """ team = Team.get_or_404(id=team_id) self.has_read_permission(team) form = self.validated_arguments group = TeamMemberGroup.create(team=team, **form) self.set_status(201) self.write(group.info)
def initial_data(self): self.team_owner = User.create(name='test_activity') self.team = Team.create(name='club_test_activity', owner_id=self.team_owner.id) self.user = self.creator = User.create(name='activity_creator') self.activity = Activity.create(team=self.team, creator=self.creator, price='10', vip_price='8', leader=self.creator, title='just a test', description='description', start_time='3000-01-01 00:00:01', end_time='3000-12-31 23:59:59') self.default_group = TeamMemberGroup.create(team=self.team, name="default group")
def get(self): team = self.current_team groups = TeamMemberGroup.select().where(TeamMemberGroup.team == team) query = TeamMember.select(TeamMember, User).join( User, on=(User.id == TeamMember.user).alias("user")).where( TeamMember.team == team) # 按备注名字搜索 kw = self.get_argument("kw", None) if kw: query = query.where((TeamMember.nick.contains(kw)) | (User.mobile == kw)) sort = self.get_argument("sort", "") if sort in self._VALID_SORT_TYPE: if sort == "-name": query = query.order_by(TeamMember.nick.desc()) elif sort == "-credit": query = query.order_by(TeamMember.credit.desc()) elif sort == "credit": query = query.order_by(TeamMember.credit.asc()) filter_by_role = self.get_argument("role", None) if filter_by_role in self._VALID_ROLE: if filter_by_role == "member": pass elif filter_by_role == "vip": query = query.where(TeamMember.is_vip == True) elif filter_by_role == "black_list": pass # 分 group 查询 group_name = self.get_argument('group', None) if group_name: query = query.where(TeamMember.group_name == group_name) members = self.paginate_query(query) self.render("member/list.html", groups=groups, groups_json=[g.info for g in groups], members=members)
def post(self): action = self.get_argument("action") user_id = self.get_argument("user_id") if action == "change_group": group_name = self.get_argument("group_name") member = Team.get_member(self.current_team.id, user_id) TeamMember.update(group_name=group_name).where( TeamMember.team == self.current_team, TeamMember.user == user_id).execute() # 计算新分组成员数量 TeamMemberGroup.update_members_count(self.current_team.id, group_name) # 计算旧分组成员数量 TeamMemberGroup.update_members_count(self.current_team.id, member.group_name) elif action == "apply": user = User.get_or_404(id=user_id) self.current_team.apply(user) elif action == "remove_member": member = Team.get_member(self.current_team.id, user_id) if member: if not member.can_leave(): raise HttpForbiddenError("该会员有财务没有结清,不能踢出") user = User.get_or_404(id=user_id) self.current_team.leave(user) TeamMemberGroup.update_members_count(self.current_team.id, member.group_name) elif action == "recharge": amount = intval(self.get_argument("amount")) freetimes = intval(self.get_argument("freetimes")) remark = self.get_argument("remark") member = Team.get_member(self.current_team.id, user_id) if member: TeamMember.change_credit( self.current_team.id, user_id=user_id, change_type=1, # 充值类型为后台操作 change_amount=amount, free_times=freetimes, operator_id=self.current_user.id, note=remark) elif action == "deduction": amount = intval(self.get_argument("amount")) freetimes = intval(self.get_argument("freetimes")) remark = self.get_argument("remark") member = Team.get_member(self.current_team.id, user_id) if member: TeamMember.change_credit( self.current_team.id, user_id=user_id, change_type=1, # 充值类型为后台操作 change_amount=0 - amount, free_times=0 - freetimes, operator_id=self.current_user.id, note=remark) self.write_success()
def main(): settings = setting_from_object(local_settings) if settings.get('debug', False): options.logging = "debug" tornado.options.parse_command_line() if options.debug: settings['debug'] = True if options.cmd == 'createall': """Create all database tables""" create_app(settings) if not Sport.table_exists(): Sport.create_table() if not User.table_exists(): User.create_table() if not Team.table_exists(): Team.create_table() if not TeamMemberGroup.table_exists(): TeamMemberGroup.create_table() if not TeamMember.table_exists(): TeamMember.create_table() if not Activity.table_exists(): Activity.create_table() if not Admin.table_exists(): Admin.create_table() if not Match.table_exists(): Match.create_table() if not MatchStatus.table_exists(): MatchStatus.create_table() models = find_subclasses(BaseModel) for model in models: if model._meta.db_table.startswith("__"): print(("table skip: " + model._meta.db_table)) elif model.table_exists(): print(('table exist: ' + model._meta.db_table)) else: model.create_table() print(('table created: ' + model._meta.db_table)) print('create all [ok]') elif options.cmd == 'createadmin': app = create_app(settings) Admin.create(username="******", password=Admin.create_password("admin"), mobile="17088888888", email="*****@*****.**", name="Admin", is_superadmin=True, state=1) elif options.cmd == 'createclient': app = create_app(settings) Client.create(name="ios", key=create_token(32), secret=create_token(32)) elif options.cmd == 'run_as_wsgi': logging.info('server started. port %s' % options.port) import gevent.wsgi app = create_app(settings) # 转换成wsgi实例 wsgi_app = tornado.wsgi.WSGIAdapter(app) http_server = gevent.wsgi.WSGIServer(('', options.port), wsgi_app) http_server.serve_forever() elif options.cmd == 'runserver': tornado.platform.asyncio.AsyncIOMainLoop().install() ioloop = asyncio.get_event_loop() app = create_app(settings) app.listen(options.port, xheaders=True) print("running...") ioloop.run_forever() elif options.cmd == "fix_notify": from fix_script.fix_match_start_nofity import fix_match_start_notify fix_match_start_notify()