Ejemplo n.º 1
0
def flag(uuid):
    post_id = decode_id(uuid)
    post = Post.query.get_or_404(post_id)
    # Get post
    user_post_rating = UserPostRating.query.filter_by(post_id=post.id, user_id=current_user.id).first()
    # Check if user flagged the post
    if user_post_rating:
        # If the post was previously flagged
        if user_post_rating.is_flagged == True:
            user_post_rating.is_flagged = 0
            post.user.karma.negative += 5
        else:
            user_post_rating.is_flagged = 1
            post.user.karma.negative -= 5
    else:
        user_post_rating = UserPostRating(user_id=current_user.id, post_id=post.id, is_flagged=True)
        post.user.karma.negative += 5
        db.session.add(user_post_rating)

    # Commit changes so far
    db.session.commit()

    # Check if the post has been flagged multiple times, currently
    # the value is hardcoded to 5
    flags = UserPostRating.query.filter_by(post_id=post.id, is_flagged=True).all()
    if len(flags) > 5:
        post.status = "flagged"
    post.user.update_karma()

    # Clear all the caches
    delete_redis_cache_keys("post_list")
    delete_redis_cache_keys("post_list", post.category.url)
    delete_redis_cache_post(post.uuid)

    return jsonify(is_flagged=user_post_rating.is_flagged)
Ejemplo n.º 2
0
def rate(uuid, rating):
    post_id = decode_id(uuid)
    post = Post.query.get_or_404(post_id)
    # Check if post has been rated
    user_post_rating = UserPostRating.query.filter_by(post_id=post.id, user_id=current_user.id).first()
    # If a rating exists, we update the the user post record and the post
    # record accordingly
    if user_post_rating:
        if user_post_rating.is_positive != rating:
            user_post_rating.is_positive = rating
            if user_post_rating.is_positive:
                post.rating.positive += 1
                post.rating.negative -= 1
                post.user.karma.positive += 5
                post.user.karma.negative -= 5
            else:
                post.rating.negative += 1
                post.rating.positive -= 1
                post.user.karma.negative += 5
                post.user.karma.positive -= 5
            db.session.commit()
        else:
            # Remove existing vote
            if user_post_rating.is_positive:
                post.rating.positive -= 1
                post.user.karma.positive -= 5
            else:
                post.rating.negative -= 1
                post.user.karma.negative -= 5
            db.session.delete(user_post_rating)
            db.session.commit()
            post.update_hot()
            post.user.update_karma()
            delete_redis_cache_keys("post_list")
            delete_redis_cache_keys("post_list", post.category.url)
            delete_redis_cache_post(post.uuid)

            return jsonify(rating=None, rating_delta=post.rating_delta)
    else:
        # if the post has not bee rated, create rating
        user_post_rating = UserPostRating(user_id=current_user.id, post_id=post.id, is_positive=rating)
        if user_post_rating.is_positive:
            post.rating.positive += 1
            post.user.karma.positive += 5
        else:
            post.rating.negative += 1
            post.user.karma.negative += 5
        db.session.add(user_post_rating)
        db.session.commit()
    post.update_hot()
    post.user.update_karma()

    delete_redis_cache_keys("post_list")
    delete_redis_cache_keys("post_list", post.category.url)
    delete_redis_cache_post(post.uuid)

    return jsonify(rating=str(user_post_rating.is_positive), rating_delta=post.rating_delta)
Ejemplo n.º 3
0
def delete(uuid):
    post_id = decode_id(uuid)
    post = Post.query.get_or_404(post_id)
    if (post.user.id == current_user.id) or (current_user.has_role("admin")):
        post.status = "deleted"
        post.edit_date = datetime.datetime.now()
        db.session.commit()
        # Clear all the caches
        delete_redis_cache_keys("post_list")
        delete_redis_cache_keys("post_list", post.category.url)
        delete_redis_cache_post(post.uuid)
        return jsonify(status="deleted")
    else:
        return abort(403)
Ejemplo n.º 4
0
def delete(uuid):
    post_id = decode_id(uuid)
    post = Post.query.get_or_404(post_id)
    post.check_permissions()
    if (post.user.id == current_user.id) or (current_user.has_role('admin')):
        post.status = 'deleted'
        post.edit_date = datetime.datetime.now()
        db.session.commit()
        # Clear all the caches
        delete_redis_cache_keys('post_list')
        delete_redis_cache_keys('post_list', post.category.url)
        delete_redis_cache_post(post.uuid)
        return jsonify(status='deleted')
    else:
        return abort(403)
