示例#1
0
def comm_add(new_id):
    """
    Обработка добавления комментария
    :param new_id: id новости в DB, к которой добавляется комментарий
    :return: форма для добавления комментария
    """
    form = ComForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()
        to_new = db_sess.query(News).filter(News.id == new_id).first()
        idd = len(db_sess.query(Comments).all()) + 1
        if len(to_new.comms) > 0:
            to_new.comms = str(to_new.comms) + ';{}'.format(idd)
        else:
            to_new.comms = idd
        com = Comments()
        com.user_id = current_user.id
        com.content = form.content.data
        com.created_date = datetime.datetime.now()
        db_sess.add(com)
        db_sess.add(to_new)
        db_sess.commit()
        return redirect('/')
    return render_template("add_com.html",
                           form=form,
                           title='Добавить комментарий')
示例#2
0
def comment(id):
    form = CommentForm()
    session = db_session.create_session()
    news = session.query(News).filter(News.id == id).first()
    if form.validate_on_submit():
        comments = Comments()
        comments.content = form.content.data
        f = form.photo.data
        if f:
            filename = secure_filename(f.filename)
            f.save(os.path.join(app.static_folder, 'img', filename))
            comments.photo = f.filename
        comments.news_id = id
        comments.user_id = current_user.name
        session.add(comments)
        session.commit()
        return redirect('/')
    return render_template('comments.html', form=form, news=news)