Ejemplo n.º 1
0
def new():
    form = PostForm()
    session['url'] = url_for('blog.new')
    if current_user.is_authenticated:
        if request.method == 'POST':
            content = request.form['body'].replace(
                "<script>", "<strong class="
                'warning'
                ">"
                "<'Scripts tags on source Code are not allowed, "
                "please use the Insert Code Snipped instead'></strong>")
            add_new_post = Post(
                post_image_file=save_picture(form.post_image_file.data),
                title=request.form['title'],
                slug=slugify(request.form['title']),
                body=content,
                # body=string.replace("geeks", "Geeks"),
                tags=request.form['tags'],
                author=current_user)
            db.session.add(add_new_post)
            db.session.commit()
            # flash('Post was successfully added')
            return redirect(url_for('blog.all_posts'))
    else:
        return current_app.login_manager.unauthorized()
    rendered_html = render_template('blog/new.html',
                                    form=form,
                                    image_file=current_user_image_file(),
                                    input_search_form=search_form())
    return html_minify(rendered_html)
Ejemplo n.º 2
0
def all_posts():
    session['url'] = url_for('blog.all_posts')
    page = request.args.get('page', 1, type=int)
    posts = Post.query.order_by(Post.date_posted.desc()).paginate(
        page=page, per_page=posts_per_page)

    if current_user.is_authenticated:
        rendered_html = render_template('blog/all.html',
                                        posts=posts,
                                        image_file=current_user_image_file(),
                                        input_search_form=search_form())
        return html_minify(rendered_html)
    rendered_html = render_template('blog/all.html',
                                    posts=posts,
                                    input_search_form=search_form())
    return html_minify(rendered_html)
Ejemplo n.º 3
0
def profile(user):
    session['url'] = '/account/profile/' + user
    # print(session['url'])
    user = User.query.filter_by(username=user).first_or_404()
    # Public image path
    posts = Post.query.filter_by(author=user).order_by(Post.date_posted.desc())
    if current_user.is_authenticated:
        return render_template('account/profile.html',
                               title='Account',
                               user=user,
                               image_file=current_user_image_file(),
                               input_search_form=search_form(),
                               posts=posts)
    return render_template('account/profile.html',
                           title='Account',
                           user=user,
                           posts=posts,
                           input_search_form=search_form())
Ejemplo n.º 4
0
def update_account():
    form = UpdateAccountForm()
    if form.validate_on_submit():
        if form.image_file.data:
            image_file = save_picture(form.image_file.data)
            current_user.image_file = image_file
        current_user.username = form.username.data
        current_user.email = form.email.data
        current_user.git_username = form.git_username.data
        db.session.commit()
        flash('Your account has been updated!', 'success')
        return redirect(url_for('account.update_account'))
    elif request.method == 'GET':
        form.username.data = current_user.username
        form.email.data = current_user.email
        form.git_username.data = current_user.git_username
        # Private image path
    return render_template('account/account.html',
                           title='Account',
                           image_file=current_user_image_file(),
                           input_search_form=search_form(),
                           form=form)
Ejemplo n.º 5
0
def update_post(id):
    post = Post.query.get(id)
    form = PostForm()
    session['url'] = url_for('blog.update_post', id=id)
    if current_user.is_authenticated:
        if post.author == current_user:
            if form.validate_on_submit():
                if form.post_image_file.data:
                    post_image_file = save_picture(form.post_image_file.data)
                    post.post_image_file = post_image_file

                post.title = form.title.data
                post.slug = slugify(form.title.data)
                post.body = form.body.data.replace(
                    "<script>", "<strong class="
                    'warning'
                    "> <'Scripts tags on source "
                    "Code are not allowed, "
                    "please use the Insert Code Snipped instead'></strong>")
                post.tags = form.tags.data
                db.session.commit()
                return redirect(url_for('blog.post', slug=post.slug))
            elif request.method == 'GET':
                post_image_file = post.post_image_file
                form.title.data = post.title
                form.body.data = post.body
                form.tags.data = post.tags

    else:
        return current_app.login_manager.unauthorized()
    rendered_html = render_template('blog/update.html',
                                    title='Update Post',
                                    form=form,
                                    image_file=current_user_image_file(),
                                    post_image_file=post_image_file,
                                    input_search_form=search_form())
    return html_minify(rendered_html)
Ejemplo n.º 6
0
def post(slug):
    title_post = Post.query.filter_by(slug=slug).first_or_404()
    x = arrow.get(title_post.date_posted)
    data = {
        'title': title_post.title,
        'post_title': title_post.title,
        'date_posted': x.humanize(),
        'body': title_post.body,
        'author': title_post.author,
        'id': title_post.id,
        'post_image_file': title_post.post_image_file
    }
    if current_user.is_authenticated:
        rendered_html = render_template(
            'blog/post.html',
            **data,
            image_file=current_user_image_file(),  # Pass image for profile
            input_search_form=search_form())
        return html_minify(rendered_html)
    else:
        rendered_html = render_template('blog/post.html',
                                        **data,
                                        input_search_form=search_form())
        return html_minify(rendered_html)