async def bid(context: CommandContext, auction_id: int, amount: float): user = context.runtime_user amount = Decimal(amount) try: auction = runtime_manager.get_auction_by_id(auction_id) auction.bid(user, amount) except AuctionDoesNotExist as e: raise CommandFailed(f"Could not bid in auction: {e}") except BiddingNotAllowed as e: raise CommandFailed(f"Bidding not allowed: {e.reason.value}") return {'auction': {**auction.to_json()}}
async def sell(context: CommandContext, auction_id: int): user = context.runtime_user try: auction = runtime_manager.get_auction_by_id(auction_id) if user.id == auction.owner.id: auction.sell() else: raise CommandFailed( "You must be the owner of the auction to perform this action.") except AuctionDoesNotExist as e: raise CommandFailed(f"Could not end auction: {e}") return {'auction': {**auction.to_json()}}
async def login(context: CommandContext, username: str, password: str): if context.runtime_user: raise CommandFailed("You are already logged in!") try: user = User.objects.get(username=username) except User.DoesNotExist: raise CommandFailed("Incorrect username or password.") if user.check_password(password): runtime_user = RuntimeUser.from_persistent_user(user) else: raise CommandFailed("Incorrect username or password.") runtime_user.connect() context.runtime_user = runtime_user return {"user": {"id": runtime_user.id}}
async def verify(context: CommandContext, verification_number): user = context.runtime_user try: user.verify(verification_number) except UserVerificationError as e: raise CommandFailed(f"Verification failed: {e}") return {'message': 'You have successfully verified your email address.'}
async def change_password(context: CommandContext, new_password: str, old_password: str): """ Used for changing the password of an already logged in user. If the password is forgotten, use the "reset_password" command. """ if old_password is None: raise CommandFailed( "You must provide your old password in order to change it.") user = context.runtime_user try: user.change_password(new_password, old_password) except InvalidPassword: raise CommandFailed("Invalid password.") return {'message': 'Your password has been changed.'}
async def create_auction(context: CommandContext, item_id: int, bidding_strategy_identifier: str, **kwargs): user = context.runtime_user # Coerce all floats to decimals for key, value in kwargs.items(): if isinstance(value, float): kwargs[key] = Decimal(value) try: auction_id = user.create_auction( item_id=item_id, bidding_strategy_identifier=bidding_strategy_identifier, **kwargs, ) except ItemAlreadyOnSale: raise CommandFailed("Cannot create auction, item is already on sale.") auction = runtime_manager.get_auction_by_id(auction_id) return {'auction': {**auction.to_json()}}
async def watch_auction(context: CommandContext, auction_id: int): try: auction = runtime_manager.get_auction_by_id(auction_id) except AuctionDoesNotExist as e: raise CommandFailed(f"Could not watch auction report: {e}") @push_notification(context.websocket) def notify(**kwargs): notification_object = { 'domain': 'auction', 'type': kwargs.get('type'), 'data': kwargs.get('data'), 'current_price': money(auction.bidding_strategy.current_price), } notification_object['msg'] = get_human_readable_activity_message( notification_object) return notification_object auction.register_user_to_updates(notify) return {}
async def wrapper(*args, **kwargs): context, *_ = args if not context.runtime_user: raise CommandFailed("You must log in to perform this action.") return await func(*args, **kwargs)
async def view_auction_history(context: CommandContext, auction_id: int): try: auction = runtime_manager.get_auction_by_id(auction_id) except AuctionDoesNotExist as e: raise CommandFailed(f"Could not view auction history: {e}") return {'auction': {'report': auction.auction_history}}