def test_get_by_id(self): test_date = datetime.now() redeem_code = self.create_redeem_code(test_date, test_date) get_redeem_code = RedeemCode.get(redeem_code.id_) assert isinstance(get_redeem_code, RedeemCode) assert redeem_code.id_ == get_redeem_code.id_
def redeem(): """使用兑换码兑换礼包 使用本接口,客户端必须有权以 ``user_info`` 作为 scope. :request: :class:`.RedeemCodeVerifySchema` :response: :class:`.WelfareInfoSchema` :status 200: 兑换成功 :status 403: 兑换失败 """ redeem_code_schema = RedeemCodeVerifySchema(strict=True) welfare_info_schema = WelfareInfoSchema(strict=True) result = redeem_code_schema.load(request.get_json(force=True)) redeem_code = RedeemCode.get_by_code(result.data['redeem_code'].upper()) if redeem_code: try: redeem_code.redeem(request.oauth.user) welfare_info = welfare_info_schema.dump( redeem_code.activity.reward_welfare_package_kind. welfare_summary) return jsonify(success=True, data=welfare_info.data) except RedeemCodeException as e: abort(403, unicode(e)) abort(404, u'您输入的兑换码不存在')
def redeem(): if request.method == 'POST': redeem_code = request.form['redeem_code'].upper() redeem_code = RedeemCode.get_by_code(redeem_code) if redeem_code: try: redeem_code.redeem(g.user) welfare_info = redeem_code.activity.reward_welfare_package_kind.welfare_summary anchor = None if welfare_info.firewood_info else 'coupon' welfare_info = { 'coupon_info': [vars(item) for item in welfare_info.coupon_info], 'firewood_info': [vars(item) for item in welfare_info.firewood_info] } welfare_detail = { 'redirect_url': url_for('welfare.index', _anchor=anchor), 'welfare_info': welfare_info } return jsonify(welfare_detail=welfare_detail) except (NotFoundError, RedeemCodeUsedError, RedeemCodeExpiredError, RedeemCodeIneffectiveError, RedemptionBeyondLimitPerCodeError, RedemptionBeyondLimitPerUserError, RedemptionBeyondLimitPerUserError) as e: message = unicode(e) else: message = u'您输入的兑换码不存在' return jsonify(error=message), 403
def main(): bcolors.run('Add redeem code.') effective_time = datetime.now() - timedelta(days=1) expire_time = datetime.now() + timedelta(days=30) try: # 创建100个可被使用一次的兑换码 RedeemCode.create_multi_codes(fanmeeting_gold.id_, RedeemCode.Kind.normal_package.value, u'测试用兑换码', 1, 100, effective_time, expire_time) # 创建一个可使用100次的兑换码 RedeemCode.create(fanmeeting_silver.id_, RedeemCode.Kind.normal_package.value, u'测试用兑换码', 100, None, effective_time, expire_time) bcolors.success('Init redeemcode done.') except Exception as e: bcolors.fail('Init redeemcode fail: %s.' % e)
def create_redeem_code(self): from core.models.redeemcode.redeemcode import RedeemCode effective_time = datetime.now() expire_time = effective_time + timedelta(days=1) redeem_code = RedeemCode.create(1, 1, u'测试', 1, None, effective_time, expire_time) return redeem_code
def test_get_by_activity(self): test_date = datetime.now() self.create_redeem_code(test_date, test_date) activity_id = '1' activity_list = RedeemCode.get_by_activity_id(activity_id) assert isinstance(activity_list, list) for activity in activity_list: assert isinstance(activity, RedeemCode)
def test_redeem_code(group_add_member, package_unpack, client, oauth_token, identity): url = 'api/v1/redeemcode/code' client.load_token(oauth_token) date_begin = datetime.now() date_end = date_begin + timedelta(days=1) redeem_code = RedeemCode.create(1, 1, u'测试', 1, None, date_begin, date_end) redeem_code3 = RedeemCode.create(1, 1, u'测试', 1, None, date_begin, date_end) expire_code = RedeemCode.create(1, 1, u'测试', 1, None, date_begin, date_begin) ineffective_code = RedeemCode.create(1, 1, u'测试', 1, None, date_end, date_begin) # empty redeem_code r = client.post(url, data={'redeem_code': ''}) assert r.data['messages']['redeem_code'][0] == u'请输入兑换码' # no redeem_code r = client.post(url, data={'redeem_code': 'TEST1234'}) assert r.data['messages']['_'][0] == u'您输入的兑换码不存在' # expire redeem_code r = client.post(url, data={'redeem_code': expire_code.code}) assert r.data['messages']['_'][0] == u'您输入的兑换码已失效' # ineffective redeem_code r = client.post(url, data={'redeem_code': ineffective_code.code}) assert r.data['messages']['_'][0] == u'您输入的兑换码未生效,请在活动期间使用' # normal redeem_code r = client.post(url, data={'redeem_code': redeem_code.code}) assert r.status_code == 200 assert r.data['success'] is True # used redeem_code r = client.post(url, data={'redeem_code': redeem_code.code}) assert r.status_code == 403 assert r.data['messages']['_'][0] == u'您输入的兑换码已使用' # RedemptionBeyondLimitPerUser redeem_code3 r = client.post(url, data={'redeem_code': redeem_code3.code}) assert r.status_code == 403 assert r.data['messages']['_'][0] == u'您使用兑换码的次数已超过本次活动上限'
def create_redeem_code(self, effective_time, expire_time): redeem_code = RedeemCode.create(1, 1, u'测试', 1, None, effective_time, expire_time) return redeem_code