Ejemplo n.º 5
0
def view(category, uuid, slug=None):
    post_id = decode_id(uuid)
    post = Post.query.get_or_404(post_id)
    categories = Category.query.all()
    user_string_id = 'ANONYMOUS'
    if current_user.is_authenticated():
        user_string_id = current_user.string_id

    # Aggressive redirect if the URL does not have a slug
    if not slug:
        return redirect(url_for('posts.view',
            category=category,
            uuid=uuid,
            slug=post.slug))
    #post.comments.sort(key=lambda comment: comment.confidence, reverse=True)

    oembed = None
    # If the content is a link, we try to pass it through micawber to do
    # some nice embedding
    if post.post_type_id == 1:
        try:
            oembed = registry.request(post.content)
            # Keep in mind that oembed can be of different types:
            # - photo
            # - video
            # - link
            # - etc
        except (ProviderNotFoundException, ProviderException):
            # If the link is not an OEmbed provider, we move on
            pass
        else:
            # For any other error (e.g. video was unpublished) we also pass
            pass

    form = CommentForm()
    if current_user.is_anonymous():
        current_user.is_owner = False
    elif current_user.is_authenticated() and post.user.id == current_user.id:
        current_user.is_owner = True
    return render_template('posts/view.html',
        title='view',
        post=post,
        oembed=oembed,
        form=form,
        categories=categories,
        user_string_id=user_string_id,
        picture=post.thumbnail('m'))
Ejemplo n.º 6
0
def view(category, uuid, slug=None):
    post_id = decode_id(uuid)
    post = Post.query.get_or_404(post_id)
    post.check_permissions()
    categories = Category.query.all()
    user_string_id = 'ANONYMOUS'
    if current_user.is_authenticated():
        user_string_id = current_user.string_id

    # Aggressive redirect if the URL does not have a slug
    if not slug:
        return redirect(
            url_for('posts.view', category=category, uuid=uuid,
                    slug=post.slug))

    oembed = None
    # If the content is a link, we try to pass it through micawber to do
    # some nice embedding
    if post.post_type_id == 1:
        try:
            oembed = registry.request(post.content)
            # Keep in mind that oembed can be of different types:
            # - photo
            # - video
            # - link
            # - etc
        except (ProviderNotFoundException, ProviderException):
            # If the link is not an OEmbed provider, we move on
            pass
        else:
            # For any other error (e.g. video was unpublished) we also pass
            pass

    form = CommentForm()
    if current_user.is_anonymous():
        current_user.is_owner = False
    elif current_user.is_authenticated() and post.user.id == current_user.id:
        current_user.is_owner = True
    return render_template('posts/view.html',
                           title='view',
                           post=post,
                           oembed=oembed,
                           form=form,
                           categories=categories,
                           user_string_id=user_string_id,
                           picture=post.thumbnail('m'))
Ejemplo n.º 7
0
def flag(uuid):
    post_id = decode_id(uuid)
    post = Post.query.get_or_404(post_id)
    post.check_permissions()
    # Get post
    user_post_rating = UserPostRating.query\
        .filter_by(post_id=post.id, user_id=current_user.id).first()
    # Check if user flagged the post
    if user_post_rating:
        # If the post was previously flagged
        if user_post_rating.is_flagged:
            user_post_rating.is_flagged = 0
            post.user.karma.negative += 5
        else:
            user_post_rating.is_flagged = 1
            post.user.karma.negative -= 5
    else:
        user_post_rating = UserPostRating(user_id=current_user.id,
                                          post_id=post.id,
                                          is_flagged=True)
        post.user.karma.negative += 5
        db.session.add(user_post_rating)

    # Commit changes so far
    db.session.commit()

    # Check if the post has been flagged multiple times, currently
    # the value is hardcoded to 5
    flags = UserPostRating.query\
        .filter_by(post_id=post.id, is_flagged=True)\
        .all()
    if len(flags) > 5:
        post.status = 'flagged'
    post.user.update_karma()

    # Clear all the caches
    delete_redis_cache_keys('post_list')
    delete_redis_cache_keys('post_list', post.category.url)
    delete_redis_cache_post(post.uuid)

    return jsonify(is_flagged=user_post_rating.is_flagged)
