Exemplo n.º 1
0
def comment():
    global postID, commentText

    commentForm = CommentForm()
    commentFormAfterCheck = CommentFormAfterCheck()

    # We come here twice, from different forms, only use it once.
    if 'postID' in request.form:
        postID = request.form['postID']

    if commentForm.validate_on_submit():
        commentText = commentForm.commentText.data
        response = requests.post(urlsConfig.URLS['profanity_url'],
                                 params={'text': commentText})

        # Only show div is post contained a bad word
        if response.text == "BAD":
            return render_template("comment.html",
                                   title="Comment",
                                   commentForm=commentForm,
                                   commentFormAfterCheck=commentFormAfterCheck,
                                   display='')
        elif response.text == "GOOD":
            comment = Comment(commentText=commentText,
                              user="******",
                              postID=postID,
                              timestamp=datetime.now())
            commentDB.session.add(comment)
            commentDB.session.commit()

            print("Successfully placed a comment!")
            return redirect(urlsConfig.URLS['newsfeed_url'])

    if commentFormAfterCheck.validate_on_submit():
        if commentFormAfterCheck.submitAfterCheck.data:
            comment = Comment(commentText=commentText,
                              user="******",
                              postID=postID,
                              timestamp=datetime.now())
            commentDB.session.add(comment)
            commentDB.session.commit()

            flash("Successfully created a new comment!")
            return redirect(urlsConfig.URLS['newsfeed_url'])

        elif commentFormAfterCheck.discardAfterCheck.data:
            print("Pressed discard")
            return redirect(url_for("comment"))
        else:
            pass

    return render_template("comment.html",
                           title="Comment",
                           commentForm=commentForm,
                           commentFormAfterCheck=commentFormAfterCheck,
                           display='none')