def post_create():
    form = PostForm()
    if form.validate_on_submit():
        slug = title_slugfier(form.title.data)
        new_post = Post(
            title=form.title.data, 
            body=form.body.data, 
            slug=slug,
            description=form.description.data,
            author=current_user)

        if  form.image.data:
            try : # try in caso di image corrotta 
                image = save_picture(form.image.data) # return path
                new_post.image = image
            except Exception:
                db.session.add(new_post)
                db.session.commit()
                flash("Si e' verifcato un problema con l'immagina caricata !!!")
                return redirect(url_for('post_update', post_id=new_post.id))

        db.session.add(new_post)
        db.session.commit()
        # return redirect(url_for("post_detail", post_id=new_post.id))
        return redirect(url_for("post_detail", post_slug=slug))
    # creare un nuovo post con un nuovo render html
    return render_template("post_editor.html", form=form)
def post_update(post_id):
    post_instance = Post.query.get_or_404(post_id)
    #   verificare che l'user che ha creato il post e' lo stesso che sta 
    #   facendo la richiesta di aggiornamento, in caso negativo paginda di ERROR
    if post_instance.author != current_user:
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post_instance.title = form.title.data
        post_instance.description = form.description.data
        post_instance.body = form.body.data

        if  form.image.data:
            try : # try in caso di image corrotta 
                print(form.image.data)
                image = save_picture(form.image.data) # return path
                post_instance.image = image
            except Exception:
                db.session.commit()
                flash("Si e' verifcato un problema con l'immagina caricata !!!")
                return redirect(url_for('post_update', post_id=post_instance.id))

        db.session.commit()
        # return redirect(url_for('post_detail', post_id=post_instance.id))
        return redirect(url_for('post_detail', post_slug=post_instance.slug))
    elif request.method == "GET" : 
        #   caso la pagina venga richiesta 
        #   e popolare il form
        form.title.data = post_instance.title
        form.description.data = post_instance.description
        form.body.data = post_instance.body
    # this row to show also image into editor preview
    post_image = post_instance.image or None
    return render_template("post_editor.html", form=form, post_image=post_image)
Exemplo n.º 3
0
def post_update(post_id):
    post_instance = Post.query.get_or_404(post_id)
    if post_instance.author != current_user:
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post_instance.title = form.title.data
        post_instance.description = form.description.data
        post_instance.body = form.body.data

        # inserisco il nome dell'immagine del db e controllo se l'immagine non è compromessa
        if form.image.data:
            try:
                image = save_picture(form.image.data)
                post_instance.image = image
            except Exception:
                db.session.commit()
                flash("Errore durante l'upload. Cambia immagine e riprova.")
                return redirect(
                    url_for('post_update', post_id=post_instance.id))

        db.session.commit()
        return redirect(url_for('post_detail', post_slug=post_instance.slug))
    elif request.method == "GET":
        form.title.data = post_instance.title
        form.description.data = post_instance.description
        form.body.data = post_instance.body

    post_image = post_instance.image or None

    return render_template("post_editor.html",
                           form=form,
                           post_image=post_image)
Exemplo n.º 4
0
def post_update(post_id):
    post_instance = Post.query.get_or_404(post_id)
    if post_instance.author != current_user:
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post_instance.title = form.title.data
        post_instance.description = form.description.data
        post_instance.body = form.body.data

        if form.image.data:
            try:
                image= save_picture(form.image.data)
                post_instance.image = image
            except Exception:
                db.session.commit()
                flash("C'è stao un problema con l'upload dell'immagine. Cambia immagine e riprova")
                return redirect(url_for('post_update', post_id=post_instance.id))

        db.session.commit()
        return redirect(url_for('post_detail', post_slug=post_instance.slug))
    elif request.method == "GET":
        form.title.data = post_instance.title
        form.description.data = post_instance.description
        form.body.data = post_instance.body
    return render_template("post_editor.html", form=form)
