def test_create(self): with patch('core.models.profile.bankcard.DEBUG', True): b1 = BankCard.add(self.local_account.id, '13800138000', '6222980000000002', '1', '440113', '440000', '大望路支行', True) b2 = BankCard.add(self.local_account.id, '13800138001', '6222980000010001', '2', '440113', '440000', '西二旗支行', False) assert b1 assert b2 bank_card = BankCard.get(b1.id_) assert bank_card.mobile_phone == '13800138000' assert bank_card.card_number == '6222980000000002' assert bank_card.city_id == '440113' assert bank_card.province_id == '440000' assert bank_card.local_bank_name == '大望路支行' assert bank_card.is_default bank_card = BankCard.get(b2.id_) assert bank_card.mobile_phone == '13800138001' assert bank_card.card_number == '6222980000010001' assert bank_card.city_id == '440113' assert bank_card.province_id == '440000' assert bank_card.local_bank_name == '西二旗支行' assert bank_card.is_default is False
def check_before_adding(cls, user_id, product_id, bankcard_id, amount): product = PlaceboProduct.get(product_id) bankcard = BankCard.get(bankcard_id) user = Account.get(user_id) # 检查关联对象 for attr_name, attr in [('product_id', product), ('bankcard_id', bankcard), ('user_id', user)]: if not attr: raise ValueError('invalid %s' % attr_name) # 检查身份认证 if not has_real_identity(user): raise InvalidIdentityError() # 检查产品是否可售 if not product.strategy.target(user_id): raise InvalidProductError(product_id) # 策略拒绝 if not product.in_stock: raise OffShelfError(product_id) # 下架 # 检查金额范围 if amount is None and product.min_amount == product.max_amount: amount = product.min_amount if (amount.is_nan() or amount < product.min_amount or amount > product.max_amount): raise OutOfRangeError(amount, (product.min_amount, product.max_amount)) return user, product, bankcard, amount
def test_discard(self): def create_bankcard_in_the_same_bank(): return BankCard.add(self.local_account.id, '13800138000', '6222980000010001', '1', '440113', '440000', '大望路支行', False) with patch('core.models.profile.bankcard.DEBUG', True): bankcard = BankCard.add(self.local_account.id, '13800138000', '6222980000000002', '1', '440113', '440000', '大望路支行', False) with raises(BankConflictError): create_bankcard_in_the_same_bank() bankcard.discard() assert self.bankcards.get_all() == [] assert self.bankcards.get_latest() is None assert self.bankcards.get_last_used() is None new_bankcard = create_bankcard_in_the_same_bank() assert new_bankcard card_ids = {c.id_ for c in self.bankcards.get_all()} assert bankcard.id_ not in card_ids assert new_bankcard.id_ in card_ids assert BankCard.get(bankcard.id_).status is BankCard.Status.discarded assert self.bankcards.get_latest().id_ == new_bankcard.id_ assert self.bankcards.get_last_used().id_ == new_bankcard.id_
def initialize_with_bankcard(self, bankcard_id): """Initializes with bankcard. :param bankcard_id: The default bankcard used in third-party account. :raises UnsupportedBankError: if the bank of choosen bankcard is not supported. """ self.bankcard = BankCard.get(bankcard_id) if not self.bankcard: raise ProgrammingError('bankcard %r not found' % bankcard_id) self.bankcard.bank.raise_for_unavailable(self.provider.bank_partner)
def test_delete_and_restore(self): with patch('core.models.profile.bankcard.DEBUG', True): b1 = BankCard.add(self.local_account.id, '13800138000', '6222980000000002', '1', '440113', '440000', '大望路支行', False) assert b1 BankCard.delete_by_card_number('6222980000000002', self.local_account.id) b2 = BankCard.get(b1.id_) assert b2 is None BankCard.restore(b1.id_, self.local_account.id) b3 = BankCard.get(b1.id_) assert b3 assert b3.mobile_phone == '13800138000' assert b3.card_number == '6222980000000002' assert b3.city_id == '440113' assert b3.province_id == '440000' assert b3.local_bank_name == '大望路支行'
def check_before_adding(cls, vendor, user_id, bankcard_id, product_id, amount): if not vendor: raise NotFoundEntityError(vendor.id_, Account) product = HoarderProduct.get(product_id) local_account = Account.get(user_id) bankcard = BankCard.get(bankcard_id) hoarder_account = HoarderAccount.get(vendor.id_, user_id) if not local_account: raise NotFoundEntityError(user_id, Account) if not bankcard: raise NotFoundEntityError(bankcard_id, BankCard) if not hoarder_account: raise UnboundAccountError(user_id) if not has_real_identity(local_account): raise InvalidIdentityError() # 产品是否处于可售状态 if product.is_sold_out: raise SoldOutError(product_id) # 产品是否处于正常销售状态 if product.is_taken_down: raise SuspendedError(product_id) # 产品是否处于在售状态 if not product.is_on_sale: raise OffShelfError(product_id) # checks the product amount limit if not isinstance(amount, Decimal): raise TypeError('order amount must be decimal') amount_range = (product.min_amount, product.max_amount) amount_check = [ amount.is_nan(), amount < 0, amount < amount_range[0], amount > amount_range[1] ] if any(amount_check): raise OutOfRangeError(amount, amount_range)
def check_before_adding(cls, user_id, bankcard_id, product_id, amount): product = XMFixedDuedayProduct.get(product_id) bankcard = BankCard.get(bankcard_id) local_account = Account.get(user_id) xm_account = XMAccount.get_by_local(user_id) # checks the related entities if not bankcard: raise NotFoundError(bankcard_id, BankCard) if not local_account: raise NotFoundError(local_account, Account) if not xm_account: raise UnboundAccountError(user_id) # checks the identity if not has_real_identity(local_account): raise InvalidIdentityError # 产品是否处于可售状态 if product.is_sold_out: raise SoldOutError(product_id) # 产品是否处于正常销售状态 if product.is_taken_down: raise SuspendedError(product_id) # 产品是否处于在售状态 if not product.in_stock: raise OffShelfError(product_id) # checks the product amount limit if not isinstance(amount, Decimal): raise TypeError('order amount must be decimal') amount_range = (product.min_amount, product.max_amount) amount_check = [amount.is_nan(), amount < 0, amount < amount_range[0], amount > amount_range[1]] if any(amount_check): raise OutOfRangeError(amount, amount_range)
def bankcard(self): if not self.bankcard_id: return return BankCard.get(self.bankcard_id)
def bankcard(self): return BankCard.get(self.bankcard_id)
def remove_bankcard(user_id, bankcard_id): profile = ZhiwangProfile.add(user_id) bankcard = BankCard.get(bankcard_id) if not bankcard: return profile.bankcards.remove(bankcard.card_number, silent=True)
def bank_card(self): return BankCard.get(self.order.bankcard_id) if self.order else ''
def check_before_adding(cls, user_id, bankcard_id, product_id, amount, wrapped_product_id=None): product = ZhiwangProduct.get(product_id) wrapped_product = ZhiwangWrappedProduct.get( wrapped_product_id) if wrapped_product_id else None # check the product and wrapped_product if wrapped_product_id is not None and not wrapped_product: raise NotFoundError(wrapped_product_id, ZhiwangWrappedProduct) if wrapped_product and wrapped_product.raw_product.product_id != product_id: raise UnknownProductInheritance(product_id, wrapped_product_id) if wrapped_product and not wrapped_product.is_qualified(user_id): raise IneligiblePurchase(user_id, wrapped_product_id) product = wrapped_product if wrapped_product else product bankcard = BankCard.get(bankcard_id) local_account = Account.get(user_id) zhiwang_account = ZhiwangAccount.get_by_local(user_id) # checks the related entities if not bankcard: raise NotFoundError(bankcard_id, BankCard) if not local_account: raise NotFoundError(local_account, Account) if not zhiwang_account: raise UnboundAccountError(user_id) # checks the identity if not has_real_identity(local_account): raise InvalidIdentityError # 产品是否处于可售状态 if product.is_either_sold_out: raise SoldOutError(product_id, wrapped_product_id) # 产品是否处于正常销售状态 if product.is_taken_down: raise SuspendedError(product_id, wrapped_product_id) # 产品是否处于在售状态 if not product.in_stock: raise OffShelfError(product_id, wrapped_product_id) # checks the product amount limit if not isinstance(amount, Decimal): raise TypeError('order amount must be decimal') amount_range = (product.min_amount, product.max_amount) amount_check = [ amount.is_nan(), amount < 0, amount < amount_range[0], amount > amount_range[1] ] if any(amount_check): raise OutOfRangeError(amount, amount_range) # checks the bank amount limit bank_limit = bankcard.bank.zwlib_amount_limit bank_limit = max(bank_limit) if cls.is_bankcard_swiped( bankcard) else min(bank_limit) if amount > bank_limit: raise ExceedBankAmountLimitError(amount, bank_limit)