示例#1
0
文件: __init__.py 项目: Fweeb/dillo
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)
示例#2
0
文件: __init__.py 项目: Fweeb/dillo
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)
示例#3
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)
示例#4
0
文件: __init__.py 项目: Fweeb/dillo
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)
示例#5
0
文件: __init__.py 项目: Fweeb/dillo
    def settings(self):
        form_settings = FormSettings()
        if form_settings.validate_on_submit():
            logo_alt = Setting.query.filter_by(name='logo_alt').first()
            logo_alt.value = form_settings.logo_alt.data
            if form_settings.logo_image.data:
                # If the user uploads an image from the form
                filename = secure_filename(form_settings.logo_image.data.filename)
                filepath = os.path.join(app.config['STATIC_IMAGES'], filename)
                form_settings.logo_image.data.save(filepath)
                logo_image = Setting.query.filter_by(name='logo_image').first()
                logo_image.value = filename
            if form_settings.favicon.data:
                # If the user uploads an image from the form
                filename = secure_filename(form_settings.favicon.data.filename)
                filepath = os.path.join(app.config['STATIC_IMAGES'], filename)
                form_settings.favicon.data.save(filepath)
                favicon = Setting.query.filter_by(name='favicon').first()
                favicon.value = filename
            title = Setting.query.filter_by(name='title').first()
            title.value = form_settings.title.data
            tagline = Setting.query.filter_by(name='tagline').first()
            tagline.value = form_settings.tagline.data
            title_html = Setting.query.filter_by(name='title_html').first()
            title_html.value = form_settings.title_html.data
            footer = Setting.query.filter_by(name='footer').first()
            footer.value = form_settings.footer.data
            credits = Setting.query.filter_by(name='credits').first()
            credits.value = form_settings.credits.data
            theme = Setting.query.filter_by(name='theme').first()
            theme.value = form_settings.theme.data
            keywords = Setting.query.filter_by(name='keywords').first()
            keywords.value = form_settings.keywords.data
            twitter_username = Setting.query.filter_by(name='twitter_username').first()
            twitter_username = form_settings.twitter_username.data
            db.session.commit()
            # Reload the settings
            load_settings()
            # Clear cache for homepage
            if redis_client:
                delete_redis_cache_keys('post_list')

        return redirect(url_for('admin.index'))
示例#6
0
    def settings(self):
        form_settings = FormSettings()
        if form_settings.validate_on_submit():
            logo_alt = Setting.query.filter_by(name='logo_alt').first()
            logo_alt.value = form_settings.logo_alt.data
            if form_settings.logo_image.data:
                # If the user uploads an image from the form
                filename = secure_filename(form_settings.logo_image.data.filename)
                filepath = os.path.join(app.config['STATIC_IMAGES'], filename)
                form_settings.logo_image.data.save(filepath)
                logo_image = Setting.query.filter_by(name='logo_image').first()
                logo_image.value = filename
            if form_settings.favicon.data:
                # If the user uploads an image from the form
                filename = secure_filename(form_settings.favicon.data.filename)
                filepath = os.path.join(app.config['STATIC_IMAGES'], filename)
                form_settings.favicon.data.save(filepath)
                favicon = Setting.query.filter_by(name='favicon').first()
                favicon.value = filename
            title = Setting.query.filter_by(name='title').first()
            title.value = form_settings.title.data
            tagline = Setting.query.filter_by(name='tagline').first()
            tagline.value = form_settings.tagline.data
            title_html = Setting.query.filter_by(name='title_html').first()
            title_html.value = form_settings.title_html.data
            footer = Setting.query.filter_by(name='footer').first()
            footer.value = form_settings.footer.data
            credits = Setting.query.filter_by(name='credits').first()
            credits.value = form_settings.credits.data
            theme = Setting.query.filter_by(name='theme').first()
            theme.value = form_settings.theme.data
            keywords = Setting.query.filter_by(name='keywords').first()
            keywords.value = form_settings.keywords.data
            twitter_username = Setting.query.filter_by(name='twitter_username').first()
            twitter_username.value = form_settings.twitter_username.data
            db.session.commit()
            # Reload the settings
            load_settings()
            # Clear cache for homepage
            if redis_client:
                delete_redis_cache_keys('post_list')

        return redirect(url_for('admin.index'))
