def friend(): query = "SELECT users.id_user, users.first_name, users.last_name FROM users" mysql = connectToMySQL("runsafe") all_users = mysql.query_db(query) query = "SELECT * FROM friends WHERE friender = %(uid)s" data = {"uid": session['user_id']} mysql = connectToMySQL("runsafe") all_user_id_friended = [ info['friended'] for info in mysql.query_db(query, data) ] friended = [] not_friended = [] for user in all_users: if user['id_user'] == session['user_id']: continue if user['id_user'] in all_user_id_friended: friended.append(user) else: not_friended.append(user) return render_template("friends.html", friended=friended, not_friended=not_friended)
def create_user(): is_valid = True if len(request.form['name']) < 1: is_valid = False flash("sorry") if len(request.form['email']) < 1: is_valid = False flash("sorry") if len(request.form['location']) < 1: is_valid = False flash("sorry") if len(request.form['language']) < 1: is_valid = False flash("sorry") if not is_valid: return redirect("/") else: mysql = connectToMySQL('mydb') query = "INSERT INTO users (name, email, location, language) VALUES (%(n)s, %(em)s, %(lo)s,%(la)s);" data = { "n": request.form["name"], "em": request.form["email"], "lo": request.form["location"], "la": request.form["language"] } db = connectToMySQL("mydb") db.query_db(query, data) flash("Successfully Added!") return redirect("/show")
def rate(): query = "SELECT users.id_user, users.first_name, users.last_name FROM users" mysql = connectToMySQL("runsafe") all_users = mysql.query_db(query) query = "SELECT * FROM rates WHERE rater = %(uid)s" data = {"uid": session['user_id']} mysql = connectToMySQL("runsafe") all_user_id_rated = [info['rated'] for info in mysql.query_db(query, data)] hasnt_rated = [] good_rating = [] bad_rating = [] for user in all_users: if user['id_user'] == session['user_id']: hasnt_rated.append(user) elif user['id_user'] in all_user_id_rated: good_rating.append(user) else: bad_rating.append(user) return render_template("rate.html", hasnt_rated=hasnt_rated, good_rating=good_rating, bad_rating=bad_rating)
def home(): if 'user_id' not in session: return redirect('/') mysql = connectToMySQL('project1') query = "SELECT * FROM users WHERE users.user_id = %(id)s" data = {'id': session['user_id']} user = mysql.query_db(query, data) mysql = connectToMySQL('project1') query = "SELECT user_id1 FROM followed_user WHERE user_id = %(id)s" data = {'id': session['user_id']} followed_users = [user['user_id1'] for user in mysql.query_db(query, data)] if followed_users: mysql = connectToMySQL('project1') query = "SELECT * FROM users WHERE users.user_id = %(id)s" data = {'id': session['user_id']} user = mysql.query_db(query, data) mysql = connectToMySQL('project1') query = "SELECT users.user_id,users.username,posts.content,posts.post_id,count(likes.post_id) as tot,count(comments.post_id) as tot1 FROM posts join users on posts.user_id = users.user_id left join likes on posts.post_id= likes.post_id left join comments on comments.post_id=posts.post_id where posts.user_id in %(f)s or users.user_id=%(u)s group by posts.post_id order by posts.post_id desc" data1={ 'f':tuple(followed_users), 'u':session['user_id'] } photos = mysql.query_db(query,data1) return render_template("home.html", user=user[0],photoss=photos) return render_template("home.html",user=user[0])
def add_pet_to_db(): print(request.form) mysql = connectToMySQL('petshw') query = "INSERT INTO pets(name, type, created_at, updated_at) VALUES(%(n)s, %(t)s, NOW(), NOW());" data = {"n": request.form['pname'], "t": request.form['tname']} db = connectToMySQL('petshw') db.query_db(query, data) return redirect('/')
def add_users1(): is_valid = True if len(request.form['firstname']) < 2: is_valid = False flash("First name must be at least 2 characters long") if len(request.form['lastname']) < 2: is_valid = False flash("Last name must be at least 2 characters long") if len(request.form['password']) < 8: is_valid = False flash("Password must be at least 8 characters long") if len(request.form['username'])<1: is_valid=False flash("Fill in the username") mysql= connectToMySQL('project1') validate_email_query = 'SELECT user_id FROM users WHERE email=%(email)s;' form_data = { 'email': request.form['email'] } existing_users =mysql.query_db(validate_email_query, form_data) if existing_users: flash("Email already in use") is_valid = False mysql= connectToMySQL('project1') validate_email_query1 = 'SELECT user_id FROM users WHERE username=%(u)s;' form_data1 = { 'u': request.form['username'] } existing_users1 =mysql.query_db(validate_email_query1, form_data1) if existing_users1: flash("Username already in use") is_valid = False if is_valid: mysql = connectToMySQL('project1') # build my query query = "INSERT into users (firstName, lastName, email,username, password) VALUES (%(fn)s, %(ln)s, %(email)s, %(u)s, %(pass)s)" # pass revlevant to with my query data = { 'fn': request.form['firstname'], 'ln': request.form['lastname'], 'pass': bcrypt.generate_password_hash(request.form['password']), 'email': request.form['email'], 'u':request.form['username'] } # commit the query user_id = mysql.query_db(query, data) session['user_id'] = user_id return redirect("/success") else: # otherwise, reidrect and show errors return redirect("/register")
def add_friend_to_db(): mysql = connectToMySQL("cr_pets") query = "INSERT INTO pets (name, type, created_at, updated_at) VALUES (%(n)s, %(t)s, NOW(), NOW());" data = {"n": request.form["name"], "t": request.form["type"]} db = connectToMySQL('cr_pets') db.query_db(query, data) return redirect("/")
def process(): is_valid = True if not EMAIL_REGEX.match(request.form['email']): is_valid = False flash("Invalid email address!", 'email') query = "SELECT * FROM users WHERE email = %(email)s" data = { 'email': request.form['email'] } mysql = connectToMySQL('wish') results = mysql.query_db(query, data) print(results) #email validations if len(results) > 0: is_valid = False flash("Email already registered", 'email') if len(request.form['first_name']) < 1: is_valid = False flash("Hey! What's your name?", 'first_name') if len(request.form['last_name']) < 1: is_valid = False flash("But we don't even know your last name.", 'last_name') if len(request.form['password']) < 8: is_valid = False flash("Whoa! You can do better than that. Your password needs to be a bit longer.", 'password') if not request.form['password'] == request.form['password_confirmation']: is_valid = False flash("Those don't match bud.", 'password') if not is_valid: return redirect('/') else: pw_hash = bcrypt.generate_password_hash(request.form['password']) print(pw_hash) mysql = connectToMySQL('wish') query = "INSERT INTO users (first_name, last_name, email, password, created_at, updated_at)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 } id = mysql.query_db(query, data) print(id) session['userid'] = id return redirect('/show')
def edit(id): userid = session['userid'] mysql = connectToMySQL('trips') users_query = "SELECT first_name FROM users WHERE id=" + str(userid) first_name = mysql.query_db(users_query) mysql = connectToMySQL('trips') trips_query = "SELECT * FROM trips WHERE id=" + id trip = mysql.query_db(trips_query) return render_template('edit.html', first_name=first_name, trip=trip)
def see_post(post_id): mysql = connectToMySQL('solo_project') query = "SELECT posts.id, user_info.id as user_id, user_info.alias, posts.post FROM posts join user_info on user_info.id = posts.user_id where posts.id=%(id)s;" data = {'id': post_id} post_data = mysql.query_db(query, data) mysql = connectToMySQL('solo_project') query1 = "SELECT Distinct likes_info.posts_id, likes_info.users_id, user_info.alias, user_info.name FROM solo_project.likes_info join user_info on likes_info.users_id=user_info.id where posts_id = %(id)s;" data = {'id': post_id} likes_data = mysql.query_db(query1, data) return render_template("likes_info.html", post_data=post_data[0], likes_data=likes_data)
def add_friend_to_db(): print(request.form) mysql = connectToMySQL('first_flask') query = "INSERT INTO friends(first_name, last_name, occupation, created_at, updated_at) VALUES(%(fn)s, %(ln)s, %(occup)s, NOW(), NOW());" data = { "fn": request.form['fname'], "ln": request.form['lname'], "occup": request.form['occ'] } db = connectToMySQL('first_flask') db.query_db(query, data) return redirect('/')
def edit(): userid = session['userid'] mysql = connectToMySQL('wish') users_query = "SELECT first_name FROM users WHERE id=" +str(userid) first_name = mysql.query_db(users_query) mysql = connectToMySQL('wish') query = "SELECT * FROM wishes WHERE user_id=" +str(session['userid']) wishes = mysql.query_db(query) return render_template('edit.html', first_name=first_name, wishes=wishes)
def user_info(user_id): mysql = connectToMySQL('solo_project') query = 'SELECT count(posts.user_id) as t_posts, user_info.name, user_info.alias, user_info.email FROM user_info join posts on posts.user_id = user_info.id where user_id = %(id)s;' data = {"id": user_id} user_info = mysql.query_db(query, data) mysql = connectToMySQL('solo_project') query = 'SELECT count(likes_info.users_id) as t_likes from user_info join likes_info on likes_info.users_id = user_info.id where users_id=%(id)s;' data1 = {"id": user_id} user_likes = mysql.query_db(query, data1) return render_template("user_info.html", info=user_info[0], t_likes=user_likes[0])
def landing(): if 'user_id' not in session: return redirect("/") mysql = connectToMySQL("dojo_tweets") query = "SELECT first_name FROM users WHERE user_id = %(u_id)s" data = {'u_id': session['user_id']} user_data = mysql.query_db(query, data) if user_data: user_data = user_data[0] else: return redirect("/") mysql = connectToMySQL("dojo_tweets") query = "SELECT tweets.author, tweets.tweet_id, tweets.message, tweets.created_at, users.first_name, users.last_name FROM tweets JOIN users ON tweets.author = users.user_id" tweets = mysql.query_db(query) mysql = connectToMySQL("dojo_tweets") query = "SELECT tweet_like FROM users_likes WHERE user_like = %(user_id)s" data = {'user_id': session['user_id']} liked_tweets = [ tweet['tweet_like'] for tweet in mysql.query_db(query, data) ] #for loop that creates a list of all the liked tweets, also known as list comprehension mysql = connectToMySQL("dojo_tweets") query = "SELECT tweet_like, COUNT(tweet_like) as like_count FROM users_likes GROUP BY tweet_like" like_count = mysql.query_db(query) for tweet in tweets: td = datetime.now() - tweet['created_at'] if td.seconds == 0: tweet['time_since_secs'] = 1 if td.seconds < 60 and td.seconds > 0: tweet['time_since_secs'] = td.seconds if td.seconds < 3600: tweet['time_since_minutes'] = round(td.seconds / 60) if td.seconds > 3600: tweet['time_since_hours'] = round(td.seconds / 3600) if td.days > 0: tweet['time_since_days'] = td.days for like in like_count: if like['tweet_like'] == tweet['tweet_id']: tweet['like_count'] = like['like_count'] if 'like_count' not in tweet: tweet['like_count'] = 0 return render_template("landing.html", user=user_data, tweets=tweets, liked_tweets=liked_tweets)
def post(post_id): query="SELECT post_id,content from posts where post_id=%(u)s" data={ 'u':post_id } mysql = connectToMySQL('project1') foto=mysql.query_db(query, data) query="select users.username,comments.content from users join comments on users.user_id=comments.user_id where comments.post_id=%(u)s" mysql = connectToMySQL('project1') comments=mysql.query_db(query, data) print(comments) return render_template('post.html',foto=foto[0],comments=comments)
def show(): userid = session['userid'] mysql = connectToMySQL('trips') query = "SELECT first_name FROM users WHERE id=" + str(userid) first_name = mysql.query_db(query) mysql = connectToMySQL('trips') trips_query = "SELECT * FROM trips WHERE users_id=" + str( session['userid']) trips = mysql.query_db(trips_query) return render_template('show.html', first_name=first_name, trips=trips)
def success_login(): query = "SELECT posts.user_id, posts.id, user_info.alias, posts.post FROM posts join user_info on user_info.id = posts.user_id;" mysql1 = connectToMySQL('solo_project') result = mysql1.query_db(query) l_count = {} for k in result: print(k['id']) query1 = "SELECT count(*) as likes FROM likes_info where posts_id=%(id)s;" data = {'id': k['id']} mysql1 = connectToMySQL('solo_project') result1 = mysql1.query_db(query1, data) l_count[k['id']] = result1[0]['likes'] return render_template('index.html', ideas=result, likes=l_count)
def join_home(): query = "SELECT users.first_name, users.last_name FROM users where id_user = %(uid)s" data = { 'uid': session['user_id'], } mysql = connectToMySQL("runsafe") results = mysql.query_db(query, data) if results: query = "SELECT events.id_event, events.host, events.content, users.first_name, users.last_name FROM users JOIN events ON users.id_user = events.host" mysql = connectToMySQL("runsafe") events = mysql.query_db(query) return render_template("join.html", user_data=results[0], events=events)
def delete_t(post_id): # if ON DELETE CASCADE is not set up for tweets DELETE likes first query = "DELETE FROM likes WHERE post_id = %(t)s" data = { 't':post_id } mysql = connectToMySQL('project1') mysql.query_db(query, data) query = "DELETE FROM posts WHERE post_id = %(t)s" mysql = connectToMySQL('project1') mysql.query_db(query, data) return redirect("/success")
def delete_email(id): session["logged"] = False query = "DELETE FROM email WHERE id = %(id)s" data = {"id": id} mysql = connectToMySQL("emailvalid") mysql.query_db(query, data) return redirect("/success")
def create_client(): is_valid = True if len(request.form["name"]) < 1: is_valid = False flash("Please enter your name") if len(request.form["location"]) < 2: is_valid = False flash("Please enter your dojo location") if len(request.form["language"]) < 2: is_valid = False flash("Please enter your favorite language") if is_valid: print("Got Post Info") print(request.form) query = "INSERT INTO dojosurvey (fullname, location, favlang, comment) VALUES (%(fn)s, %(lo)s, %(fl)s, %(co)s);" data = { "fn" : request.form["name"], "lo" : request.form["location"], "fl" : request.form["language"], "co" : request.form["comment"] } session["name"] = request.form["name"] session["location"] = request.form["location"] session["language"] = request.form["language"] session["comment"] = request.form["comment"] mysql = connectToMySQL("dojosurvey") mysql.query_db(query,data) # flash("Survey form successful!") return redirect ("/result") else: return redirect("/")
def login(): is_valid = True if len(request.form['email']) < 1 or len(request.form['pwd']) < 1: is_valid = False flash("Please enter your e-mail and password") return redirect('/main') if not EMAIL_REGEX.match(request.form['email']): is_valid = False flash("Invalid email address!") return redirect('/main') if not PASS_REGEX.match(request.form['pwd']): is_valid = False flash("Invalid password type!") return redirect('/main') if is_valid: query = "SELECT * from user_info where email=%(eml)s;" data = {'eml': request.form['email']} mysql = connectToMySQL('solo_project') result = mysql.query_db(query, data) if result: if bcrypt.check_password_hash(result[0]['password'], request.form['pwd']): session['userid'] = result[0]['id'] session['alias'] = result[0]['alias'] return redirect('/ideas') flash("You could not be logged in") return redirect("/main")
def login(): is_valid = True if len(request.form['email']) < 1: is_valid = False flash('Please enter your email') if len(request.form['pword']) < 1: is_valid = False flash('Please enter your password') if not EMAIL_REGEX.match(request.form['email']): flash('Invalid email address') if not is_valid: return redirect('/') else: query = 'SELECT * FROM users WHERE users.email = %(em)s' data = {'em': request.form.get('email')} mysql = connectToMySQL('inventory') user = mysql.query_db(query, data) print(user) if user: hashed_password = user[0]['password'] if bcrypt.check_password_hash(hashed_password, request.form['pword']): session['user_id'] = user[0]['id'] session['user_name'] = user[0]['first_name'] return redirect('/welcome') else: flash('Password incorrect') return redirect('/')
def process_form(): is_valid = True if len(request.form['name']) < 1: is_valid = False flash("Oops! Looks like you forgot to enter your name.") if len(request.form['email']) < 1: is_valid = False flash("Uh oh! What's your email?") if not is_valid: return redirect('/') else: mysql = connectToMySQL('survey') query = "INSERT INTO users (name, email, location, language, created_at, updated_at) VALUES (%(name)s, %(email)s, %(location)s, %(language)s, NOW(), NOW());" data = { 'name' : request.form['name'], 'email' : request.form['email'], 'location' : request.form['location'], 'language' : request.form['language'] } id = mysql.query_db(query, data) flash("Survey Completed!") return redirect('/show/' + str(id))
def edit_user(id): query = "SELECT * FROM r_users WHERE user_id = %(id)s" data = {"id": id} mysql = connectToMySQL("restful_users") edit_info = mysql.query_db(query, data) print(id) return render_template("update.html", edit_info=edit_info)
def purchase(): mysql = connectToMySQL('amadon') query = "SELECT price FROM items WHERE id=" + id data = {'price': request.form['price']} mysql.query_db(query, data) print('price') return redirect('/checkout/' + str(id))
def discover(): mysql=connectToMySQL('project1') query="select users.username,users.user_id from users where users.user_id<>%(u)s" data={'u':session['user_id']} users=mysql.query_db(query,data) return render_template('discover.html',users=users)
def logIn(): is_valid = True if len(request.form['username']) < 1: is_valid = False flash("Please enter your username") if len(request.form['password']) < 1: is_valid = False flash("Please enter your password") if not is_valid: return redirect("/") else: mysql = connectToMySQL('project1') query = "SELECT * FROM users WHERE username = %(u)s" data = { 'u': request.form['username'] } user = mysql.query_db(query, data) if user: hashed_password = user[0]['password'] if bcrypt.check_password_hash(hashed_password, request.form['password']): session['user_id'] = user[0]['user_id'] return redirect("/success",) else: flash("Password is invalid") return redirect("/") else: flash("Please use a valid email address") return redirect("/")
def tweet_detail(t_id): mysql = connectToMySQL("dojo_tweets") query = "SELECT tweet_id, tweets.message, tweets.created_at, users.first_name, users.last_name FROM tweets JOIN users ON tweets.author = users.user_id WHERE tweet_id = %(t_id)s" data = {'t_id': t_id} tweet_data = mysql.query_db(query, data) return render_template("detail.html", tweet_data=tweet_data)
def on_login(): is_valid = True if len(request.form['em']) < 1: is_valid = False flash("Email cannot be blank.") if not EMAIL_REGEX.match(request.form['em']): is_valid = False flash("Please use a valid email.") if is_valid: mysql = connectToMySQL('login_registration') query = "SELECT user_id, email, password FROM users WHERE email = %(em)s" data = {'em': request.form['em']} user_data = mysql.query_db(query, data) if user_data: user = user_data[0] if bcrypt.check_password_hash(user_data[0]['password'], request.form['pw']): session['user_id'] = user['user_id'] return redirect("/success") else: flash("Password is invalid") return redirect("/") else: flash("Email is not valid.") return redirect("/") else: return redirect("/")