def viewposts(): if request.method == "GET": posts = utils.displayposts() if utils.checksession(session): user = session['username'] else: user = None return render_template('view_posts.html',posts = posts, user=user) else: if utils.checksession(session): user=session['username'] updatepostinput = request.form['updatepost'][0:2] print(updatepostinput) # create a post when the button "Bloginate!" is clicked if updatepostinput == "cr": if 'username' in session: post=request.form['posttext'] utils.createpost(user,post) return redirect(url_for('viewposts')) else: return redirect(url_for('login')) # from viewposts homepage, user clicked "Write a comment" elif updatepostinput == "wc": postid = request.form['updatepost'][2:] post = utils.getpost(postid) return render_template('createcomment.html',postid = postid, user = user, post = post) # from the createcomments page, user submitted comment elif updatepostinput == "pc": comment = request.form['comment'] postid = request.form['updatepost'][2:] utils.createcomment(postid,user,comment) return redirect(url_for('viewposts')) # from viewposts homepage, user click "Edit post" elif updatepostinput == "ep": postid = request.form['updatepost'][2:] post = utils.getpost(postid) return render_template('editpost.html',postid = postid, post = post, user = user) elif updatepostinput == "dp": postid = request.form['updatepost'][2:] utils.deletepost(postid) return redirect(url_for('viewposts')) elif updatepostinput == "up": postid = request.form['updatepost'][2:] print(postid) post = request.form['editpost'] utils.editpost(postid,user,post) return redirect(url_for('viewposts')) else: return redirect(url_for('login')) else: return redirect(url_for('login'))
def user_profile(username=''): if utils.checksession(session): user = session['username'] else: user = None posts = utils.finduserposts(username) return render_template("profile.html", username=username, user=user, posts=posts)
def createcomment(postid): if utils.checksession(session): user = session.get('username') else: return redirect('/view_posts') if request.method == "GET": post = utils.getpost(postid) return render_template('createcomment.html',postid = post['_id'], user = post['username'], post = post['post']) else: post = request.form['comment'] postid = request.form['updatepost'] utils.createcomment(postid, user, post) return redirect('/view_posts')
def logout(): if utils.checksession(session): session.pop('username', None) session.pop('id', None) return redirect('/view_posts')