def get_available_coupons_and_redpackets(product_id, wrapped_product_id=None): """产品可用礼券和红包 :param product: 产品ID. :param wrapped_product_id: 包装后产品ID. :reqheader Authorization: OAuth 2.0 Bearer Token :status 200: 返回 :class:`.CouponCollectionSchema` :status 200: 返回 :class:`.RedPacketsSchema` """ coupon_schema = CouponCollectionSchema(strict=True) redpacket_schema = RedPacketsSchema(strict=True) xm_product = XMFixedDuedayProduct.get(product_id) if xm_product: welfare_data = dict() coupons = [ c for c in g.available_coupons if c.is_available_for_product(xm_product) ] welfare_data.update(coupon_schema.dump({'coupons': coupons}).data) welfare_data.update( redpacket_schema.dump({ 'balance': g.firewood_flow.balance, 'start_amount': decimal.Decimal(200), 'deduct_amount': decimal.Decimal(1), 'is_accepting_bonus': xm_product.is_accepting_bonus, 'unavailable_text': '该产品暂无可用红包', 'firewood_rate': decimal.Decimal(0) }).data) return jsonify(success=True, data=welfare_data) product = ZhiwangProduct.get(product_id) wrapped_product = ZhiwangWrappedProduct.get(wrapped_product_id) welfare_data = dict() if wrapped_product_id is not None and not wrapped_product: abort(404) coupons = [ c for c in g.available_coupons if c.is_available_for_product(wrapped_product or product) ] welfare_data.update(coupon_schema.dump({'coupons': coupons}).data) welfare_data.update( redpacket_schema.dump({ 'balance': g.firewood_flow.balance, 'start_amount': decimal.Decimal(200), 'deduct_amount': decimal.Decimal(1), 'is_accepting_bonus': (wrapped_product or product).is_accepting_bonus, 'unavailable_text': '该产品暂无可用红包', 'firewood_rate': decimal.Decimal(0) }).data) return jsonify(success=True, data=welfare_data)
def order(): """认购产品""" # 基础数据 due_date = None profile = ZhiwangProfile.add(g.user.id_) if g.user else abort(401) bankcard_id = request.form.get('bankcard_id', type=int) or abort(400) bankcard = profile.bankcards.get(bankcard_id) or abort(400) if Partner.zw not in bankcard.bank.available_in: abort(400, 'unsupported bankcard') coupon_id = request.form.get('coupon_id', type=int, default=0) coupon = Coupon.get(coupon_id) if coupon and not coupon.is_owner(g.user): abort(403) product_id = request.form.get('product_id', type=int, default=0) product = ZhiwangProduct.get(product_id) or abort(404) wrapped_product_id = request.form.get('wrapped_product_id', type=int) wrapped_product = ZhiwangWrappedProduct.get(wrapped_product_id) if wrapped_product and not wrapped_product.is_qualified(g.user.id_): abort(401) # 选择表单验证 form = select_subscribe_form(wrapped_product or product)() if not form.validate(): return jsonify(r=False, error=form.errors.values()[0][0]) # 只有房贷宝产品才有到期日 if product.product_type is ZhiwangProduct.Type.fangdaibao: due_date = wrapped_product.due_date if wrapped_product else form.data[ 'due_date'] # 设置购买卡为默认卡 profile.bankcards.set_default(bankcard) # 申购产品 try: order = subscribe_product( g.user, product, bankcard, form.data['order_amount'], form.data['pay_amount'], due_date, wrapped_product=wrapped_product, coupon=coupon, pocket_deduction_amount=form.data['pocket_deduction_amount']) except (SoldOutError, SuspendedError, OffShelfError, OutOfRangeError, InvalidIdentityError, ExceedBankAmountLimitError, SubscribeProductError, CouponBusinessError, FirewoodBusinessError) as e: return jsonify(r=False, error=unicode(e)) payment_url = url_for('.pay', order_code=order.order_code, pay_code=order.pay_code) return jsonify(r=True, payment_url=payment_url)
def index(): cur_path = 'savings' # 个人信息 saving_manager = SavingsManager(g.user.id_) coupon_manager = CouponManager(g.user.id_) # 礼券 coupons = coupon_manager.deduplicated_available_coupons # 指旺产品 fdb_products, cls_products, ncm_products = [], [], [] for p in ZhiwangProduct.get_all(): if p.product_type is ZhiwangProduct.Type.fangdaibao: fdb_products.append(p) elif p.product_type is ZhiwangProduct.Type.classic: # 指旺暂不显示中长期产品 if p.profit_period['min'] not in ( ProfitPeriod(90, 'day'), # 临时决定不在Web上显示90天产品 ProfitPeriod(180, 'day'), ProfitPeriod(270, 'day'), ProfitPeriod(365, 'day')): cls_products.append(p) # 2016年五月一日下线指旺自选到期日产品 if not zhiwang_fdb_product_on_switch.is_enabled: fdb_products = [] xm_products = XMFixedDuedayProduct.get_all() has_identity = has_real_identity(g.user) # 合作方授权(检查是否需要验证(尝试自动注册)指旺账号 zhiwang_account = ZhiwangAccount.get_by_local(g.user.id_) can_quick_reg_zhiwang = has_identity and not zhiwang_account # 合作方授权(检查是否需要验证(尝试自动注册)新米账号 xm_account = XMAccount.get_by_local(g.user.id_) can_quick_reg_xm = has_identity and not xm_account context = { 'cur_path': cur_path, 'coupons': coupons, 'ncm_products': ncm_products, 'fdb_products': fdb_products, 'cls_products': cls_products, 'xm_products': xm_products, 'saving_manager': saving_manager, 'plan_amount': g.hoard_profile.plan_amount, 'can_quick_reg_xm': can_quick_reg_xm, 'can_quick_reg_zhiwang': can_quick_reg_zhiwang } return render_template('savings/mine.html', **context)
def publicity(): # 指旺活动产品 products = [ p for p in ZhiwangProduct.get_all() if p.product_type is ZhiwangProduct.Type.fangdaibao ] products = sorted(products, key=attrgetter('final_due_date')) product = first((p for p in products if p.in_stock), None) can_quick_reg_zhiwang = None if g.user: zhiwang_account = ZhiwangAccount.get_by_local(g.user.id_) can_quick_reg_zhiwang = not zhiwang_account and has_real_identity( g.user) return render_template('savings/publicity.html', cur_path='savings', product=product, can_quick_reg_zhiwang=can_quick_reg_zhiwang)
def purchase(product_id, wrapped_product_id=None): cur_path = 'record' partner = Partner.zw agreement_url = url_for('savings.landing.agreement_zhiwang') banks = iter_banks(g.user.id_) bankcards = g.zhiwang_profile.bankcards.get_all() raw_product = ZhiwangProduct.get(product_id) or abort(404) spring_gift = SpringGift.get_by_user(g.user) wrapped_product = ZhiwangWrappedProduct.get(wrapped_product_id) if wrapped_product_id is not None and not wrapped_product: abort(404) coupons = [ c.to_dict() for c in g.coupon_manager.available_coupons if c.is_available_for_product(wrapped_product or raw_product) ] context = { 'coupons': coupons, 'banks': banks, 'bankcards': bankcards, 'partner': partner, 'user_balance': g.firewood_flow.balance, 'cur_path': cur_path, 'product': raw_product, 'spring_gift': spring_gift, 'agreement_url': agreement_url } if wrapped_product and wrapped_product.is_qualified(g.user.id_): context.update(dict(product=wrapped_product)) context['product_type'] = 'newcomer' return render_template('savings/order.html', **context) if raw_product.product_type is ZhiwangProduct.Type.fangdaibao: context['annual_rate_layers'] = raw_product.annual_rate_layers context['product_type'] = 'ladder' elif raw_product.product_type is ZhiwangProduct.Type.classic: context['product_type'] = 'regular' else: abort(404) return render_template('savings/order.html', **context)
def main(): """Downloads the product data of Zhiwang.""" with app.app_context(): response = zhiwang.product_list() all_products = [] for product_info in response.products: base_product = ZhiwangProduct.add(product_info) wrapped_products = ZhiwangWrappedProduct.get_multi_by_raw( base_product.product_id) all_products.append(base_product) all_products.extend(wrapped_products) # 注意: 如果12点后依然没有产品,则12点后创建的产品将只能由运营人员手动上架 hour = datetime.now().hour for p in all_products: if hour == 0 and p.is_taken_down and not p.is_either_sold_out: p.is_taken_down = False
def init(): """Downloads the product data of Zhiwang.""" try: response = zhiwang.product_list() except RemoteError as e: bcolors.fail('Fetching Zhiwang Product Failed:%s' % e) else: all_products = [] for product_info in response.products: base_product = ZhiwangProduct.add(product_info) bcolors.success('ZhiwangProduct: %s' % base_product.name) wrapped_products = ZhiwangWrappedProduct.get_multi_by_raw(base_product.product_id) for wp in wrapped_products: bcolors.success('ZhiwangWrappedProduct: %s' % wp.name) all_products.append(base_product) all_products.extend(wrapped_products) for p in all_products: # 测试环境默认上架 p.is_taken_down = False
def mine(): # 攒钱助手 savings_manager = SavingsManager(g.user.id_) savings_products = ZhiwangProduct.get_all() vendor = Vendor.get_by_name(Provider.sxb) sxb_products = Product.get_products_by_vendor_id(vendor.id_) xm_products = XMFixedDuedayProduct.get_all() # 零钱包 wallet_dashboard = PublicDashboard.today() wallet_account = WalletAccount.get_by_local_account(g.user, zhongshan) if wallet_account: wallet_profile = UserDashboard.today(wallet_account) wallet_has_transaction = bool( WalletTransaction.get_ids_by_account(wallet_account.id_)) else: wallet_profile = None wallet_has_transaction = False # 规划书 report = Report.get_latest_by_plan_id(g.plan.id) if g.plan else None if not (report and report.status >= REPORT_STATUS.interdata): return render_template('/mine/center_unplanned.html', **locals()) if int(report.formula_ver) < int(FORMULA_VER): regen_log(report, 'start regenerate inter data') cal_intermediate_data(report, force=True, log=regen_log) report.update_formula_ver(FORMULA_VER) regen_log(report, 'success regenerate inter data FV:%s' % report.formula_ver) inter_data = report.inter_data locals().update(inter_data) cur_path = 'center' return render_template('/mine/center.html', **locals())
def products(): """攒钱助手待售产品. :query partner: 可选参数,按合作方支持情况限制返回结果. 目前可为: - ``"zw"`` 指旺 - ``"xm"`` 新米 :reqheader Authorization: OAuth 2.0 Bearer Token :reqheader If-None-Match: 客户端缓存的 ETag :resheader ETag: 客户端可缓存的 ETag :status 304: 客户端缓存未过期, 无需返回数据 :status 200: 返回 :class:`.ProductSchema` 列表 """ from .products.consts import sale_display_text product_schema = ProductSchema(strict=True, many=True) partners = frozenset(request.args.getlist('partner')) profile = SavingsManager(request.oauth.user.id_) profile.refresh_profile() services = [] def product_sale_status_to_text(product): is_early_morning_product = isinstance( product, (XMProduct, ZhiwangWrappedProduct)) if product.in_stock: return sale_display_text['on_sale'] elif product.is_taken_down: if is_early_morning_product: return sale_display_text['early_morning_off_sale'] return sale_display_text['late_morning_off_sale'] elif product.is_either_sold_out: if is_early_morning_product: return sale_display_text['early_morning_sold_out'] return sale_display_text['late_morning_sold_out'] if 'zw' in partners and zhiwang_fdb_product_on_switch.is_enabled: zw_services = [] for s in ZhiwangProduct.get_all(): if s.product_type is ZhiwangProduct.Type.fangdaibao: zw_services.append(s) elif s.product_type is ZhiwangProduct.Type.classic: if s.profit_period['min'] not in (OriginProfitPeriod( 90, 'day'), OriginProfitPeriod( 180, 'day'), OriginProfitPeriod( 270, 'day'), OriginProfitPeriod(365, 'day')): zw_services.append(s) zw_services.sort(key=attrgetter('annual_rate')) ZhiwangProfile.add(request.oauth.user.id_) for zw_service in zw_services: product_text = product_sale_status_to_text(zw_service) if product_text: zw_service.button_display_text, zw_service.button_click_text = product_text zw_service.is_able_purchased = zw_service.in_stock zw_service.introduction = '' zw_service.title = '' zw_service.activity_title = '' zw_service.activity_introduction = '' zw_service._total_amount = 0 zw_service.agreement = url_for('savings.landing.agreement_zhiwang', _external=True) zw_service.annotations = ZhiwangProduct.get_product_annotations( g.coupon_manager, zw_service) services.extend(zw_services) if 'xm' in partners: xm_products = [s for s in XMProduct.get_all()] xm_products.sort(key=attrgetter('annual_rate')) XMProfile.add(request.oauth.user.id_) for xm_product in xm_products: product_text = product_sale_status_to_text(xm_product) if product_text: xm_product.button_display_text, xm_product.button_click_text = product_text xm_product.is_able_purchased = xm_product.in_stock xm_product.introduction = '' xm_product.title = '' xm_product.activity_title = '' xm_product.activity_introduction = '' xm_product._total_amount = 0 xm_product.agreement = url_for('savings.landing.agreement_xinmi', _external=True) xm_product.annotations = XMProduct.get_product_annotations( g.coupon_manager, xm_product) services.extend(xm_products) product_data = product_schema.dump(services).data for product in product_data: product.update({'is_newcomer': False}) all_product_data = [] if 'sxb' in partners: from .products.sxb import get_sxb_products product_schema = SxbProductSchema(strict=True, many=True) sxb_products = get_sxb_products(request.oauth.user.id_) services.extend(sxb_products) sxb_father_products = [ p for p in sxb_products if p.kind is Product.Kind.father ] sxb_father_product_data = product_schema.dump(sxb_father_products).data if SavingsManager(request.oauth.user.id_).is_new_savings_user: sxb_child_products = [ p for p in sxb_products if p.kind is Product.Kind.child ] sxb_child_product_data = product_schema.dump( sxb_child_products).data for product in sxb_child_product_data: product.update({'is_newcomer': True}) all_product_data.extend(sxb_child_product_data) all_product_data.extend(sxb_father_product_data) all_product_data.extend(product_data) conditional_for(json.dumps(all_product_data)) return jsonify(success=True, data=all_product_data)
def obtain_zw_product(product_id): product = ZhiwangProduct.get(product_id) if not product: warning('用户访问不存在的产品', product_id=product_id) abort(400, u'该产品不存在') return product