Ejemplo n.º 8
0
def edit(uuid):
    post_id = decode_id(uuid)
    post = Post.query.get_or_404(post_id)
    if (post.user.id == current_user.id) or (current_user.has_role("admin")):
        post.title = request.form["title"]
        post.status = "published"
        post.edit_date = datetime.datetime.now()

        if post.post_type.url == "text":
            post.content = bleach_input(request.form["content"])

        db.session.commit()

        # Clear all the caches
        delete_redis_cache_keys("post_list")
        delete_redis_cache_keys("post_list", post.category.url)
        delete_redis_cache_post(post.uuid)

        return jsonify(status="published")
    else:
        return abort(403)
Ejemplo n.º 9
0
def edit(uuid):
    post_id = decode_id(uuid)
    post = Post.query.get_or_404(post_id)
    if (post.user.id == current_user.id) or (current_user.has_role('admin')):
        post.title = request.form['title']
        post.status = 'published'
        post.edit_date = datetime.datetime.now()

        if post.post_type.url == 'text':
            post.content = bleach_input(request.form['content'])

        db.session.commit()

        # Clear all the caches
        delete_redis_cache_keys('post_list')
        delete_redis_cache_keys('post_list', post.category.url)
        delete_redis_cache_post(post.uuid)

        return jsonify(status='published')
    else:
        return abort(403)
Ejemplo n.º 10
0
def edit(uuid):
    post_id = decode_id(uuid)
    post = Post.query.get_or_404(post_id)
    post.check_permissions()
    if (post.user.id == current_user.id) or (current_user.has_role('admin')):
        post.title = request.form['title']
        post.status = 'published'
        post.edit_date = datetime.datetime.now()

        if post.post_type.url == 'text':
            post.content = bleach_input(request.form['content'])

        db.session.commit()

        # Clear all the caches
        delete_redis_cache_keys('post_list')
        delete_redis_cache_keys('post_list', post.category.url)
        delete_redis_cache_post(post.uuid)

        return jsonify(status='published')
    else:
        return abort(403)
Ejemplo n.º 11
0
def rate(uuid, rating):
    post_id = decode_id(uuid)
    post = Post.query.get_or_404(post_id)
    post.check_permissions()
    # Check if post has been rated
    user_post_rating = UserPostRating.query\
        .filter_by(post_id=post.id, user_id=current_user.id).first()
    # If a rating exists, we update the the user post record and the post
    # record accordingly
    if user_post_rating:
        if user_post_rating.is_positive != rating:
            user_post_rating.is_positive = rating
            if user_post_rating.is_positive:
                post.rating.positive += 1
                post.rating.negative -= 1
                post.user.karma.positive += 5
                post.user.karma.negative -= 5
            else:
                post.rating.negative += 1
                post.rating.positive -= 1
                post.user.karma.negative += 5
                post.user.karma.positive -= 5
            db.session.commit()
        else:
            # Remove existing vote
            if user_post_rating.is_positive:
                post.rating.positive -= 1
                post.user.karma.positive -= 5
            else:
                post.rating.negative -= 1
                post.user.karma.negative -= 5
            db.session.delete(user_post_rating)
            db.session.commit()
            post.update_hot()
            post.user.update_karma()
            delete_redis_cache_keys('post_list')
            delete_redis_cache_keys('post_list', post.category.url)
            delete_redis_cache_post(post.uuid)

            return jsonify(rating=None, rating_delta=post.rating_delta)
    else:
        # if the post has not bee rated, create rating
        user_post_rating = UserPostRating(user_id=current_user.id,
                                          post_id=post.id,
                                          is_positive=rating)
        if user_post_rating.is_positive:
            post.rating.positive += 1
            post.user.karma.positive += 5
        else:
            post.rating.negative += 1
            post.user.karma.negative += 5
        db.session.add(user_post_rating)
        db.session.commit()
    post.update_hot()
    post.user.update_karma()

    delete_redis_cache_keys('post_list')
    delete_redis_cache_keys('post_list', post.category.url)
    delete_redis_cache_post(post.uuid)

    return jsonify(rating=str(user_post_rating.is_positive),
                   rating_delta=post.rating_delta)