def edit_post(post_id): post = Post.query.get_or_404(post_id) # Below line of code ensures that only the author of the post can edit it if post.author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): form.title.data = post.title form.post_content.data = post.post_content db.session.commit() flash('Your Post Has Been Updated', 'success') return redirect(url_for('post', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.post_content.data = post.post_content return render_template('create_post.html', title='Edit-Post', form=form, legend='Edit Post')
def user_panel_appeal(): if request.method == 'POST': theme = request.form.get('theme') text_appeal = request.form.get('text-appeal') try: new_appeal = appeal(theme=theme, description=text_appeal, user_id=current_user.id) db.session.add(new_appeal) db.session.commit() except Exception: print('Ошибка') return redirect(url_for('user_panel_history_appeal')) else: form = PostForm() return render_template('UserPanel/appeal.html', form=form)
def add_form(): from forms import PostForm from models import Post if request.method == 'POST': form = PostForm(request.form) if form.validate(): post = Post(**form.data) db.session.add(post) db.session.commit() return home() else: return str(form.errors), 400 elif request.method == 'GET': return render_template('add_post.html') else: return 'bad method', 400
def post_form(post_id=None): data = {} form = PostForm() if request.method == "POST" and form.validate_on_submit(): title = form.title.data slug = form.slug.data content = form.content.data data['title'] = title data['slug'] = slug data['content'] = content POSTS[slug] = {'title': title, 'slug': slug, 'content': content} to_json(POSTS) return redirect(url_for('index')) return render_template('admin/post_form.html', form=form, **data)
def post_update(request, slug=None): if not request.user.is_staff or not request.user.is_superuser: raise Http404 instance = get_object_or_404(Post, slug=slug) form = PostForm(request.POST or None, request.FILES or None, instance=instance) if form.is_valid(): instance = form.save(commit=False) instance.save() messages.success(request, "<a href='#'>Item Saved</a>", extra_tags='html_safe') return HttpResponseRedirect(instance.get_absolute_url()) return render(request, 'post_form.html', {'form': form})
def index(page=1): form = PostForm() if form.validate_on_submit(): language = guessLanguage(form.post.data) if language == 'UNKNOWN' or len(language) > 5: language = '' post = Post(body=form.post.data, timestamp=datetime.utcnow(), author=g.user, language=language) db.session.add(post) db.session.commit() flash(gettext('Your post is now live!')) return redirect(url_for('index')) posts = g.user.followed_posts().paginate(page, POSTS_PER_PAGE, False) return render_template('index.html', title='Home', form=form, posts=posts)
def search(): """ Search bar route """ search = request.args.get('search') symbols = search_symbol(search) post_form = PostForm() signup_form = RegisterUserForm() login_form = LoginForm() return render_template('users/index.html', login_form=login_form, post_form=post_form, symbols=symbols, signup_form=signup_form)
def create_news(): if request.method == 'POST': title = request.form['title'] description = request.form['description'] try: news_post = news(title=title, description=description) db.session.add(news_post) db.session.commit() except Exception: print('Error') return redirect(url_for('news_func')) else: form = PostForm() return render_template('create_news.html', form=form)
def home(): form = PostForm() posts = Post.query.order_by(desc(Post.posted_at)).limit(100).all() if request.method == 'GET': return render_template('home.html', form=form, posts=posts) if request.method == 'POST': if form.validate_on_submit() == False: return render_template('home.html', form=form, posts=posts) newPost = Post(content=form.post.data, uid=current_user.uid) db.session.add(newPost) db.session.commit() flash("Message posted.") return redirect(url_for('home'))
def create_post(): if request.method =='POST' : title = request.form['title'] body = request.form['body'] try: post = Post(title=title, body=body) db.session.add(post) db.session.commit() except: print('Something wrong') return redirect('/') form = PostForm() return render_template('create_post.html' , form=form)
def newpost(): pid = request.args.get('pid') form = PostForm(pid=pid) if request.method == 'GET': return render_template('newpost.html', form=form) else: if not Problem.query.filter_by( pid=form.pid.data).all() or not form.validate_title(): return 'error' comment = Comment(pid = form.pid.data, userid = current_user.userid, \ nickname = current_user.nickname, title = form.title.data, \ content = form.content.data, post_time = get_now_time()) db.session.add(comment) db.session.commit() return redirect(url_for('discuss', pid=form.pid.data))
def post(): from models import Post, User from forms import PostForm if request.method == 'POST': print(request.form) form = PostForm(request.form) if form.validate(): post = Post(**form.data) db.session.add(post) db.session.commit() flash('Post created!') else: flash('Form is not valid! Post was not created.') flash(str(form.errors))
def update(id: int): post = get_object_by_id_or_404(Post, id=id) checked = "checked" if post.is_public else "" form = PostForm() if request.method == 'POST': if form.validate_on_submit: with database.atomic(): post.title = form.title.data post.content = form.content.data post.is_public = form.is_public.data post.pub_time = datetime.datetime.now() post.save() return redirect(url_for('detail', id=post.id)) return render_template('update.html', post=post, form=form, checked=checked)
def comment(base_id): user = g.user posts = "" form = PostForm() if form.validate_on_submit(): category = Base_Post.query.filter_by(id=base_id).first().category comment = User_Post(body=form.post.data, description=form.post.data, timestamp=datetime.utcnow(), category=category, comment=base_id, extend=0, discriminator='user_post', author=g.user) db.session.add(comment) db.session.commit() flash('you commented') return redirect(url_for('story', base_id=base_id)) return render_template("post_edit.html", form=form)
def EditPost(post_id): form=PostForm() post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) # unforbidden route if form.validate_on_submit(): post.title = form.title.data post.body = form.content.data db.session.commit() flash("Your post has been updated.", "success") return redirect(url_for("EditPost", post_id=post_id)) elif request.method =="GET": form.title.data = post.title form.content.data = post.body return render_template("edit_post.html", title="Update Post", form=form, post=post)
def post(self): form = PostForm() if form.validate_on_submit(): try: if not form.validate(): raise Exception(_('ERROR_INVALID_SUBMISSION')) remain = request.values.get('remain', False, bool) post = Post.create() form.populate_obj(post) post.user = current_user f = request.files.get('file') if f: picture = Picture.create() picture.save_file(f, current_user) post.cover_picture_id = picture.id if picture else 0 # init the score post.update_score(page_view=1) post.editor_version = 1 post.save() Feed.clear_feed_cache() if post.is_draft: message = _('POST_DRAFT_SAVE_SUCESS') else: message = _('POST_PUBLIC_SAVE_SUCESS') if remain: url = url_for('PostsView:put', id=post.id, remain='y') else: url = url_for('PostsView:get', id=post.id) return render_view(url, redirect=True, message=message) except Exception as e: flash(e.message, 'error') return render_view('admin/posts/edit.html', form=form)
def edit_post(id): """ Обработчик страницы редактирования поста """ form = PostForm() if request.method == "GET": session = db_session.create_session() if current_user.role == 'admin': post = session.query(Post).filter(Post.id == id).first() else: post = session.query(Post).filter( Post.id == id, Post.user == current_user).first() if post: form.title.data = post.title form.content.data = post.content else: abort(404) if form.validate_on_submit(): session = db_session.create_session() if current_user.role == 'admin': post = session.query(Post).filter(Post.id == id).first() else: post = session.query(Post).filter( Post.id == id, Post.user == current_user).first() if post: post.title = form.title.data post.content = form.content.data session.commit() if bool(form.attachment.data): session = db_session.create_session() post = session.query(Post).order_by(Post.id.desc()).first() f = form.attachment.data type = f.filename.split('.')[-1] filename = f'post_attachment_{post.id}.{type}' path = os.path.join('static', 'attachments', filename) f.save(path) post.attachment = path session.commit() return redirect('/blog') else: abort(404) return render_template('post.html', title='Редактирование поста', form=form, css=url_for('static', filename='css/post_style.css'))
def post(): form = PostForm() if form.validate_on_submit(): # get form data title = form.title.data body = form.body.data post_id = str(random.randrange(1000000000, 9999999999)) # add the post to the database postsDB.insert( 0, { 'title': title, 'author': session.get('login')[1], 'content': body, 'comments': [], 'id': post_id }) userDB[session.get('login')[1]]['posts'].append({ 'title': title, 'author': session.get('login')[1], 'content': body, 'comments': [], 'id': post_id }) postsDBdict[title + '-' + post_id] = { 'title': title, 'author': session.get('login')[1], 'content': body, 'comments': [], 'id': post_id } session['message'] = 'You posted ' + title return redirect('/view-post=' + title + '-' + post_id) message = session.get('message') session['message'] = None return render_template('post.html', form=form, login=session.get('login'), message=message)
def edit_post(slug): post = Post.query.filter(Post.slug == slug).filter( Post.author == session.get('username')).first() form = PostForm( title=post.title, body=post.body, preview=post.preview, # реализовать удаление тегов tags=', '.join(list(str(i) for i in post.tags.__iter__()))) try: if request.form['submit'] == 'cancel': return redirect(url_for('posts.index')) except KeyError: pass if request.method == 'POST' and session[ 'username'] == post.author and form.validate_on_submit(): post.title = form.title.data post.body = form.body.data post.preview = form.preview.data tag_list = re.sub(r'[\s, .]', ' ', form.tags.data).split() post.tags.clear() db.session.commit() for i in set(tag_list): if Tag.query.filter_by(name=i).first(): tag = Tag.query.filter_by(name=i).first() else: tag = Tag(name=i) post.tags.append(tag) try: if request.form['submit'] == 'publish': post.visible = True db.session.commit() print('Post save') except: print('not save') return redirect(url_for('posts.post_content', slug=post.slug)) elif not session: return redirect(url_for('login.log_in')) return render_template( 'edit_post.html', post=post, form=form, )