def get_user_by_username(username: str): '''Returns a user by their id''' user = {} db_user = _db.users.find_one({'username': username}) if db_user: user = User.from_dict(db_user) return user
def register(username: str, password: str, role: str, age: int): ''' Creates a new user.''' _id = _db.counter.find_one_and_update( {'_id': 'USER_COUNT'}, {'$inc': { 'count': 1 }}, return_document=pymongo.ReturnDocument.AFTER)['count'] _log.debug(_id) query = { "_id": _id, "username": username, "password": password, "role": role } user = User(_id, username, password, role, int(age)) _log.debug(query) try: _db.users.insert_one(user.to_dict()) except pymongo.errors.DuplicateKeyError: _log.info( 'Duplicate key error detected in the database for username %s', username) return 'Duplicate Username Error' except pymongo.errors.PyMongoError: _log.exception('register has failed in the db') return None return User.from_dict(_db.users.find_one({'_id': _id}))
def login(username: str, password: str): '''checks the given username/password combination against the database. returns the username for now. Will discus and return either the user id or username''' # query = {"username": username, "password": password} response = _db.users.find_one({'username': username}) if response: return User.from_dict(response) return None
def get_users_by_usertype(usertype): ''' Retrieves all users of a given usertype''' query = {'usertype': usertype} user_list = None try: user_list = _db.users.find(query) except pymongo.errors.PyMongoError: _log.exception('get_user_by_usertype failed on usertype %s', usertype) return [User.from_dict(user) for user in user_list] if user_list else None
def get_users_by_set(setid): ''' Returns a list of all users that have voted on a particular set''' query = {'voted_sets': setid} user_list = None try: user_list = _db.users.find(query) except pymongo.errors.PyMongoError: _log.exception('get_users_by_set has failed on set_id %d', setid) return [User.from_dict(user) for user in user_list] if user_list else None
def get_users_by_age_range(start_age, end_age): ''' Retrieves all users within the age range given. Start inclusive, End exclusive ''' query = {'$and': [{'age': {'$gte': start_age}}, {'age': {'$lt': end_age}}]} user_list = [] try: user_list = _db.users.find(query) except pymongo.errors.PyMongoError: _log.exception('get_users_by_age_range has failed on range %d to %d', start_age, end_age) return [User.from_dict(user) for user in user_list] if user_list else None
def test_from_dict(self): '''tests the from_dict class method''' _log.debug("Testing User.from_dict()") dictionary = UserTests.user.__dict__ self.assertIsInstance(User.from_dict(dictionary), User, "Should return a User")
def get_user_by_id(db_id: int): '''Returns a user by their id''' return User.from_dict(_db.users.find_one({'_id': db_id}))
def get_users(): try: user_list = _db.users.find() except pymongo.errors.PyMongoError: _log.exception('get_users has failed in the database') return [User.from_dict(user) for user in user_list]