def one_blog_page(id): form = CommentsForm() db_sess = db_session.create_session() if form.validate_on_submit(): comment = Comments() comment.text = form.content.data comment.news_id = int(id) comment.name_id = current_user.id db_sess.add(comment) db_sess.commit() return redirect(f'/blogs/{id}') news = db_sess.query(News).filter(News.id == id) comms = db_sess.query(Comments).filter(Comments.news_id == id) comments = [] for c in comms: user = db_sess.query(User).filter(User.id == c.name_id).first() comments.append((user.name, c.text)) cmnts = False if comms is None else True return render_template('one_blog_page.html', news=news[0], current_user=current_user, comms=comms, cmnts=cmnts, form=form, comments=comments)
def add_comment(id): form = CommentForm() if form.validate_on_submit(): session = db_session.create_session() comment = Comments() comment.text = form.text.data comment.news_id = id current_user.comments.append(comment) session.merge(current_user) session.commit() return redirect('/') return render_template('comments.html', title='Добавление комментария', form=form)
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)
def index(): session = db_session.create_session() # Отображение новостей if current_user.is_authenticated: news = session.query(News).filter((News.user == current_user) | (News.is_private != True)) else: news = session.query(News).filter(News.is_private != True) # Отображение комментариев comments = session.query(Comments).filter() # Форма для добавления комментария form = CommentForm() if form.validate_on_submit(): comment = Comments() comment.text = form.text.data comment.news_id = int(''.join(form.news_id.raw_data)) current_user.comment.append(comment) session.merge(current_user) session.commit() return redirect('/') # Форма для удаления комментария delete_form = CommentDelete() if delete_form.validate_on_submit() and current_user.is_authenticated: comm = session.query(Comments).filter(Comments.id == int(''.join( delete_form.comment_id.raw_data))).first() if comm: session.delete(comm) session.commit() else: abort(404) return redirect('/') return render_template("index.html", news=news, comments=comments, form=form, form_del=delete_form, title="Форум Питонистов")