def deleteArticle(id): conn = getConn() cursor = conn.cursor() cursor.execute('DELETE FROM articles WHERE id = %s', [id]) conn.commit() return render_template('blog.html')
def leaveMessage(): conn = getConn() cursor = conn.cursor() try: _subject = request.form['inputSubject'] _email = request.form['inputEmail'] _text = request.form['inputText'] if _subject and _email and _text: print("Email:", _email, "\n", "Subject:", _subject, "\n", "Text:", _text) cursor.execute( 'INSERT INTO messages (subject, email, text, date) values (%s, %s, %s, %s)', [_subject, _email, _text, datetime.datetime.now()]) conn.commit() else: print('fields not submitted') return 'Enter the required fields' except Exception as ex: print('got an exception: ', ex) return json.dumps({'error': str(ex)}) finally: cursor.close() conn.close() return "OK"
def validateLogin(): try: _username = request.form['inputUsername'] _password = request.form['inputPassword'] conn = getConn() cursor = conn.cursor() cursor.execute("SELECT * from users where username = %s", [_username]) users = cursor.fetchall() #acctually validate these users if len(users) > 0: _hashed_password = hashlib.md5( _password.encode('utf-8')).hexdigest() if users[0][2] == _hashed_password: session['user'] = users[0] return redirect('/userHome') else: return render_template('error.html', error="incorrect username or password") else: return render_template('error.html', error="incorrect username or password") except Exception as ex: print("Error getting username and password, Error:", ex) return render_template('error.html', error='Missing Email Adress or Password') finally: cursor.close() conn.close()
def viewArticle(id): print("in view article") conn = getConn() cursor = conn.cursor() cursor.execute('SELECT * FROM articles WHERE id = %s', [id]) article = cursor.fetchone() return render_template('viewArticle.html', article = article)
def insertTestData(): conn = getConn() cursor = conn.cursor() cursor.execute("INSERT INTO articles (title, author_id, text) values ('a', '1', 'a')") cursor.execute("INSERT INTO articles (title, author_id, text) values ('b', '1', 'b')") cursor.execute("INSERT INTO articles (title, author_id, text) values ('c', '1', 'c')") cursor.execute("INSERT INTO articles (title, author_id, text) values ('d', '1', 'd')") cursor.execute("INSERT INTO articles (title, author_id, text) values ('e', '1', 'e')") conn.commit()
def getAllArticles(): conn = getConn() cursor = conn.cursor() try: cursor.execute('SELECT id, title, date, text FROM articles') articles = cursor.fetchall() articles_list = [{"Id": article[0], "Title": article[1], "Date": article[2], "Text": article[3]} for article in articles] return json.dumps(articles_list) except Exception as e: return render_template('error.html', error = str(e)) finally: cursor.close() conn.close()
def signUp(): """ method to deal with creating a new user in the MySQL Database """ print("signing up user...") conn = getConn() #create a cursor to query the stored procedure cursor = conn.cursor() try: #read in values from frontend _name = request.form['inputName'] _email = request.form['inputEmail'] _password = request.form['inputPassword'] #Make sure we got all the values if _name and _email and _password: print("Email:", _email, "\n", "Name:", _name, "\n", "Password:"******"Hashed Password:"******"OK"
def editArticle(id): print("in editArticle") try: _user = session.get('user') if _user: print(_user) _title = request.form['inputTitle'] _text = request.form['inputText'] conn = getConn() print("aici %s" % conn) cursor = conn.cursor() print("aici2 %s" % cursor) cursor.execute("UPDATE articles SET title = %s, text = %s WHERE id = %s ", (_title, _text, id)) conn.commit() else: return render_template('error.html',error = 'Unauthorized Access') except Exception as e: print("in exception for addArticle") return render_template('error.html', error = str(e)) finally: cursor.close() conn.close() return redirect('/blog')
def addArticle(): print("in addArticle") try: _user = session.get('user') if _user: print(_user) _title = request.form['inputTitle'] _text = request.form['inputText'] conn = getConn() print("aici %s" % conn) cursor = conn.cursor() print("aici2 %s" % cursor) cursor.execute("INSERT INTO articles (title, text, date, author_id) values (%s, %s, %s, %s)", (_title, _text, datetime.datetime.now(), session.get('user')[0])) conn.commit() else: return render_template('error.html',error = 'Unauthorized Access') except Exception as e: print("in exception for addArticle") return render_template('error.html', error = str(e)) finally: cursor.close() conn.close() return redirect('/blog')
def getAllMessages(): conn = getConn() cursor = conn.cursor() try: cursor.execute('SELECT id, subject, email, date, text FROM messages') messages = cursor.fetchall() messages_list = [{ "Id": message[0], "Subject": message[1], "Email": message[2], "Date": message[3], "Text": message[4] } for message in messages] print(messages_list) return json.dumps(messages_list) except Exception as e: return render_template('error.html', error=str(e)) finally: cursor.close() conn.close()