def story(id, title): # defining variables # posts = Story.query.all() id = int(id) story = Story.query.get(id) form = LoginForm() # defining variables end # checking if the story exist if story: blog_title = Story.query.get(id).title comments = Comment.query.filter_by(story=story).order_by( Comment.date_posted.desc()) return render_template('story.html', title=blog_title, story=story, form=form, comments=comments, timeago=timeago, datetime=datetime) else: flash("We Couldn't Find That Story", 'danger') return redirect(url_for('home'))
def home(): # defining variables form = LoginForm() story = Story posts = Story.query.order_by(Story.date_posted.desc()).all() # defining variables end return render_template('home.html', posts=posts, story=story, form=form, current_year=current_year)
def dashboard(username): # getting url parameters (args) status = request.args.get("status") # getting user from database user = User.query.filter_by(username=username).first() # getting user from database # allowing access only if user exist if user: # defining variables if len(user.profile_bg.split("//")) > 1: bg_img = user.profile_bg else: bg_img = url_for('static', filename='img/profile_bg/' + user.profile_bg) story = Story storylikes = Storylikes posts = Story.query.filter_by(author=user).order_by( Story.date_posted.desc()) blog_title = f"@{username} Stories In One View" form = ProfileForm() login_form = LoginForm() about = user.about post_count = posts.count() profile_pic = url_for('static', filename='img/profile_pic/' + user.profile_pic) # defining variables end # submitting profile form if form.validate_on_submit(): # commiting changes to database current_user.username = form.username.data current_user.email = form.email.data current_user.about = form.about.data if form.profile_pic.data: pic_path = pic_save(form.profile_pic.data) current_user.profile_pic = pic_path if form.profile_bg.data: bg_path = bg_save(form.profile_bg.data) current_user.profile_bg = bg_path db.session.commit() # commiting changes to database end # flashing message flash('Your Profile Was Updated', 'success') # redirecting user return redirect("/" + current_user.username + '#success') # populating form with user's default data elif request.method == "GET": if current_user.is_authenticated: form.username.data = current_user.username form.email.data = current_user.email form.about.data = current_user.about # populating form with user's default data end # checking if URL Parameter exisits before rendring if status == 'new-post': flash('Your Story is Live', 'success') return render_template('dashboard.html', title=blog_title, about=about, username=username, bg_img=bg_img, profile_pic=profile_pic, posts=posts, form=form, login_form=login_form, story=story, storylikes=storylikes, post_count=post_count, user=user) elif status == 'updated-post': flash('Your Story Has Been Updated', 'success') return render_template('dashboard.html', title=blog_title, about=about, username=username, bg_img=bg_img, profile_pic=profile_pic, posts=posts, form=form, login_form=login_form, story=story, storylikes=storylikes, post_count=post_count, user=user) else: return render_template('dashboard.html', title=blog_title, about=about, username=username, bg_img=bg_img, profile_pic=profile_pic, posts=posts, form=form, login_form=login_form, story=story, storylikes=storylikes, post_count=post_count, user=user) # checking if URL Parameter exisits before rendring end else: flash('That User Does Not Exist', 'danger') return redirect(url_for('home'))
def login(): user_data = request.get_json() # defining variables form = LoginForm() blog_title = "Sign In | Start Telling Your Stories" # defining variables end if user_data: email = user_data['email'] password = user_data['password'] remember_me = user_data['remember_me'] existing_user = User.query.filter_by(email=email.lower()).first() if existing_user and bcrypt.check_password_hash( existing_user.password, password): # logging user in using flask login (login_user) for session management login_user(existing_user, remember=remember_me) return jsonify({"message": "Login Successful"}), 200 elif existing_user: raise RequestError('Invalid Password') else: raise RequestError('Invalid Credentials') # redirecting using for accessing page if already logged in if current_user.is_authenticated: return redirect(url_for('dashboard', username=current_user.username)) # redirecting using for accessing page if already logged in end # Validating form on submit if form.validate_on_submit(): # checking if user exists existing_user = User.query.filter_by( email=form.email.data.lower()).first() # checking if password matches signup password if existing_user and bcrypt.check_password_hash( existing_user.password, form.password.data): # logging user in using flask login (login_user) for session management login_user(existing_user, remember=form.remember_me.data) next_page = request.args.get("next") # sending flased message flash("Login Successful", "success") # logging user in using flask login (login_user) for session management end # redirecting user to dashboard return redirect(next_page) if next_page else redirect( url_for('dashboard', username=existing_user.username)) # redirecting user to dashboard end # checking if user exists end elif existing_user: flash("Invalid Sign In Password", "danger") else: flash("That User Does not Exist", "danger") # checking if password matches signup password end # sending flased message end # Validating form on submit end return render_template('login.html', form=form, title=blog_title, current_year=current_year)