示例#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)
示例#8
0
文件: __init__.py 项目: Fweeb/dillo
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)
示例#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)
示例#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)
示例#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)
示例#12
0
def submit():
    form = get_post_form()
    if request.method == 'GET':
        return render_template('posts/form.html', submit_post_form=form)
    else:
        if form.validate_on_submit():
            content = form.content.data
            # If the post is a link (is 1), we cast this because it's coming from
            # a hidden field
            post_type_id = int(form.post_type_id.data)
            if post_type_id == 1:
                content = form.url.data
                if not check_url(content):
                    return abort(404)
            else:
                # Clean the content
                content = bleach_input(content)
            if not content:
                return abort(400)

            post = Post(user_id=current_user.id,
                        category_id=form.category_id.data,
                        post_type_id=post_type_id,
                        title=form.title.data,
                        slug=slugify(form.title.data),
                        content=content)
            db.session.add(post)
            db.session.commit()
            post.uuid = encode_id(post.id)
            db.session.commit()
            post_rating = PostRating(post_id=post.id, positive=0, negative=0)
            db.session.add(post_rating)
            post.update_hot()
            if form.picture.data or form.picture_remote.data:
                if form.picture.data:
                    # If the user uploads an image from the form
                    _, filepath = tempfile.mkstemp()
                    form.picture.data.save(filepath)
                else:
                    # If the url is retrieved via embedly
                    _, filepath = tempfile.mkstemp()
                    with open(filepath, 'wb') as handle:
                        response = requests.get(form.picture_remote.data,
                                                stream=True)
                        for block in response.iter_content(1024):
                            if not block:
                                break
                            handle.write(block)

                # In both cases we get the image now saved in temp and upload it
                # to Imgur
                if imgur_client:
                    image = imgur_client.upload_from_path(filepath,
                                                          config=None,
                                                          anon=True)
                else:
                    image = dict(link=None, deletehash=None)

                if app.config['USE_UPLOADS_LOCAL_STORAGE']:
                    # Use the post UUID as a name for the local image.
                    # If Imgur is not available save it in the database with a _ to
                    # imply that the file is available only locally.
                    if not image['link']:
                        image['link'] = '_' + post.uuid
                    image_name = post.uuid + '.jpg'
                    # The root of the local storage path
                    local_storage_path = app.config[
                        'UPLOADS_LOCAL_STORAGE_PATH']
                    # Get the first 2 chars of the image name to make a subfolder
                    storage_folder = os.path.join(local_storage_path,
                                                  image_name[:2])
                    # Check if the subfolder exists
                    if not os.path.exists(storage_folder):
                        # Make it if it does not
                        os.mkdir(storage_folder)
                    # Build the full path to store the image
                    storage_filepath = os.path.join(storage_folder, image_name)
                    # Copy from temp to the storage path
                    im = Image.open(filepath)
                    im.save(storage_filepath, "JPEG")
                    # Make all the thumbnails
                    generate_local_thumbnails(storage_filepath)
                post.picture = image['link']
                post.picture_deletehash = image['deletehash']
                os.remove(filepath)
            db.session.commit()

            # Subscribe owner to updates for this post (mainly comments)
            notification_subscribe(current_user.id, 1, post.id)

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

            return jsonify(post_url=url_for('posts.view',
                                            category=post.category.url,
                                            uuid=post.uuid,
                                            slug=post.slug))
        else:
            return abort(400, '{"message" : "form validation error"}')
示例#13
0
文件: __init__.py 项目: Fweeb/dillo
def submit():
    form = PostForm()
    form.category_id.choices = [(c.id, c.name) for c in Category.query.all()]
    if form.validate_on_submit():
        content = form.content.data
        # If the post is a link (is 1), we cast this because it's coming from
        # a hidden field
        post_type_id = int(form.post_type_id.data)
        if post_type_id == 1:
            content = form.url.data
            if not check_url(content):
                return abort(404)
        else:
            # Clean the content
            content = bleach_input(content)
        if not content:
            return abort(400)

        post = Post(
            user_id=current_user.id,
            category_id=form.category_id.data,
            post_type_id=post_type_id,
            title=form.title.data,
            slug=slugify(form.title.data),
            content=content,
        )
        db.session.add(post)
        db.session.commit()
        post.uuid = encode_id(post.id)
        db.session.commit()
        post_rating = PostRating(post_id=post.id, positive=0, negative=0)
        db.session.add(post_rating)
        post.update_hot()
        if form.picture.data or form.picture_remote.data:
            if form.picture.data:
                # If the user uploads an image from the form
                filename = secure_filename(form.picture.data.filename)
                filepath = "/tmp/" + filename
                form.picture.data.save(filepath)
            else:
                # If the url is retrieved via embedly
                filename = secure_filename(form.picture_remote.data)
                filepath = "/tmp/" + filename
                with open(filepath, "wb") as handle:
                    response = requests.get(form.picture_remote.data, stream=True)
                    for block in response.iter_content(1024):
                        if not block:
                            break
                        handle.write(block)

            # In both cases we get the image now saved in temp and upload it
            # to Imgur
            if imgur_client:
                image = imgur_client.upload_from_path(filepath, config=None, anon=True)
            else:
                image = dict(link=None, deletehash=None)

            if app.config["USE_UPLOADS_LOCAL_STORAGE"]:
                # Use the post UUID as a name for the local image.
                # If Imgur is not available save it in the database with a _ to
                # imply that the file is available only locally.
                if not image["link"]:
                    image["link"] = "_" + post.uuid
                image_name = post.uuid + ".jpg"
                # The root of the local storage path
                local_storage_path = app.config["UPLOADS_LOCAL_STORAGE_PATH"]
                # Get the first 2 chars of the image name to make a subfolder
                storage_folder = os.path.join(local_storage_path, image_name[:2])
                # Check if the subfolder exists
                if not os.path.exists(storage_folder):
                    # Make it if it does not
                    os.mkdir(storage_folder)
                # Build the full path to store the image
                storage_filepath = os.path.join(storage_folder, image_name)
                # Copy from temp to the storage path
                im = Image.open(filepath)
                im.save(storage_filepath, "JPEG")
                # Make all the thumbnails
                generate_local_thumbnails(storage_filepath)
            post.picture = image["link"]
            post.picture_deletehash = image["deletehash"]
            os.remove(filepath)
        db.session.commit()

        # Subscribe owner to updates for this post (mainly comments)
        notification_subscribe(current_user.id, 1, post.id)

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

        return jsonify(post_url=url_for("posts.view", category=post.category.url, uuid=post.uuid, slug=post.slug))
    else:
        return abort(400, '{"message" : "form validation error"}')