def edit_news(id): form = NewsForm() if request.method == "GET": session = db_session.create_session() news = session.query(News).filter( News.id == id, News.user == current_user).first() if news: form.title.data = news.title form.content.data = news.content form.is_private.data = news.is_private else: abort(404) if form.validate_on_submit(): session = db_session.create_session() news = session.query(News).filter( News.id == id, News.user == current_user).first() if news: news.title = form.title.data news.content = form.content.data news.is_private = form.is_private.data session.commit() return redirect('/') else: abort(404) return render_template('news.html', title='Редактирование новости', form=form)
def add_news(): form = NewsForm() if form.validate_on_submit(): session = db_session.create_session() news = News() news.title = form.title.data news.content = form.content.data news.is_private = form.is_private.data current_user.news.append(news) session.merge(current_user) session.commit() return redirect('/') return render_template('news.html', title='Добавление новости', form=form)