def patch(self, team_id): """ 修改俱乐部信息; 俱乐部徽标额外接口修改 """ team = Team.get_or_404(id=team_id) self.has_update_permission(team) form = self.validated_arguments Team.update(**form).where(Team.id == team.id).execute() self.set_status(204)
def gen_next_activity(activity_id): """ 生成下期活动 """ if Activity.select().where(Activity.parent_id == activity_id).exists(): logging.debug("已经存在") return activity = Activity.get_or_404(id=activity_id) # 时间相关都顺延一周 delta = timedelta(days=7) if activity.repeat_end and \ datetime.now() + delta > activity.repeat_end: logging.debug("活动已经结束自动循环") return activity = copy.copy(activity) activity.id = None activity.members_count = 0 activity.comments_count = 0 activity.recommend_time = 0 activity.recommend_region = 0 if activity.join_start: activity.join_start += delta if activity.join_end: activity.join_end += delta activity.cancelled = None activity.cancel_reason = "" activity.finished = None activity.start_time += delta activity.end_time += delta activity.online_paid_amount = 0 activity.credit_paid_amount = 0 activity.cash_paid_amount = 0 activity.free_times_amount = 0 activity.created = datetime.now() activity.updated = datetime.now() activity.state = Activity.ActivityState.opening activity.parent_id = activity_id activity.save() # 更新俱乐部活动数 Team.update_activities_count(activity.team_id)
def post(self): action = self.get_argument("action") team_id = self.get_argument("team_id") if action == "pass": Team.update( state=1, updated=datetime.now() ).where( Team.id == team_id ).execute() self.write_success()
def initial_database(): """ 初始化数据库, 重建旧表 创建表 Returns: """ # TODO: 重复代码抽象 from yiyun.helpers import find_subclasses from yiyun.models import BaseModel, Sport, User, Team, Activity # drop old database and create new; test_settings = reject_settings() test_db = test_settings['db_name'] raw_sql = ("drop database {test_db};" "create database {test_db};" "use {test_db};") # create new tables BaseModel._meta.database.execute_sql(raw_sql.format(test_db=test_db)) models = find_subclasses(BaseModel) 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 Activity.table_exists(): Activity.create_table() for model in models: if model._meta.db_table.startswith("__"): logging.debug(("table skip: " + model._meta.db_table)) elif model.table_exists(): logging.debug(('table exist: ' + model._meta.db_table)) else: model.create_table() logging.debug(('table created: ' + model._meta.db_table)) logging.debug('create all [ok]')
def get(self): """获取俱乐部列表""" keyword = self.get_argument("kw", "") state = intval(self.get_argument("state", -1)) sort = intval(self.get_argument("sort", 0)) query = Team.select() # 这里限制的需要是 俱乐部创建者所在的位置进行过滤 而不是俱乐部本身的位置 is_restrict_by_areas = self.current_user.is_restrict_by_areas() if is_restrict_by_areas: query = query.join( User, on=(User.id == Team.owner_id) ).where( User.province << self.current_user.valid_manage_provinces ) if state in (0, 1): query = query.where(Team.state == state) if keyword: query = query.where(Team.name.contains(keyword)) query = query.order_by(Team.id.desc()) teams = self.paginate_query(query) self.render("team/list.html", teams=teams, )
def test_patch_team(self): url = self.OBJECT_PATH.format(team_id=self.team.id) body = { "name": "new test create team", "description": "new description", "notice": "new notice", "province": "四川省", "city": "成都市", "address": "天府四街", "contact_person": "new contact person", "contact_phone": "new contact phone", "lat": 123.0, "lng": 123.0, "open_type": 1, "state": 2, } 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 = Team.get(id=self.team.id) self.assertEqual(0, updated.state, "状态被修改了") body.pop("state") for k, v in body.items(): self.assertEqual(v, getattr(updated, k), k)
def new_match_status(self, match_id: int): """ 主办方发布新战报时向赛事成员推送信息 :param self: :param match_id: :return: """ match = Match.get(id=match_id) # type: Match team = Team.get(id=match.team_id) # type: Team members = MatchService.members(match, state=MatchMember.MatchMemberState.normal) infos = [] for member in members: infos.append({"mobile": member.mobile, "userId": member.user_id}) message = NewMatchScheduleMessage( user_infos=infos, match_id=match_id, match_name=match.title, sponsor_name=team.name, sponsor_pic_url=team.get_cover_url(size="medium")) pt = Parteam(app.settings["parteam_api_url"]) if not pt.push_message(message): self.retry(ParteamRequestError("调用派队推送接口失败"))
def validate_activity_visible(self, activity: Activity): """ 校验是否允许报名 Args: activity: """ if activity.visible == 0: # 所有人可加入 return True else: team = activity.team if activity.allow_groups: # 特定组成员可加入 team_member = Team.get_member(team_id=team.id, user_id=self.current_user.id) if team_member.group_name in activity.allow_groups: return True else: raise ApiException(403, "该活动之允许特定分组成员加入, " "你不是该组成员") if team.is_member(self.current_user.id): # 会员均可加入 return True else: raise ApiException(403, "该活动仅允许会员参加, 你还不是该俱乐部会员")
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.order = OrderService.new_order(10, self.team, self.user, TeamOrder.OrderType.ACTIVITY, TeamOrder.OrderPaymentMethod.WXPAY, self.activity.id, title="UserOrderTest" ) self.activity.add_member(self.user.id, users_count=1, price=10, free_times=0, total_fee=10, order_id=self.order.id, order_no=self.order.order_no, payment_method=TeamOrder.OrderPaymentMethod.WXPAY, payment_state=TeamOrder.OrderState.TRADE_BUYER_PAID, state=TeamMember.TeamMemberState.normal)
def post(self): form = LoginVerifyCodeForm(self.arguments) fail = False if form.validate() and self.validate_verify_code(form): user = User.get_or_none(mobile=self.get_argument("mobile")) if user: remember_me = self.get_argument("remember", "off") if remember_me == "on": expires_days = 30 else: expires_days = None self.login(user, expires_days) team = Team.get_or_none(owner_id=user.id) if team is None: self.redirect(self.reverse_url("club_create")) return if team.state == 0: self.redirect(self.reverse_url("club_wait_approve")) return if self.next_url: self.redirect(self.next_url) else: self.redirect(self.reverse_url("club_home")) return fail = True self.render("login-by-sms.html", form=form, fail=fail)
def post(self): form = self.validated_arguments team = Team.get(id=form.pop("team_id")) self.has_create_permission(team) fields = copy.copy(form) # 如果有地址信息, 获取 GPS 信息 if "city" in form and "address" in form: logging.debug("有 `city` 和 `address` 开始调用远程接口获取 GPS 信息") geocode = yield self.get_geocode(city=form["city"], address=form["address"]) logging.debug("获取到 geocode: {0}".format(geocode)) if geocode.get("geocodes", []): location = geocode['geocodes'][0]['location'].split(",") fields["lat"] = location[1] fields["lng"] = location[0] fields["geohash "] = geohash.encode(float(location[1]), float(location[0])) # TODO: 处理循环类型 if "repeat_type" in form: if form["repeat_type"] == "week": fields["week_day"] = form["start_time"].weekday() + 1 elif form["repeat_type"] == "month": fields["month_day"] = form["start_time"].day activity = Activity.create(team=team, creator=self.current_user, leader=self.current_user, **fields) self.set_status(201) self.write(ActivitySerializer(instance=activity).data)
def post(self): form = LoginForm(self.arguments) if form.validate(): user = self.have_user(form.username.data) if user and User.check_password(user.password, form.password.data): remember_me = self.get_argument("remember", "off") if remember_me == "on": expires_days = 30 else: expires_days = None self.login(user, expires_days) team = Team.get_or_none(owner_id=user.id) if team is None: return self.redirect(self.reverse_url("club_create")) elif team.state == 0: return self.redirect(self.reverse_url("club_wait_approve")) elif self.next_url: return self.redirect(self.next_url) else: return self.redirect(self.reverse_url("club_home")) messages = [('danger', '登录失败:账号或密码不正确')] self.render("login.html", form=form, messages=messages)
def patch(self, team_id, user_id): """修改会员信息, 如果有分组信息变化, 需要额外处理""" form = self.validated_arguments if not form: raise ApiException(400, "填写需要修改的属性和值") team = Team.get_or_404(id=team_id) member = TeamMember.get_or_404(user=user_id, team=team_id) self.has_update_permission(member) group = None group_id = form.pop("group_id", None) if group_id: group = self.validate_group_id(team, group_id) with self.db.transaction(): # 如果有分组修改, 额外处理 if group: TeamMemberGroupService.add_member(group, member) form["group_name"] = group.name logging.debug(form) TeamMember.update(**form)\ .where(TeamMember.id == user_id, TeamMember.team == team_id)\ .execute() self.set_status(204)
def match_start(self, match_id: int): """ 赛事开始前, 调用派队消息推送接口 :param self: celery task Context :param match_id: :return: """ match = Match.get(id=match_id) # type: Match team = Team.get(id=match.team_id) # type: Team members = MatchService.members(match, state=MatchMember.MatchMemberState.normal) infos = [] for member in members: infos.append({"userId": member.user_id, "mobile": member.mobile}) message = MatchStartMessage( user_infos=infos, match_id=match_id, match_name=match.title, sponsor_name=team.name, sponsor_pic_url=team.get_cover_url(size="medium")) pt = Parteam(app.settings["parteam_api_url"]) if not pt.push_message(message=message): self.retry(exc=ParteamRequestError("调用派队推送接口失败"))
def get(self, team_id): team = Team.get_or_404(id=team_id) if not team.is_member(user_id=self.current_user.id): raise ApiException(400, "未加入俱乐部") team.leave(self.current_user) self.set_status(204)
def get(self): team = Team.get_or_none(owner_id=self.current_user.id) if team is None: return self.redirect(self.reverse_url("club_create")) elif team.state == 1: return self.redirect(self.reverse_url("club_home")) else: self.render("club/wait_approve.html")
def test_patch_team_permission_deny(self): url = self.OBJECT_PATH.format(team_id=self.team.id) body = {"name": "new name"} self.auth_user = User.create(name="permission deny for patch team") response = self.fetch(url, method="PATCH", body=json.dumps(body)) self.assertEqual(403, response.code, response.body.decode) updated = Team.get(id=self.team.id) self.assertNotEqual("new name", updated.name, "权限错误, 但是跟新成功了")
def post(self): form = CreateActivityFrom(self.arguments, team=self.current_team) if form.validate(): activity = Activity() form.populate_obj(activity) need_fields = self.get_arguments("need_fields") for field in need_fields: setattr(activity, field, True) geocode = yield self.get_geocode(activity.city, activity.address) if geocode.get("geocodes", []): location = geocode['geocodes'][0]['location'].split(",") activity.lat = location[1] activity.lng = location[0] activity.geohash = geohash.encode(float(location[1]), float(location[0])) if activity.repeat_type == "week": activity.week_day = activity.start_time.weekday() + 1 elif activity.repeat_type == "month": activity.month_day = activity.start_time.day activity.team = self.current_team activity.creator = self.current_user activity.save() # 更新俱乐部活动数量 Team.update_activities_count(self.current_team.id) self.redirect(self.reverse_url("club_activity_list")) return province = self.get_argument("province", None) if province: form.city.choices = ChinaCity.get_cities(province) self.render("activity/new.html", form=form, cities=ChinaCity.get_cities())
def get(self): """获取俱乐部列表""" query = Team.select() query = self.filter_query(query) page = self.paginate_query(query) data = self.get_paginated_data(page=page, alias='teams', serializer=SimpleTeamSerializer) self.write(data)
def check_create_throttle(self): """ 一个用户只允许创建一个俱乐部 """ count = Team.select()\ .where(Team.owner_id == self.current_user.id)\ .count() if count == 0: return True raise ApiException(400, "已创建的俱乐部数量 [{0}] 超限".format(count))
def get(self, team_id, user_id): team = Team.get_or_404(id=team_id) member = TeamMember.get_or_404(user=user_id, team=team_id) if self.current_user == member.user or self.current_user == team.owner: serializer = InsecureMemberSerializer else: serializer = SimpleMemberSerializer self.write(serializer(instance=member).data)
def get(self): announces = Announce.select().order_by(Announce.id.desc()) announces = self.paginate_query(announces) members_count = Team.get_members_count(self.current_team.id) self.render("home.html", announces=announces, members_count=members_count)
def post(self, *args, **kwargs): # 检查创建数量是否超限 self.check_create_throttle() form = self.validated_arguments team = Team.create(owner_id=self.current_user.id, **form) self.set_status(201) self.write(TeamSerializer(instance=team).data)
def post(self, team_id: int): """ 关注俱乐部 :param team_id: :return: """ team = Team.get_or_404(id=team_id) # type: Team if team.get_follower(user_id=self.current_user.id): raise ApiException(422, "您已关注俱乐部, 无须重复关注") team.add_follower(user_id=self.current_user.id) self.set_status(204, "关注成功")
def put(self, team_id): team = Team.get_or_404(id=team_id) self.has_update_permission(team) if "icon" not in self.request.files: raise ApiException(400, "请选择文件") to_bucket = self.settings['qiniu_avatar_bucket'] to_key = "team:%s%s" % (self.current_user.id, time.time()) to_key = hashlib.md5(to_key.encode()).hexdigest() icon_key = self.upload_file( "icon", to_bucket=to_bucket, to_key=to_key, ) team.icon_key = icon_key team.save() updated = Team.get(id=team.id) self.write(updated.icon)
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')
def _approve(self, application_id): application = TeamCertifyApplication.get_or_404(id=application_id) team = Team.get_or_404(id=application.team_id) with(self.db.transaction()): application.set_approved() application.save() team.verified = True team.save() self.write_success()
def _disapprove(self, application_id, reason=""): application = TeamCertifyApplication.get_or_404(id=application_id) team = Team.get_or_404(id=application.team_id) with(self.db.transaction()): application.set_disapproved() application.save() team.verified = False team.verified_reason = reason team.save() self.write_success()
def post(self): form = ApplyCashFrom(self.arguments) settings = TeamSettings.get_or_none(team=self.current_team.id) if form.validate() and self.validate_amount(form) and \ settings and settings.cash_ready(): with(self.db.transaction()): team = Team.select().where( Team.id == self.current_team.id ).for_update().get() cash_amount = decimalval(self.get_argument("amount")) TeamCashLog.create( team_id=team.id, amount=cash_amount, cash_account_type=settings.cash_type, cash_account=settings.cash_account, cash_name=settings.cash_username, order_no=TeamCashLog.get_new_order_no() ) # 将收入打到俱乐部账上 Team.update( credit=Team.credit - cash_amount, cashed_amount=Team.cashed_amount + cash_amount, updated=datetime.now() ).where( Team.id == team.id ).execute() self.redirect(self.reverse_url("club_cash_logs")) return self.render("orders/apply_cash.html", form=form, settings=settings )
def delete(self, team_id: int): """ 取消关注 :param team_id: :return: """ # user_id = self.get_query_argument("user_id", None) user_id = self.current_user.id team = Team.get_or_404(id=team_id) # type: Team if not team.get_follower(user_id=user_id): raise ApiException(422, "您未关注俱乐部") team.delete_follower(user_id=user_id) self.set_status(204, "取消关注成功")