def new_post(username): # Check user authentication if check_user_auth(username): user = User.query.filter_by(username=username).first() # check method of request if request.method == "POST": # get all posts of this user: posts = Post.query.filter_by(username=username).order_by( Post.post_date.desc()).all() # create new post post = Post() caption = request.form.get("caption") post.user = user post.caption = caption # upload post picture this_file = request.files.get("picture") post.picture = upload_file(this_file, username) db.session.add(post) db.session.commit() return redirect(url_for("post.post_index", username=username)) if request.method == "GET": return render_template("new-post.html", user=user) else: return "You have not any access to this page"
def profile_index(username): # Check User Auth.. if check_user_auth(username): user = User.query.filter_by(username=username).first() # Join Follow and post for get post of followers of this user following_cnd = Follow.query.filter_by( follower_username=username).all() query = db.session.query( Follow, Post).filter(Follow.follower_username == user.username).filter( Follow.following_username == Post.username).order_by( Post.post_date.desc()).all() print(query) POSTS_INFO = [] for q in query: post_like_number = len( Like.query.filter_by(like_recipient=q.Post.username, post_date=q.Post.post_date).all()) POSTS_INFO.append({ "Post": q.Post, "Follow": q.Follow, "Likes": post_like_number }) return render_template("profile.html", user=user, POSTS_INFO=POSTS_INFO) else: return "You have not access to this page"
def search(): loged_in_username = session.get("username") if check_user_auth(loged_in_username): q = request.args.get("q") name_cnd = User.name.ilike(f'%{q}%') lastname_cnd = User.lastname.ilike(f'%{q}%') username_cnd = User.username.ilike(f'%{q}%') ### using or_ SQLAlchemy : by default is and # before pagination # found_users = User.query.filter(or_( # name_cnd, # lastname_cnd, # username_cnd # )).all() # pagination p = request.args.get("p", default=1, type=int) found_users = User.query.filter( or_(name_cnd, lastname_cnd, username_cnd)).paginate(p, 5) print(found_users) print(get_debug_queries()) ### show queries that was sent to database loged_in_user = User.query.filter_by( username=loged_in_username).first() return render_template("search.html", loged_in_user=loged_in_user, users=found_users, q=q)
def delete_post(username, date): if check_user_auth(username): post = Post.query.filter_by(username=username, post_date=date).first() db.session.delete(post) db.session.commit() user = User.query.filter_by(username=username).first() posts = Post.query.filter_by(username=username) return redirect(url_for("post.post_index", username=username))
def direct_index(username): # check user authentication if check_user_auth(username): user = User.query.filter_by(username=username).first() # get all directmessages of this user: direct_messages = Direct_Message.query.filter_by( recipient_username=username).order_by( Direct_Message.sending_date.desc()).all() return render_template("direct-message.html", user=user, direct_messages=direct_messages)
def unfollow_process(username): loged_in_username = session.get("username") if check_user_auth(loged_in_username): f = Follow.query.filter_by(follower_username=loged_in_username, following_username=username).first() if f: user = User.query.filter_by(username=username).first() # UNFOLLOW db.session.delete(f) db.session.commit() return redirect( url_for("user_page.user_page_index", username=username))
def send_message(username): loged_in_username = session.get("username") if check_user_auth(loged_in_username): loged_in_user = User.query.filter_by( username=loged_in_username).first() user = User.query.filter_by(username=username).first() msg = Direct_Message() msg.sender = loged_in_user msg.recipient = user if request.form.get("message_text"): msg.message_text = request.form.get("message_text") db.session.add(msg) db.session.commit() return redirect(url_for('direct.chat', username=username))
def follow_process(username): loged_in_username = session.get("username") if check_user_auth(loged_in_username): f = Follow.query.filter_by(follower_username=loged_in_username, following_username=username).first() if not f: loged_in_user = User.query.filter_by( username=loged_in_username).first() user = User.query.filter_by(username=username).first() # FOLLOW f = Follow() f.follower = loged_in_user f.following = user db.session.add(f) db.session.commit() return redirect( url_for("user_page.user_page_index", username=username))
def setting_index(username): # Check User Auth if check_user_auth(username): # Upload User Avatar: if request.method == "POST": this_file = request.files.get("filename") # Get File # Check file : if this_file: avatar_path = upload_file(this_file, username) user = User.query.filter_by(username=username).first() user.avatar = avatar_path db.session.commit() user = User.query.filter_by(username=username).first() avatar = user.avatar return render_template("setting.html", user=user) else: return "You have not any access to this page."
def post_index(username): # Check user authentication if check_user_auth(username): # get all posts of this user: user = User.query.filter_by(username=username).first() posts = Post.query.filter_by(username=username).order_by( Post.post_date.desc()).all() follower_number = len( Follow.query.filter_by(following_username=username).all()) following_number = len( Follow.query.filter_by(follower_username=username).all()) return render_template("post.html", user=user, posts=posts, follower_number=follower_number, following_number=following_number) else: return "You have not any access to this page"
def chat(username): loged_in_username = session.get("username") if check_user_auth(loged_in_username): loged_in_user = User.query.filter_by( username=loged_in_username).first() user = User.query.filter_by(username=username).first() cnd_1 = Direct_Message.query.filter_by( sender_username=loged_in_username, recipient_username=username) cnd_2 = Direct_Message.query.filter_by( sender_username=username, recipient_username=loged_in_username) ### using or_ SQLAlchemy : by default is and messages = cnd_1.union(cnd_2) messages = messages.order_by(Direct_Message.sending_date.asc()) return render_template("single-direct-message.html", messages=messages, user=user, loged_in_user=loged_in_user)