def getUserById(self, id): query = 'SELECT * from users WHERE id = %(id)s' data = { 'id': id, } mysql = connectToMySQL(db_name) return mysql.query_db(query, data)
def getUserByEmail(self): query = 'SELECT * from users WHERE email = %(email)s' data = { 'email': request.form['email'], } mysql = connectToMySQL(db_name) return mysql.query_db(query, data)
def post_message(self): mysql = connectToMySQL('theWall') query = 'INSERT INTO messages (message, created_at, updated_at, user_id) VALUES (%(message)s,NOW(),NOW(),%(user_id)s)' data = { 'message' : request.form['message'], 'user_id': session['user_id'] } mysql.query_db(query, data) return redirect('/message_success')
def insertMessage(self): query = 'INSERT INTO messages (user_id, message, created_at, updated_at) ' query += 'VALUES (%(user_id)s, %(message)s, NOW(), NOW())' data = { 'user_id': session['user_id'], 'message': request.form['message'] } mysql = connectToMySQL(db_name) newId = mysql.query_db(query, data) return newId
def post_comment(self, id): mysql = connectToMySQL('theWall') query = 'INSERT INTO comments (user_id,message_id, comment, created_at, updated_at) VALUES (%(user_id)s,%(message_id)s,%(comment)s,NOW(),NOW())' data = { 'user_id': session['user_id'], 'message_id': id, 'comment': request.form['comment'] } mysql.query_db(query, data) return redirect('/comment_success')
def getAllPosts(self): query = 'SELECT * FROM messages JOIN users ON messages.user_id = users.id ORDER BY messages.created_at DESC' mysql = connectToMySQL(db_name) messages = mysql.query_db(query) query = 'SELECT * FROM comments JOIN users ON comments.user_id = users.id' mysql = connectToMySQL(db_name) comments = mysql.query_db(query) for msg in messages: msg['comments'] = [] msg['date'] = self.datefy(msg['created_at']) for msg in messages: for com in comments: if msg['id'] == com['message_id']: com['date'] = self.datefy(com['created_at']) msg['comments'].append(com) # pp.pprint(messages) return messages
def newUser(self, pw_hash): mysql = connectToMySQL('theWall') query = 'INSERT INTO users (first_name,last_name, email, password, birthday, created_at, updated_at) VALUES (%(first_name)s, %(last_name)s,%(email)s, %(password)s, %(birthday)s, NOW(), NOW())' data = { 'first_name': request.form['first_name'], 'last_name': request.form['last_name'], 'email': request.form['email'], 'password' : pw_hash, 'birthday': request.form['birthday'] } mysql.query_db(query, data) return redirect('/registration_success')
def deleteCommentById(self, id, created_at): query = 'DELETE FROM comments WHERE id = %(comment_id)s' data = {'comment_id': id} now = datetime.datetime.now() created_at = datetime.datetime.strptime(created_at, '%Y-%m-%d %X') diff = now - created_at minutes = int(diff.total_seconds() / 60) print('-' * 30, 'diff:', minutes) if minutes > 30: return False else: mysql = connectToMySQL(db_name) msg = mysql.query_db(query, data) return True
def registerUser(self): pw_hash = bcrypt.generate_password_hash(request.form['password']) query = 'INSERT INTO users (first_name, last_name, email, password, created_at, updated_at) ' query += 'VALUES (%(first_name)s, %(last_name)s, %(email)s, %(password)s, NOW(), NOW());' data = { 'first_name': request.form['first_name'], 'last_name': request.form['last_name'], 'email': request.form['email'], 'password': pw_hash, } mysql = connectToMySQL(db_name) newId = mysql.query_db(query, data) if newId: self.logInUser(newId) return newId
def checkUser(self): mysql = connectToMySQL('theWall') query = 'SELECT * FROM users WHERE email = %(email)s;' data = {'email' : request.form['email']} result = mysql.query_db(query, data) return result
def post_comment_update(self): mysql = connectToMySQL('theWall') query = 'SELECT * FROM comments JOIN messages on comments.message_id = messages.id JOIN users ON users.id = comments.user_id ORDER BY comments.created_at DESC' result = mysql.query_db(query) return result
def all_messages(self): mysql = connectToMySQL('theWall') query = 'SELECT * FROM messages JOIN users on messages.user_id = users.id ORDER BY messages.created_at DESC' result = mysql.query_db(query) return result