Exemple #1
0
def edit_news(id):
    form = NewsForm()
    if request.method == "GET":
        db_sess = db_session.create_session()
        news = db_sess.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():
        db_sess = db_session.create_session()
        news = db_sess.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
            db_sess.commit()
            return redirect('/')
        else:
            abort(404)
    return render_template('news.html',
                           title='Редактирование новости',
                           form=form)
 def post(self, request):
     news_form = NewsForm(request.user, request.POST, request.FILES)
     if news_form.is_valid():
         news = news_form.save()
         return Response(NewsReadSerializer(news).data)
     else:
         return HttpResponseBadRequest(news_form.errors.as_json(), content_type='application/json')
Exemple #3
0
 def post(self, request):
     news_form = NewsForm(request.user, request.POST, request.FILES)
     if news_form.is_valid():
         news = news_form.save()
         return Response(NewsReadSerializer(news).data)
     else:
         return HttpResponseBadRequest(news_form.errors.as_json(),
                                       content_type='application/json')
Exemple #4
0
def add_news():
    form = NewsForm()
    if form.validate_on_submit():
        db_sess = 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)
        db_sess.merge(current_user)
        db_sess.commit()
        return redirect('/')
    return render_template('news.html', title='Добавление новости', form=form)