Esempio n. 1
0
def comment_submit(id, cid = None):
    form = CommentForm(request.form)

    user = db.session.query(User).get(session['user'])
    image = db.session.query(Image).filter_by(id=id).one()

    if request.method == 'POST' and form.validate():
        comment = Comment(session['user'], image, form.text.data, cid)

        if user.already_commented(image, cid): 
            flash(local.comment['ALREADY_COMMENTED'], 'error')
        else:
            db.session.add(comment)
            db.session.flush()
            comment.vote(1, session['user'])
            db.session.commit()
            flash(local.comment['POSTED'], 'success')

        # Return user to image or comment controllers depending on ref
        if cid is None:
            return redirect(url_for('image', id=image.id))
        else:
            return redirect(url_for('comment', id=image.id, cid=cid))

    flash_errors(form)
    comments = construct_comment_tree(image.comments)
    if cid is None:
        try:
            commented = Comment.query.filter_by(id_user=session['user'], image=image, id_father=None).one()
        except:
            commented = False
        return render('image.html', title=image.name, commented=commented, image=image, form=form, comments=comments)
    else:
        return render('comment.html', title=local.comment['TITLE_ONE'], id=id, cid=cid, comments=comments, form=form)