def rent_to(self, user, card_href=None): # Fetch the buyer buyer = user.balanced_customer # Fetch the buyers card to debit if card_href: card = balanced.Card.fetch(card_href) else: if not buyer.cards.count(): raise Exception('No card on file') if not user.has_password: raise Exception('Anonymous users must specify a card') else: card = buyer.cards.first() # Fetch the merchant owner_user = User.query.get(self.owner_guid) owner = owner_user.balanced_customer # create an Order order = owner.create_order(desciption=self.bike_type) # debit the buyer for the amount of the listing debit = order.debit_from( source=card, amount=(self.price * 100), appears_on_statement_as=self.bike_type, ) # credit the owner of bicycle for the amount of listing # # since this is an example, we're showing how to issue a credit # immediately. normally you should wait for order fulfillment # before crediting. # First, fetch the onwer's bank account to credit bank_account = owner.bank_accounts.first() if not bank_account: raise Exception('No bank account on file') bank_account.credit(amount=(self.price * 100)) order.credit_to( destination=bank_account, amount=(self.price * 100), ) rental = Rental(buyer_guid=user.guid, order_href=order.href, listing_guid=self.id, owner_guid=owner_user.guid) Session.add(rental) return rental
def rent_to(self, user, card_href=None): # Fetch the buyer buyer = user.balanced_customer # Fetch the buyers card to debit if card_href: card = balanced.Card.fetch(card_href) else: if not buyer.cards.count(): raise Exception('No card on file') if not user.has_password: raise Exception('Anonymous users must specify a card') else: card = buyer.cards.first() # Fetch the merchant owner_user = User.query.get(self.owner_guid) owner = owner_user.balanced_customer # create an Order order = owner.create_order(desciption=self.bike_type) # debit the buyer for the amount of the listing debit = order.debit_from( source=card, amount=(self.price * 100), appears_on_statement_as=self.bike_type, ) # credit the owner of bicycle for the amount of listing # # since this is an example, we're showing how to issue a credit # immediately. normally you should wait for order fulfillment # before crediting. # First, fetch the onwer's bank account to credit bank_account = owner.bank_accounts.first() if not bank_account: raise Exception('No bank account on file') bank_account.credit( amount=(self.price * 100) ) order.credit_to( destination=bank_account, amount=(self.price * 100), ) rental = Rental(buyer_guid=user.guid, order_href=order.href, listing_guid=self.id, owner_guid=owner_user.guid) Session.add(rental) return rental
def add_dummy_data(self): for name, email, password in config['DEFAULT_USERS']: user = User.query.filter(User.email_address == email).count() if not user: user = User(name=name, email_address=email, password=password) Session.add(user) for i in range(4): listing = Listing.query.filter(Listing.id == i + 1).count() if not listing: listing = Listing(id=i + 1) Session.add(listing) Session.commit()
def create_guest_user(email_address, name=None, password=None): try: user = User.query.filter( User.email_address == email_address).one() except NoResultFound: Session.flush() user = User(email_address=email_address, name=name, password=password) Session.add(user) try: Session.flush() except: Session.rollback() Session.commit() Session.add(user) Session.commit() return user
def create(**kwargs): account_form = kwargs.pop('forms')[0] if account_form.validate(): user = account_form.create_user() Session.add(user) try: Session.commit() except IntegrityError: flash('This account already exists!', 'warning') Session.rollback() else: user.lookup_balanced_customer() Session.commit() session['user_guid'] = user.guid return redirect(url_for('accounts.index')) return new(account_form)
def create_guest_user(email_address, name=None, password=None): try: user = User.query.filter(User.email_address == email_address).one() except NoResultFound: Session.flush() user = User(email_address=email_address, name=name, password=password) Session.add(user) try: Session.flush() except: Session.rollback() Session.commit() Session.add(user) Session.commit() return user
def create(**kwargs): account_form = kwargs.pop("forms")[0] if account_form.validate(): user = account_form.create_user() Session.add(user) try: Session.commit() except IntegrityError: flash("This account already exists!", "warning") Session.rollback() else: user.lookup_balanced_account() Session.commit() session["user_guid"] = user.guid return redirect(url_for("accounts.index")) return new(account_form)
def add_dummy_data(self): user = User( name='Dummy User', email=self.dummy_email_generator(), password='******') Session.add(user) user.create_balanced_customer() for i in range(4): owner = User.fetch_one_at_random() if not user.balanced_customer.bank_accounts.count(): self.dummy_bank_account_generator(owner) listing = Listing.query.filter(Listing.id == i + 1).count() if not listing: listing = Listing(id=i + 1, owner_guid=owner.guid) Session.add(listing) Session.commit()
def rent_to(self, user, card_uri=None): account = user.balanced_account if not card_uri: if not account.cards.count(): raise Exception('No card on file') if not user.has_password: raise Exception('Anonymous users must specify a card') # this will throw balanced.exc.HTTPError if it fails debit = account.hold(self.price * 100, source_uri=card_uri) rental = Rental(buyer_guid=user.guid, debit_uri=debit.uri, bike_guid=self.id) Session.add(rental) return rental
def __init__(self, password=None, **kwargs): if password: self.password_hash = generate_password_hash(password) self.has_password = True super(User, self).__init__(**kwargs) Session.add(self)