示例#1
0
def create_bidder(new_bidder: Bidder):
    '''Create a Bidder in the database'''
    new_bidder.set_id(_get_user_id_counter())
    try:
        users.insert_one(new_bidder.to_dict())
        op_success = new_bidder
    except pymongo.errors.DuplicateKeyError as err:
        _log.error(err)
        op_success = None
    _log.info('Added %s', new_bidder.get_username)
    return op_success
示例#2
0
def auction_end(auction_id, bidder_id):
    '''Find and end the specified auction. Sets the auction winner, and updates bid history
    of bidders involved.'''
    bidder_id = int(bidder_id)
    auction_id = int(auction_id)
    query_string = {'_id': auction_id}
    auct = Auction.from_dict(read_auction_by_id(auction_id))
    bid_list = auct.get_bids()
    winning_bid = None
    for bid in bid_list:
        if int(bid['bidder_id']) == bidder_id:
            winning_bid = bid
            bidder_doc = read_user_by_id(int(bid['bidder_id']))
            try:
                bidder = Bidder.from_dict(bidder_doc)
                bidder.create_history(auction_id, float(bid['amount']), 'Win')
                users.update_one({'_id': bidder_id},
                                 {'$set': bidder.to_dict()})
            except TypeError as err:
                _log.error('Encountered an error: %s', err)
        else:
            bidder_doc = read_user_by_id(int(bid['bidder_id']))
            try:
                bidder = Bidder.from_dict(bidder_doc)
                bidder.create_history(auction_id, float(bid['amount']), 'Loss')
                users.update_one({'_id': int(bid['bidder_id'])},
                                 {'$set': bidder.to_dict()})
            except TypeError as err:
                _log.error('Encountered an error: %s', err)
    try:
        date_now = datetime.datetime.now()
        auctions.update_one(
            query_string, {
                '$set': {
                    'status': 'Closed',
                    'bids': winning_bid,
                    'date_end': date_now
                }
            })
        op_success = winning_bid
    except:
        op_success = None
    _log.info(' to auction %s', auction_id)
    return op_success
示例#3
0
def create_user():
    '''This is user route for registration.'''
    _log.debug('Creating bidder')
    required_fields = ['username', 'password']
    if request.method == 'POST':
        input_dict = request.get_json(force=True)
        _log.debug('User POST request received with body %s', input_dict)
        if all(field in input_dict for field in required_fields):
            username = input_dict['username']
            password = input_dict['password']
            new_bidder = Bidder(username, password)
            if create_bidder(new_bidder):
                return jsonify(new_bidder.to_dict()), 201
            else:
                return request.json, 400
        else:
            return request.json, 400
    else:
        empty = make_response({})
        empty.set_cookie('authorization', '')
        return empty, 204
示例#4
0
def read_product_info_by_user_history(user_id):
    bidder = Bidder.from_dict(read_user_by_id(int(user_id)))
    product_history = []
    for bid in bidder.get_history():
        auct = read_auction_by_id(bid['auction_id'])
        _log.debug(auct)
        prod = read_product_by_id(auct['item_id'])
        product_history.append({
            'auction_id': bid['auction_id'],
            'name': prod['name'],
            'description': prod['description']
        })
    return product_history
示例#5
0
def login(username: str, password: str):
    '''A function that takes in a username and returns a user object with that username'''
    _log.debug('Attempting to retrieve user from database')
    query_dict = {'username': username}
    user_dict = users.find_one(query_dict)
    if user_dict['password'] == password:
        _log.debug(user_dict)
        _log.debug(type(user_dict))
        # Ternary is "True value" if <condition> else "False Value"
        if user_dict:
            if 'role' in user_dict:
                return_user = Employee.from_dict(user_dict)
            else:
                return_user = Bidder.from_dict(user_dict)
        else:
            return_user = None
        return return_user
    else:
        return None
示例#6
0

if __name__ == "__main__":
    util.drop()
    products.drop()
    users.drop()
    auctions.drop()

    util.insert_one({'_id': 'USERID_COUNTER', 'count': 0})
    util.insert_one({'_id': 'AUCTIONID_COUNTER', 'count': 0})
    util.insert_one({'_id': 'PRODUCTID_COUNTER', 'count': 0})

    users.create_index('username', unique=True)

    # Bidder
    bidder = Bidder('bidder', 'password')
    bidder = create_bidder(bidder)
    # manager
    manager = Employee('manager', 'password', 'Manager')
    create_employee(manager)
    # curator
    curator = Employee('curator', 'password', 'Curator')
    create_employee(curator)
    # auctioneer
    auctioneer = Employee('auctioneer', 'password', 'Auctioneer')
    create_employee(auctioneer)

    # product
    product = Product('Egyptian Sarcophagus',
                      'Egyptian funeral receptacle for a corpse.', 1000)
    create_product(product)
示例#7
0
 def test_from_dict(self):
     '''Test for the from_dict function.'''
     test_dict = {'username': '******', 'password': '******', 'history': []}
     self.bidder = Bidder().from_dict(test_dict)
     self.assertIs(type(BidderTestSuite.bidder), Bidder)
示例#8
0
 def setUpClass(cls):
     cls.bidder = Bidder()
示例#9
0
 def setUp(self):
     self.bidder = Bidder('username', 'password')