def all_tweets(): """ This method handles All Tweets View. :return: If "GET" rendering template "all_tweets", If "POST" adding new Tweet to db and redirecting back to all_tweets. """ if not session['logged_in']: return redirect(url_for('login')) if request.method == "GET": cnx = connect_db() tweets = Tweet.load_all_tweets(cnx.cursor()) return render_template('all_tweets.html', tweets=tweets) elif request.method == "POST": tweet = Tweet() tweet.user_id = session['user_id'] tweet.text = request.form['new_tweet'] tweet.creation_date = datetime.now() cnx = connect_db() tweet.add_tweet(cnx.cursor()) cnx.commit() return redirect(url_for('all_tweets'))
def main_page(): html = """ <!doctype html> <html> <head> <meta charset="utf-8"> </head> <body> <form method="POST"> Lista tweetow: <br> </form> </body> </html> """ if request.method == "GET": cnx = connect(user="******", password="******", host="localhost", database="twitter") print("Connected") cursor = cnx.cursor() all_tweets = Tweet.load_all_tweets(cursor) for tweet in all_tweets: html += """ {} {} {} <br>""".format(tweet.user_id, tweet.text, tweet.creation_date) return html else: pass