Exemple #1
0
    def post(self, post_id, post):
        action = self.request.get("action")
        error_message = ""
        if action:
            if self.user_logged_in():
                if self.user_owns_post(post):
                    if action == 'like' or action == 'dislike':
                        error_message = 'You cannot like your own post'
                else:
                    if (action == 'like'):
                        if Post.likedPost(post, self.user.key()):
                            post.liked_user.append(self.user.key())
                            post.put()
                        else:
                            error_message = 'You can like only once'
                    elif (action == 'dislike'):
                        if Post.dislikedPost(post, self.user.key()):
                            post.disliked_user.append(self.user.key())
                            post.put()
                        else:
                            error_message = 'You cand dislike only once'

                # Adding Comment
                if (action == 'Add Comment'):
                    content = self.request.get("comment_content")
                    comment = Comment(parent=blog_key(),
                                      content=content,
                                      author=self.user,
                                      post=post)
                    comment.put()
                # Deleting Comments
                elif (action == 'Delete Comment'):
                    comment_id = self.request.get("comment_id")
                    key = db.Key.from_path('Comment',
                                           int(comment_id),
                                           parent=blog_key())
                    comment = db.get(key)
                    if comment:
                        if self.user_owns_comment(comment):
                            comment.delete()
                        else:
                            error_message = "You can only delete your own comments"
                elif (action == "Edit Comment"):
                    comment_id = self.request.get("comment_id")
                    comment_content = self.request.get("comment_edit_content")
                    key = db.Key.from_path('Comment',
                                           int(comment_id),
                                           parent=blog_key())
                    comment = db.get(key)
                    if comment:
                        if self.user_owns_comment(comment):
                            comment.content = comment_content
                            comment.put()
                        else:
                            error_message = "You can only edit your own comments"
                self.renderPage(post_id, post, error_message)
    def post(self, post_id, comment_id=None):
        """
        Adds a new comment to the post
        """
        content = self.request.get('content')

        key = db.Key.from_path('Post', int(post_id), parent=blog_key())
        post = db.get(key)

        if not post:
            self.error(404)
            return

        if not self.user:
            user_name = "Anon"
            # self.redirect('/blog')
        else:
            user_name = self.user.name

        if not content:
            self.render("permalink.html", post=post, comments=post.comments,
                        user=self.user,
                        error_comment="No text provided for comment")
            return
        # Create comment
        if not comment_id:
            comment = Comment(parent=blog_key(), post=post,
                              user_name=user_name, content=content)
        # Edit comment
        else:
            key = db.Key.from_path('Comment', int(comment_id),
                                   parent=blog_key())
            comment = db.get(key)
            if not comment:
                self.error(404)
                return
            # Make sure comment author is same as current user before
            # making the edit. If there is a mismatch, redirect to post page
            if not (comment.user_name == self.user.name):
                self.redirect('/blog/%s' % post_id)
                return
            comment.content = content
        comment.put()
        sleep(0.1)
        self.redirect('/blog/%s' % post_id)
Exemple #3
0
def comment_edit(comment_id):
    # check if post exists
    try:
        comment = Comment(comment_id)
    except:
        error_context = {
            'error_name': "404 Not Found",
            'error_info': "The comment you tried to access does not exist"
        }
        return render_template('error.html', **error_context)

    # check if user is logged in
    if not check.logged_in():
        error_context = {
            'error_name': "Unauthorized",
            'error_info': "You must log in first"
        }
        return render_template('error.html', **error_context)

    # check if user is OP
    if not (comment.user_id == session['user_id']):
        error_context = {
            'error_name': "Unauthorized",
            'error_info': "You are not the original poster of this comment"
        }
        return render_template('error.html', **error_context)

    # get POST input
    form = CommentForm(content=comment.content)
    if form.validate_on_submit():
        comment.content = form.content.data
        comment.content_html = md.render(form.content.data)
        comment.save()

        flash("Comment edited successfully")
        return redirect(
            url_for('post_pages.post_view', post_id=comment.post_id))

    return render_template('post_text_edit.html',
                           form=form,
                           body=comment.content,
                           name="Comment")
Exemple #4
0
def post_view(post_id):
    # get post info by id,
    # render post block
    # TO DO AFTER:
    # expose comment input
    # get all comments
    # group comments (nesting)
    # sort comments by opt
    """
    Expand this DOCSTRING
    """
    try:
        # get post object with all comments
        post = Post(post_id, True)
    except:
        error_context = {
            'error_name': "404 Not Found",
            'error_info': "The post you tried to access does not exist"
        }
        return render_template('error.html', **error_context)

    form = CommentForm()
    if form.validate_on_submit():
        if not check.logged_in():
            error_context = {
                'error_name':
                "403 Forbidden",
                'error_info':
                "You may not comment without an account. Please log in or create an account"
            }
            return render_template('error.html', **error_context)
        # create comment
        comment = Comment()
        comment.user_id = session['user_id']
        comment.post_id = post_id
        comment.content_type = 'text'
        comment.content = form.content.data
        comment.content_html = md.render(form.content.data)
        comment.is_external = False
        comment.date = datetime.now()
        comment.current_vote = 0

        comment.save()

        post.comment_count += 1
        post.save()

        flash('Comment created successfuly')
        # reload page with the new comment
        return redirect(url_for('post_pages.post_view', post_id=post_id))

    context = post.generate_context()
    # sets flag if viewer is logged in
    context['is_logged_in'] = check.logged_in()
    # sets flag if viewer is the original poster (a.k.a OP)
    context['is_op'] = context['is_logged_in'] and (post.user_id
                                                    == session['user_id'])
    if not context['is_op']:
        context['is_op'] = context['is_logged_in'] and TagModerator.is_mod(
            session['user_id'], post.tag_id)

    return render_template('post.html', **context, form=form)