Exemplo n.º 1
0
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'))
Exemplo n.º 2
0
def viewposts():
    print 'in viewposts'
    if request.method == "GET":
        posts = utils.displayposts()
        if len(session.keys())!=0:
            user=session[session.keys()[0]]
        else:
            user=''
        return render_template('view_posts.html',posts = reversed(posts), user=user)
    else:
        print session.keys()
        if len(session.keys())!=0:
            print 'SEOTHING'
            user = session[session.keys()[0]]
            print request.form
            # create a post when the button "Bloginate!" is clicked
            if request.form['updatepost'] == 'createpost':
                 print 'creating new post'
                 if 'username' in session:
                     post=request.form['posttext']
                     newpostid=utils.nextpostid()
                     utils.createpost(newpostid,user,post)
                     return redirect(url_for('viewposts'))
                 else:
                     return "you are not logged in"
            # either edit the specific post or delete the post
            elif utils.is_number(request.form['updatepost']):
                if float(request.form['updatepost']) > 0:
                    postid = request.form['updatepost']
                    post = utils.getpost(postid)
                    return render_template('editpost.html',postid = postid, user = user, post = post)
                else:
                    postid = -float(request.form['updatepost'])
                    utils.deletepost(postid)
                    return redirect(url_for('viewposts'))
            elif utils.is_number(request.form['updatepost'][2:]):
                updatepostinput = request.form['updatepost'][0:2]
                # from viewposts homepage, user clicked "Write a comment"
                if updatepostinput == "cc":
                    postid = request.form['updatepost']
                    postid = float(postid[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 = float(request.form['updatepost'][2:])
                    newcommentid = utils.nextcommentid(postid)
                    utils.createcomment(postid,newcommentid,user,comment)
                    return redirect(url_for('viewposts'))             
                # from viewposts homepage, user click "Edit post"
                elif updatepostinput == "ep":
                    post = request.form['editpost']
                    postid = float(request.form['updatepost'][2:])
                    utils.editpost(postid,user,post)
                    return redirect(url_for('viewposts'))                   
            else:
                print 'ELSE'
                return redirect(url_for('login'))
        else:
            return redirect(url_for('login'))