def deleteTweet(id): db = connectToMySQL('registration') query = "DELETE FROM likes WHERE tweets_id = " + id delete_likes = db.query_db(query) db = connectToMySQL('registration') query = "DELETE FROM tweets where id = " + id delete_tweet = db.query_db(query) return redirect('/success')
def tweet_home(): if 'id' not in session: return redirect("/") mysql = connectToMySQL('dojo_tweets') query = "SELECT * FROM users WHERE id = %(id)s" data = {'id': session['id']} user = mysql.query_db(query, data) mysql = connectToMySQL("dojo_tweets") query = "SELECT users.first_name, tweets.created_at, tweets.content, tweets.created_at FROM tweets JOIN users ON tweets.users_id = users.id ORDER BY tweets.created_at DESC" tweets = mysql.query_db(query) return render_template("dashboard.html", user=user[0], tweets=tweets)
def register(): pw_hash = bcrypt.generate_password_hash(request.form['password']) if len(request.form['first_name']) < 1: flash('First name required!') if len(request.form['last_name']) < 1: flash('Last name required!') if request.form['password'] != request.form['confirm_password']: flash('Passwords must match!') if not EMAIL_REGEX.match(request.form['email']): flash("Invalid email address!") if not '_flashes' in session.keys(): db = connectToMySQL('registration') query = "INSERT INTO users (first_name, last_name, email, password, created_at, updated_at) VALUES (%(fn)s, %(ln)s, %(em)s, %(pw)s, now(), now());" data = { 'fn': request.form['first_name'], 'ln': request.form['last_name'], 'pw': pw_hash, 'em': request.form['email'] } new_id = db.query_db(query, data) flash('Submission Successful!') return redirect('/')
def addFollow(id): db = connectToMySQL('registration') query = "INSERT INTO follows (users_id, follower_id) VALUES(" + str( session['id']) + ", " + id + ");" new_follow = db.query_db(query) return redirect('/users')
def add_like(id): db = connectToMySQL('registration') query = "SELECT * FROM likes where users_id = " + str( session['id']) + " and tweets_id = " + str(id) results = db.query_db(query) if results: flash("You already liked this tweet! Can't like it again!") return redirect('/success') db = connectToMySQL('registration') query = "INSERT INTO likes (users_id, tweets_id, created_at, updated_at) VALUES(%(id)s, %(tw)s, now(), now());" data = {'id': session['id'], 'tw': int(id)} update_like = db.query_db(query, data) return redirect('/success')
def login(): is_valid = True if len(request.form['lemail']) < 1: is_valid = False flash("Please enter your email") if len(request.form['lpassword']) < 1: is_valid = False flash("Please enter your password") if not is_valid: return redirect("/") else: mysql = connectToMySQL("dojo_tweets") query = "SELECT * FROM users WHERE email = %(email)s;" data = {"email": request.form["lemail"]} user = mysql.query_db(query, data) if not user: flash("User not found") return redirect("/") if not bcrypt.check_password_hash(user[0]["password"], request.form["lpassword"]): is_valid = False flash("Password is not valid") if is_valid: session["id"] = user[0]["id"] return redirect("/dashboard") else: flash("User not found") return redirect("/")
def login(): is_valid = True if len(request.form['lemail']) < 1: is_valid = False flash("Please enter your email") if len(request.form['lpassword']) < 1: is_valid = False flash("Please enter your password") if not is_valid: return redirect("/") else: mysql = connectToMySQL("basic_registration") query = "SELECT * FROM registration WHERE email = %(email)s;" data = { "email" : request.form["lemail"] } user = mysql.query_db(query, data) if user: hashed_password = user[0]["password"] if bcrypt.check_password_hash(user[0]["password"], request.form["lpassword"]): session["id"] = user[0]["id"] flash("You successfully logged in!") return redirect ("/success") else: flash("Please us a valid email address") return redirect ("/")
def user_unlike_tweet(tweet_id): mysql = connectToMySQL("dojo_tweets") query = "DELETE FROM liked_tweets WHERE user_id = %(user_id)s AND tweet_id = %(tweet_id)s" data = {"user_id": session["id"], "tweet_id": tweet_id} mysql.query_db(query, data) return redirect("/dashboard")
def info_display(id): id=id query2 = "SELECT * FROM friends where user_id=%(id)s;" data2 = { "id": id } mysql = connectToMySQL('users') friends = mysql.query_db(query2, data2) return render_template('show.html',id=id, friends=friends)
def edit(id): id=id query3= "SELECT * FROM friends where user_id=%(id)s;" data3= { "id":id } mysql= connectToMySQL('users') friends= mysql.query_db(query3, data3) return render_template('edit.html', id=id, friends=friends)
def destroy(id): id=id query4= "DELETE FROM friends where user_id=%(id)s;" data4={ "id":id } mysql= connectToMySQL('users') friends=mysql.query_db(query4, data4) return redirect('/')
def add_friend_to_db(): print(request.form) mysql = connectToMySQL('pets') data = {'pn': request.form['pname'], 'pt': request.form['ptype']} query = "INSERT INTO pets(name, type, created_at, updated_at) VALUES(%(pn)s, %(pt)s, NOW(), NOW());" new_pet_id = mysql.query_db(query, data) return redirect('/')
def landing(): if 'id' not in session: return redirect("/") mysql = connectToMySQL('basic_registration') query = "SELECT * FROM registration WHERE id = %(id)s" data = {'id': session['id']} user = mysql.query_db(query, data) return render_template("success.html", user=user[0])
def edit_tweet(tweet_id): if "id" not in session: flash("You can not edit tweets that are not your.") return redirect("/dashboard") query = "SELECT * FROM tweets WHERE id = %(id)s" data = {"id": tweet_id} mysql = connectToMySQL('dojo_tweets') results = mysql.query_db(query, data) return render_template("edit.html", tweet=results[0])
def add_a_user(): print(request.form) mysql = connectToMySQL('users') query = 'INSERT INTO friends(first_name, last_name, email, created_at) VALUES (%(fn)s, %(ln)s, %(email)s, NOW());' data= { 'fn': request.form['fname'], 'ln': request.form['lname'], 'email': request.form['mail'] } new_friend_id = mysql.query_db(query, data) return redirect('/')
def update(id): id=id mysql = connectToMySQL('users') query5= "UPDATE friends SET first_name = %(fn)s, last_name=%(ln)s, email=%(email)s WHERE user_id=%(id)s;" data5 = { "id": id, "fn":request.form['fname'], "ln": request.form['lname'], "email": request.form['mail'] } new_friend_id = mysql.query_db(query5, data5) return redirect('/')
def updateTweet(id): if len(request.form['edit']) < 1 or len(request.form['edit']) > 255: flash('Your tweet must be between 1 and 255 characters!') if not '_flashes' in session.keys(): db = connectToMySQL('registration') query = "UPDATE tweets set tweet = %(tw)s, updated_at = now() where id = " + id data = {'tw': request.form['edit']} updated = db.query_db(query, data) return redirect('/success') else: return redirect('/tweets/' + id + '/edit')
def tweet(): print(request.form) if len(request.form['tweet']) < 1 or len(request.form['tweet']) > 255: flash('Your tweet must be between 1 and 255 characters!') if not '_flashes' in session.keys(): db = connectToMySQL('registration') query = 'INSERT INTO tweets (users_id, tweet, created_at, updated_at) VALUES(%(id)s, %(tw)s, now(), now());' data = {'id': session['id'], 'tw': request.form['tweet']} new_tweet_id = db.query_db(query, data) return redirect('/success') else: return redirect('/success')
def validate_form(): is_valid = True if len(request.form["first_name"]) < 1: is_valid = False flash("First name can not be blank") if len(request.form["last_name"]) < 1: is_valid = False flash("Last name can not be blank") if len(request.form["email"]) < 1: is_valid = False flash("Email can not be blank") if not EMAIL_REGEX.match(request.form["email"]): flash("Invalid email address" ) if len(request.form["password"]) < 5: is_valid = False flash("Password must be at least 5 characters") if len(request.form["conf_pass"]) < 1: is_valid = False flash("Confirm password can not be blank") if request.form["conf_pass"] != request.form["password"]: is_valid = False flash("Passwords must match") if is_valid: pw_hash = bcrypt.generate_password_hash(request.form['password']) conf_pw_hash = bcrypt.generate_password_hash(request.form['conf_pass']) mysql = connectToMySQL("basic_registration") query = "INSERT INTO registration (first_name, last_name, email, password, pw_cnfm, created_at) VALUES (%(fname)s, %(lname)s, %(em)s, %(pass)s, %(cf_pass)s, now());" data = { "fname" : request.form['first_name'], "lname" : request.form['last_name'], "em" : request.form["email"], "pass" : pw_hash, "cf_pass" : conf_pw_hash } id = mysql.query_db(query, data) session["id"] = id flash("You successfully registered!") return redirect ("/success") else: return redirect("/")
def success(): if not 'userid' in session.keys(): flash('You must log in to see this page!') return redirect('/') else: db = connectToMySQL('registration') query = """SELECT tweets.id, tweets.tweet, tweets.created_at, tweets.users_id, CONCAT(users.first_name, ' ', users.last_name) as user_name, count(likes.tweets_id) as likes, follows.follower_id, follows.users_id as follower FROM users LEFT JOIN tweets on users.id = tweets.users_id LEFT JOIN likes on tweets.id = likes.tweets_id LEFT JOIN follows on users.id = follows.users_id WHERE tweets.id IS NOT NULL GROUP BY tweets.id ORDER BY tweets.created_at desc""" results = db.query_db(query) print('*' * 20, results) return render_template('/login.html', tweets=results)
def login(): db = connectToMySQL('registration') query = 'SELECT * from users where email = %(em)s' data = {'em': request.form['email']} results = db.query_db(query, data) if results: if bcrypt.check_password_hash(results[0]['password'], request.form['password']): session['userid'] = results[0]['email'] flash('Successful log in!') return redirect('/success') else: flash('You could not be logged in!') return redirect('/') else: flash('User not found!') return redirect('/')
def post_tweet(): if "id" not in session: return redirect("/") is_valid = True if len(request.form["post_tweet"]) < 1: is_valid = False flash("Tweet can not be blank") if len(request.form["post_tweet"]) > 255: is_valid = False flash("Tweet can not be more than 255 characters") if is_valid: mysql = connectToMySQL("dojo_tweets") query = "INSERT INTO tweets (content, users_id, created_at) VALUES (%(content)s, %(user_id)s, NOW())" data = { "user_id": session["id"], "content": request.form["post_tweet"] } mysql.query_db(query, data) return redirect("/dashboard") else: return redirect("/dashboard")
def validate_form(): is_valid = True if len(request.form["full_name"]) < 1: is_valid = False flash("Name can not be blank") if len(request.form["comments"]) > 120: is_valid = False flash("comment can not be more than 120 characters") if is_valid: mysql = connectToMySQL("validation") query = "INSERT INTO users (name, location, language, comment) VALUES (%(n)s, %(l)s, %(lan)s, %(com)s);" data = { "n": request.form['full_name'], "l": request.form['location'], "lan": request.form['language'], "com": request.form['comments'] } id = mysql.query_db(query, data) return redirect("/result/{}".format(id)) else: return redirect("/")
def update_tweet(tweet_id): if 'id' not in session: flash("You cannot edit tweets that are not yours.") return redirect("/") is_valid = True if len(request.form['update_tweet']) < 1: is_valid = False flash('Tweet cannot be blank') if len(request.form['update_tweet']) >= 256: is_valid = False flash('Tweet cannot be more than 255 characters') if is_valid: query = "UPDATE tweets SET content = %(content)s, updated_at = NOW() WHERE id = %(tid)s" data = {"content": request.form["update_tweet"], "tid": tweet_id} mysql = connectToMySQL("dojo_tweets") mysql.query_db(query, data) flash("Successfully updated") return redirect("/dashboard") else: return redirect("/edit_tweet/{}".format(tweet_id))
def survey_result(id): query = "SELECT * FROM users WHERE id = %(user_id)s" data = {"user_id": id} mysql = connectToMySQL("validation") survey_info = mysql.query_db(query, data) return render_template("result.html", result=survey_info[0])
def editTweet(id): db = connectToMySQL('registration') query = "SELECT tweet from tweets WHERE id = " + id result = db.query_db(query) return render_template('edit.html', id=id, results=result)
def users(): db = connectToMySQL('registration') query = "SELECT id, CONCAT(first_name, ' ', last_name) as name, email from users;" results = db.query_db(query) return render_template('users.html', users=results)
def index(): mysql = connectToMySQL('users') print('friends') friends = mysql.query_db('SELECT * FROM friends;') return render_template('all_users.html', all_friends=friends)
def delete_tweet(tweet_id): mysql = connectToMySQL("dojo_tweets") query = "Delete FROM tweets WHERE id = %(t_id)s AND users_id = %(u_id)s" data = {"t_id": tweet_id, "u_id": session["id"]} mysql.query_db(query, data) return redirect("/dashboard")
def user_liked_tweet(tweet_id): mysql = connectToMySQL("dojo_tweets") query = "INSERT INTO liked_tweets (user_id, tweet_id) VALUES (%(user_id)s, %(tweet_id)s)" data = {"user_id": session["id"], "tweet_id": tweet_id} mysql.query_db(query, data) return redirect("/dashboard")