def search(page_name=None): """ @param page_name: @return: @rtype: """ error = list() # get the id of the user you want to follow if request.method == 'GET': # via ajax search_request = request.args.get('search-request', '', type=str) else: # via no javascript post search_request = request.form['search-request'] if 'user' in session: dbh = UserHelper(StrictRedis()) user = dbh.get_user_by_id(session['user']['id']) dbh.get_all_usernames() dbh.post_message(message_object) else: return redirect(url_for('login')) if request.method == 'GET': return jsonify(post_time=message_object.posted_time, format_time=message_object.formatted_time, msg_id=message_object.id) return redirect(url_for('dash'))
def dash(app): """ @param app: @type app: Flask @return: @rtype: """ if request.method == 'GET': if session.has_key('user'): dbh = MessageHelper(StrictRedis()) dbh_user = UserHelper(StrictRedis()) backward = dbh.get_user_messages(session['user']['id']) messages = reversed(backward) user = dbh_user.get_user_by_id(session['user']['id']) #users = list() #for message in messages: #user = dbh_user.get_user_by_id(message.user_id) #users.append(user) #message.user_id = user return render_template('dash.html', error=None, title='Twic Dashboard', messages=messages, user=user) return render_template('login.html', error='Please log in', title='Login') if request.method == 'POST': error = list()
def __init__(self, user_id, redis_connection, password=None): """ @param user_id: @type user_id: int @param redis_connection @type redis_connection: StrictRedis @param password: @type password: str @return: @rtype: """ # get or create some password to encrypt the user verification token self.redis = UserHelper(redis_connection, user_id) self.password = password if password else self.redis.get('token_pass') super(RedisToken, self).__init__(user_id, password)
def login(app): """ :param app: :return: :rtype: """ if request.method == 'GET': return render_template('login.html', error=None, title='Twic Login') if request.method == 'POST': error = list() name = request.form['name'] password = request.form['password'] # should be verifying via the redis wrapper methods, not using app.* # if name != app.config['name']: # error.append('Invalid Username') # elif password != app.config['password']: # error.append('Invalid Password') # else: # Set the session as logged in. user = None redis = StrictRedis() dbh = UserHelper(redis) email_validator = lepl.apps.rfc3696.Email() valid = email_validator(name) if email_validator(name) and dbh.email_exists(name): user = dbh.get_user_by_email(request.form['name']) elif dbh.username_exists(name): user = dbh.get_user_by_username(request.form['name']) if user: hashed_password = UserHelper.hash_password(password, user.salt) if hashed_password == user.password: # Save the user to the session session['user'] = user.get_dict() session['logged_in'] = True return redirect(url_for('dash')) else: error.append('That user does not exist.') return render_template('login.html', error=error)
def register(app): """ @param app: @type app: Flask @return: @rtype: """ if request.method == 'GET': return render_template('registration.html', error=None, title='Twic Registration') if request.method == 'POST': errors = list() name = request.form['name'] username = request.form['username'] email = request.form['email'] password = request.form['password'] user = User(username, email, password, name) dbh = UserHelper(StrictRedis()) if not name or not username or not email or not password: errors.append('All fields are required') if dbh.username_exists(user.username): errors.append( 'That username already exists. Please choose another') if dbh.email_exists(user.email): errors.append('That email already exists. Please choose another') if len(errors): return render_template('registration.html', error=errors, title='Twic Registration', user=user) dbh.add_user(user) session['user'] = user.get_dict() session['logged_in'] = True return redirect(url_for('dash'))
class RedisTests(unittest.TestCase): """ @param user: user instance to test @type user: User """ redis = redis.StrictRedis() dbh = UserHelper(redis) dbhm = MessageHelper(redis) user = User('jsmith123', '*****@*****.**', 'plain-text-pass', 'Jon Smith') def setUp(self): pass def test_add_user(self): """ Test adding a user to redis """ if self.dbh.username_exists(self.user.username): self.user = self.dbh.get_user_by_username(self.user.username) else: user_id = self.dbh.add_user(self.user) self.user.id = user_id self.assertTrue(self.dbh.email_exists(self.user.email)) self.assertTrue(self.dbh.username_exists(self.user.username)) self.assertTrue(self.dbh.user_id_exists(self.user.id)) stored_user = self.dbh.get_user_by_id(self.user.id) self.assertEqual(self.user.email, stored_user.email) self.assertEqual(self.user.username, stored_user.username) self.assertEqual(str(self.user.id), str(stored_user.id)) self.assertEqual(self.user.salt, stored_user.salt) self.assertEqual(self.user.name, stored_user.name) # self.assertEqual(self.user.followers, stored_self.user.followers) # self.assertEqual(self.user.following, stored_self.user.following) # self.assertEqual(self.user.messages, stored_self.user.messages) search_results = self.dbh.fuzzy_username_search( self.user.username[:-3]) print(search_results) self.assertTrue(len(search_results) > 0) self.assertEqual(self.user.password, stored_user.password) def test_add_message(self): if self.dbh.username_exists(self.user.username): self.user = self.dbh.get_user_by_username(self.user.username) else: user_id = self.dbh.add_user(self.user) self.user.id = user_id message = Message(self.user.id, 'Test twitter clone message number 1, yay!!!!') result = self.dbhm.post_message(message) self.assertTrue(result) stored_message = self.dbhm.get_message(message.id) self.assertEqual(stored_message.message, message.message) self.assertEqual(stored_message.user_id, str(message.user_id)) message = Message(self.user.id, 'Another test message wooo') result = self.dbhm.post_message(message) self.assertTrue(result) stored_message = self.dbhm.get_message(message.id) self.assertEqual(stored_message.message, message.message) self.assertEqual(stored_message.user_id, str(message.user_id)) messages = self.dbhm.get_user_messages(self.user.id) self.assertEqual(2, len(messages)) #def test_add_follower(self): # """ # Test adding and getting a follower # """ # self.dbh.add_user(user) # self.dbh.add_follower(2, self.user.id) # followers = self.dbh.get_follower_ids(self.user.id) # self.assertTrue('2' in followers) def tearDown(self): """