Exemplo n.º 5
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data

        if form.image.data:
            img_file = save_picture(form.image.data, folder='images')
            post.image_file = img_file

        db.session.commit()
        flash('Post update', 'success')
        return redirect(url_for('post_detail', post_id=post_id))

    elif request.method == 'GET':
        # implicity GET
        form.title.data = post.title
        form.content.data = post.content
    return render_template('new_post.html',
                           title='Update Post',
                           form=form,
                           legend='Update Post')
Exemplo n.º 6
0
def post_create():
    form = PostForm()
    if form.validate_on_submit():
        slug = title_slugifier(form.title.data)
        new_post = Post(title=form.title.data,
                        body=form.body.data,
                        slug=slug,
                        description=form.description.data,
                        author=current_user)

        # inserisco il nome dell'immagine del db e controllo se l'immagine non è compromessa
        if form.image.data:
            try:
                image = save_picture(form.image.data)
                new_post.image = image
            except Exception:
                db.session.add(new_post)
                db.session.commit()
                flash("Errore durante l'upload. Cambia immagine e riprova.")
                return redirect(url_for('post_update', post_id=new_post.id))

        db.session.add(new_post)
        db.session.commit()
        return redirect(url_for('post_detail', post_slug=slug))
    return render_template("post_editor.html", form=form)
Exemplo n.º 7
0
def post_create():
    form = PostForm()
    if form.validate_on_submit():
        slug = title_slugifier(form.title.data)
        new_post = Post(title=form.title.data,
                        body=form.body.data,
                        slug=slug,
                        description=form.description.data,
                        author=current_user)

        if form.image.data:
            try:
                image = save_picture(form.image.data)
                new_post.image = image
            except Exception:
                db.session.add(new_post)
                db.session.commit()
                flash(
                    "C'è stato un problema con l'upload dell'immagine. Cambia immagine e riprova."
                )
                return redirect(url_for('post_update', post_id=new_post.id))

        db.session.add(new_post)
        db.session.commit()
        return redirect(url_for('post_detail', post_slug=slug))
    return render_template("post_editor.html", form=form)
Exemplo n.º 8
0
def account():
    form = UpdateAccountForm()
    if form.validate_on_submit():
        if form.picture.data:
            current_user.image_file = save_picture(form.picture.data)
        current_user.username = form.username.data
        current_user.email = form.email.data
        db.session.commit()
        flash(f'Account updated successfully', 'success')
        redirect(url_for('.account'))
    elif request.method == 'GET':
        form.username.data = current_user.username
        form.email.data = current_user.email
    img_file = url_for('static', filename='profile_img/' + current_user.image_file)
    return render_template('user/account.html', title='Account', img_file=img_file, form=form)
Exemplo n.º 9
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    content=form.content.data,
                    author=current_user)
        if form.image.data:
            img_file = save_picture(form.image.data, folder='images')
            post.image_file = img_file
        db.session.add(post)
        db.session.commit()
        flash('Post created', 'success')
        return redirect(url_for('home'))
    return render_template('new_post.html',
                           title='New Post',
                           form=form,
                           legend='New Post')
Exemplo n.º 10
0
def account():
    form = AccountForm()
    if form.validate_on_submit():
        if form.picture.data:
            img_file = save_picture(form.picture.data)
            current_user.image_file = img_file
        current_user.email = form.email.data
        current_user.username = form.username.data
        db.session.commit()
        flash('Account Update', 'success')
        return redirect(url_for('account'))

    image_file = url_for('static',
                         filename='profile_img/{}'.format(
                             current_user.image_file))
    return render_template('account.html',
                           title='Account',
                           image_file=image_file,
                           form=form)
Exemplo n.º 11
0
def register():
    if current_user.is_authenticated:
        redirect(url_for('home'))
    form = RegistrationForm()
    if form.validate_on_submit():

        pass_encrypt = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        username = form.username.data

        user = User(
            username=username,
            email=form.email.data,
            password=pass_encrypt,
        )
        if form.picture.data:
            img_file = save_picture(form.picture.data)
            user.image_file = img_file
        db.session.add(user)
        db.session.commit()
        flash(f'Your account has been created!', 'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)