Example #1
0
    def register_item(self, item_name, reserved_price):
        if Item.one(item_name) is not None:
            return {
                "status": "error",
                "errors": ["Item already exists."],
            }

        item = Item(item_name, reserved_price)
        item.save()

        return {
            "status": "success",
            "item_name": item.name,
        }
Example #2
0
    def unstage_item_from_auction(self, item_name):
        item = Item.one(item_name)
        if item is None:
            return {
                "status": "error",
                "errors": ["Item does not exist."],
            }

        if item.status != const.ITEM_STATUS_STAGED:
            return {
                "status": "error",
                "errors": ["Item is not staged for auction."],
            }

        def filter_func(auc_record):
            item_name_met = auc_record.get("item_name") == item_name
            auc_status_met = auc_record.get("status") == const.AUCTION_STATUS_CREATED

            return item_name_met and auc_status_met

        ret = Auction.all(filter_func)
        if ret:
            auction = ret[0]
            auction.delete()
        else:
            return {
                "status": "error",
                "errors": [
                    "Auction for this item already progressed, and cannot "
                    "unstage the item at this time."
                ],
            }

        item.status = const.ITEM_STATUS_AVAILABLE
        item.save()

        return {
            "status": "success",
            "item_name": item.name,
        }
Example #3
0
    def update_item_reserved_price(self, item_name, reserved_price):
        item = Item.one(item_name)
        if item is None:
            return {
                "status": "error",
                "errors": ["Item does not exist."],
            }

        if item.status != const.ITEM_STATUS_AVAILABLE:
            return {
                "status": "error",
                "errors": [
                    "You cannot change the reserved price of an item that is"
                    "currently staged for auction or was sold already."
                ],
            }

        item.reserved_price = reserved_price
        item.save()

        return {
            "status": "success",
            "item_name": item.name,
        }
Example #4
0
    def create_auction(self, item_name):
        item = Item.one(item_name)
        if item is None:
            return {
                "status": "error",
                "errors": ["Item does not exist."],
            }

        def filter_func(auc_record):
            return auc_record.get("status") != const.AUCTION_STATUS_CALLED_FAIL

        if Auction.all(filter_func):
            return {
                "status": "error",
                "errors": ["Auction already exists for this item."],
            }

        auction = Auction(item_name)
        auction.save()

        return {
            "status": "success",
            "auction_id": auction.id,
        }
Example #5
0
    def query_latest_summary_for_item(self, item_name):
        item = Item.one(item_name)
        if item is None:
            return {
                "status": "error",
                "errors": ["Item does not exist."],
            }

        item_info = {
            "name": item.name,
            "reserved_price": item.reserved_price,
            "status_code": item.status,
            "status_name": const.ITEM_STATUS_NAMES[item.status],
            "created_at": item.created_at,
            "updated_at": item.updated_at,
        }

        auction = None
        auction_info = None
        if item.status > const.ITEM_STATUS_AVAILABLE:

            # We will exclude any past failed auctions in summary because
            # presumably that's not good for auction.
            def filter_func(auc_record):
                item_name_met = auc_record.get("item_name") == item_name
                auc_status_met = auc_record.get("status") != const.AUCTION_STATUS_CALLED_FAIL
                return item_name_met and auc_status_met

            ret = Auction.all(filter_func)
            if ret:
                auction = ret[0]
                auction_info = {
                    "id": auction.id,
                    "status_code": auction.status,
                    "status_name": const.AUCTION_STATUS_NAMES[auction.status],
                    "highest_bid_id": auction.highest_bid_id,
                    "winning_bid_id": auction.winning_bid_id,
                    "created_at": auction.created_at,
                    "started_at": auction.started_at,
                    "closed_at": auction.closed_at,
                }

        bid_id = None
        if auction and auction.winning_bid_id:
            bid_id = auction.winning_bid_id
        elif auction and auction.highest_bid_id:
            bid_id = auction.highest_bid_id

        bid_info = None
        if bid_id:
            prevailing_bid = Bid.one(bid_id)
            if prevailing_bid:
                bid_info = {
                    "id": prevailing_bid.id,
                    "offer_price": prevailing_bid.offer_price,
                    "participant_id": prevailing_bid.participant_id,
                    "submitted_at": prevailing_bid.submitted_at,
                }

        return {
            "status": "success",
            "item": item_info,
            "auction": auction_info,
            "prevailing_bid": bid_info,
        }