Пример #1
0
def posts():
    postList = query.get_all_pids()
    #postList.reverse()
    postDict = []
    for pid in postList:
        title = query.get_post(pid)['title'] 
        content = query.get_post(pid)["content"]
        topComment = ""
        if len(query.get_comments_for_post(pid)) > 0:
            topCommentID = query.get_comments_for_post(pid)[0]["comment_id"]
            topComment = query.get_comment_contents(topCommentID)
        postDict.append({'pid':pid,'title':title,'topComment':topComment,'content':content})
    if request.method == "GET":
        #postDict format: {pid: {'title':, 'topComment':}, pid:{...}, ...}
        return render_template("allposts.html", POST_DICT = postDict)
    else:
        assert(request.method == "POST")
        if not session['logged_in']:
            return render_template("allposts.html", POST_DICT = postDict, error = "Please log in before you post!")
        else:
            print request.form
            if 'newpost' in request.form:
                query.addPost(session["UID"],request.form['title'], request.form['content'])
                return redirect(url_for("posts"))
            else:
                print("ERROR: NOT ADDED")
                return redirect(url_for("posts"))
Пример #2
0
def posts():
    postList = query.get_all_pids()
    postList.reverse()
    postDict = []
    for pid in postList:
        title = query.get_post(pid)["title"]
        content = query.get_post(pid)["content"]
        topComment = ""
        if len(query.get_comments_for_post(pid)) > 0:
            topCommentID = query.get_comments_for_post(pid)[0]["comment_id"]
            topComment = query.get_comment_contents(topCommentID)
        postDict.append({
            'pid': pid,
            'title': title,
            'topComment': topComment,
            'content': content
        })
    if request.method == "GET":
        #postDict format: {pid: {'title':, 'topComment':}, pid:{...}, ...}
        return render_template("allposts.html", POST_DICT=postDict)
    else:
        assert (request.method == "POST")
        if not session['logged_in']:
            return render_template("allposts.html",
                                   POST_DICT=postDict,
                                   error="Please log in before you post!")
        else:
            print request.form
            if 'newpost' in request.form:
                query.addPost(session["UID"], request.form['title'],
                              request.form['content'])
                return redirect(url_for("posts"))
            else:
                print("ERROR: NOT ADDED")
                return redirect(url_for("posts"))
Пример #3
0
def profile():
    postList = query.get_posts_by_user(session['UID']) #post ids
    postDict = []
    for post in postList:
        d = query.get_post(post) # {"title":-, "contents":-, "username":-,}
        title = d['title']
        listOfComments = query.get_comments_for_post(post)
        topComment = ""
        if len(listOfComments) > 0:
            topCommentID = listOfComments[-1]["comment_id"]
            topComment = query.get_comment_contents(topCommentID)
        postDict.append({'title':title, 'topComment':topComment})
    return render_template("userposts.html", POST_LIST = postDict)
Пример #4
0
def profile():
    postList = query.get_posts_by_user(session['UID'])  #post ids
    postDict = []
    for post in postList:
        d = query.get_post(post)  # {"title":-, "contents":-, "username":-,}
        title = d['title']
        listOfComments = query.get_comments_for_post(post)
        topComment = ""
        if len(listOfComments) > 0:
            topCommentID = listOfComments[-1]["comment_id"]
            topComment = query.get_comment_contents(topCommentID)
        postDict.append({'title': title, 'topComment': topComment})
    return render_template("userposts.html", POST_LIST=postDict)
Пример #5
0
def onepost(pid=-1):
    pid = int(pid)
    if len(query.get_post(pid)) == 0:
        return redirect(url_for("posts"))
    if request.method == "GET":
        commentList = query.get_comments_for_post(pid)
        for comment in commentList:
            comment["contents"] = query.get_comment_contents(comment["comment_id"])
        return render_template("post.html", POST_CONTENT = query.get_post(pid), COMMENT_LIST = commentList)
    else:
        assert(request.method == "POST")
        if 'add_comment' in request.form:
            query.addComment(session['UID'], pid, request.form['comment_content'])
            return redirect(url_for("onepost", pid=pid))
        elif 'del_comment' in request.form:
            if session['UID'] == query.get_uid_from_post(pid):
                query.delPost(pid)
            return redirect(url_for("posts"))
        else:
            return redirect(url_for("posts"))
Пример #6
0
def onepost(pid=-1):
    pid = int(pid)
    if len(query.get_post(pid)) == 0:
        return redirect(url_for("posts"))
    if request.method == "GET":
        commentList = query.get_comments_for_post(pid)
        for comment in commentList:
            comment["contents"] = query.get_comment_contents(
                comment["comment_id"])
        return render_template("post.html",
                               POST_CONTENT=query.get_post(pid),
                               COMMENT_LIST=commentList)
    else:
        assert (request.method == "POST")
        if 'add_comment' in request.form:
            query.addComment(session['UID'], pid,
                             request.form['comment_content'])
            return redirect(url_for("onepost", pid=pid))
        elif 'del_comment' in request.form:
            if session['UID'] == query.get_uid_from_post(pid):
                query.delPost(pid)
            return redirect(url_for("posts"))
        else:
            return redirect(url_for("posts"))