def index(): form = PostForm() if form.validate_on_submit(): language = detected_language(form.post.data) if language == 'UNKNOWN': language = '' post = Post(body=form.post.data, author=current_user, language=language) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('main.index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, current_app.config['POSTS_PER_PAGE'], False) next_url = url_for('main.index', page=posts.next_num) if posts.has_next else None prev_url = url_for('main.index', page=posts.prev_num) if posts.has_prev else None return render_template('index.html', title='Home', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def upload_post(): post_form = PostForm() print('headers', current_app.config.get('WTF_CSRF_FIELD_NAME')) print('validete', post_form.validate(), post_form.errors) if post_form.validate_on_submit(): return jsonify({'success': 'upload post successfully'}) return jsonify({'error': 'fail to validete'})
def index(): form = PostForm() if form.validate_on_submit(): language = detect(form.post.data) if language == 'UNKNOWN' or len(language) > 5: language = '' post = Post(body=form.post.data, author=current_user, language=language) db.session.add(post) db.session.commit() flash(_l('Ваше сообщение было добавлено')) return redirect(url_for('main.index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, current_app.config['POSTS_PER_PAGE'], False) next_url = url_for('main.index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('main.index', page=posts.prev_num) \ if posts.has_prev else None all_users = User.query.all() return render_template('index.html', title='Главная', posts=posts.items, form=form, next_url=next_url, prev_url=prev_url, all_users=all_users)
def index(): form = PostForm() if form.validate_on_submit(): language = guess_language(form.post.data) if language == "UNKNOWN" or len(language) > 5: language = "" post = Post(body=form.post.data, author=current_user, language=language) db.session.add(post) db.session.commit() flash(_("Your post is now live!")) return redirect(url_for("main.index")) page = request.args.get("page", 1, type=int) posts = current_user.followed_posts().paginate( page, current_app.config["POSTS_PER_PAGE"], False) next_url = url_for("main.index", page=posts.next_num) if posts.has_next else None prev_url = url_for("main.index", page=posts.prev_num) if posts.has_prev else None return render_template( "index.html", title=_("Home"), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url, )
def edit_post(id, ver): if ver and ver > 1: abort(404) nom = Nom.query.get_or_404(id) author = current_user._get_current_object() post = Post.query.filter_by(nom=nom, author=author).first() if post is None: post = Post(keyword=nom.keyword, story="", nom=nom, author=author) db.session.add(post) form = PostForm() if form.validate_on_submit(): if post.keyword != form.keyword.data or \ post.story != form.story.data: post.keyword_backup = post.keyword post.keyword = form.keyword.data post.story_backup = post.story post.story = form.story.data post.shared = form.shared.data db.session.add(post) elif post.shared != form.shared.data: post.shared = form.shared.data db.session.add(post) cache.delete_memoized(user) return redirect(url_for('.nom_view', id=nom.id)) form.keyword.data = post.keyword_backup if ver else post.keyword form.story.data = post.story_backup if ver else post.story form.shared.data = post.shared return render_template('main/nom_edit.html', nom=nom, form=form, ver=ver)
def post(): form = PostForm() otherpost = Post.query.all() if form.validate_on_submit(): post = Post(post=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash(_('Your post is now live!')) return render_template('post.html', title=_('Post'), form=form, otherpost = otherpost)
def index(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=form.author.data) db.session.add(post) db.session.commit() return redirect(url_for('main.index')) posts = Post.query.order_by(Post.timestamp.desc()) return render_template('index.html', form=form, posts=posts)
def write_articles(): form = PostForm(user=current_user) if current_user.can(Permission.WRITE_ARTICLES) and form.validate_on_submit(): kind = PostKind.query.filter_by(name=form.kind.data).first() post = Post(title=form.title.data, outline=form.outline.data, body=form.body.data, author=current_user._get_current_object(), kind=kind) db.session.add(post) return redirect(url_for('.index')) return render_template('write_articles.html', form=form)
def add_post(): form = PostForm() error = None if form.validate_on_submit(): title = form.title.data if postService.is_unique_title(title): postService.add(title, form.body.data, current_user) return redirect(url_for('main.posts')) error = 'Title must be unique' return render_template('add_post.html', form=form, error=error)
def post(): form1 = PostForm() form2 = PlantFormDropDown() form2.garden.choices = [(g.id, g.name) for g in current_user.gardens] if form1.validate_on_submit(): post = Post(body=form1.post.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('main.index'))
def add_post(): form = PostForm() if current_user.can(Permission.WRITE_ARTICLES) and form.validate_on_submit(): post = Post(body=form.body.data, title=form.title.data, author=current_user._get_current_object()) post.addTag(form.tag.data) db.session.add(post) flash("发布成功!") return redirect(url_for('.post')) return render_template("auth/add_post.html", form=form)
def user(username): form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect('/index') user = User.query.filter_by(username=username).first_or_404() posts = Post.query.order_by(Post.timestamp.desc()).all() return render_template('user.html',form=form, user=user, posts=posts)
def index(): form = PostForm() if form.validate_on_submit(): part = CarPart(name=form.name.data, body=form.body.data) db.session.add(part) db.session.commit() flash("Your Post Is Live!") return redirect(url_for("main.index")) parts = CarPart.query.all() return render_template("index.html", title="Home", form=form, parts=parts)
def create_post(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.body.data, author=current_user, title=form.title.data) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('main.index')) return render_template('create_post.html', title='create', form=form)
def index(): form = PostForm() if current_user.can(Permission.WRITE) and form.validate_on_submit(): post = Post(body=form.body.data, author=current_user._get_current_object()) db.session.add(post) db.session.commit() flash("Submission of success !") return redirect(url_for('.index')) posts = Post.query.order_by(Post.timestamp.desc()).all() return render_template('index.html', form=form, posts=posts)
def post(): if not current_user.get_admin(): return redirect(url_for('index')) form = PostForm() if form.validate_on_submit(): stage = int(form.data.stage) users = mongo.db.users user = users.find_one({'name': form.username.data}) user['projects'][stage]['passed'] = True users.save(user) return redirect(url_for('profile', username=form.username.data)) return abort(404)
def edit_post(id): post = Post.query.get_or_404(id) if current_user != post.user and not current_user.is_administer(): abort(403) form = PostForm() if form.validate_on_submit(): post.body = form.body.data db.session.add(post) flash("博文修改完毕") return redirect(url_for("main.post", id=id)) form.body.data = post.body return render_template("edit_post.html", form=form)
def index(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.popst.data, author=current_user) db.session.add(post) db.session.commit() return redirect(url_for('index')) all = [{ 'name': user.username } for user in User.query.all()] return jsonify(all)
def add_post(): form = PostForm() if current_user.can( Permission.WRITE_ARTICLES) and form.validate_on_submit(): post = Post(body=form.body.data, title=form.title.data, author=current_user._get_current_object()) post.addTag(form.tag.data) db.session.add(post) flash("发布成功!") return redirect(url_for('.post')) return render_template("auth/add_post.html", form=form)
def edit(id): post = Post.query.get_or_404(id) if current_user != post.author and not current_user.can(Permission.ADMINISTER): abort(403) form = PostForm() if form.validate_on_submit(): post.body = form.body.data db.session.add(post) flash('The post has been update') return redirect(url_for('post', id=post.id)) form.body.data = post.body return render_template('edit_post.html', form=form)
def hansard(date): """shows the user the representative hansard for a particular date""" realdate = date #save real date for later use hansard = Hansard.query.filter_by( date=date, debate_type="representatives").first() #finds the hansard in question if hansard is None: flash('hansard not found.') return redirect( url_for('main.index') ) #if no hansard is found redirect to the main index with flashed message hansards = Hansard.query.filter_by(debate_type="representatives").all( ) #Queries all the possible representative hansards so it can be rendered as green in the calendar view dates = [] for date in hansards: dates.append(str(date.date).replace("-", "/")) page = request.args.get( 'page', 1, type=int ) #paginates the hansard so it can be viewed with a faster loading time. heading = hansard.majorheading.paginate(page, 1, False) form1 = PostForm( identifier="FORM1") #creates the form so users can submit new comments form2 = DateForm( identifier="FORM2" ) #creates the form so users can navigate to other hansards through the calender function if form1.identifier.data == 'FORM1' and form1.validate_on_submit( ): #if the POST request is for new comments add comment to be related to speech they requested and commit to database speech = Speech.query.filter_by(exact_id=form1.hidden.data).first() language = guess_language(form1.post.data) if language == 'UNKNOWN' or len(language) > 5: language = '' post = Post(body=form1.post.data, author=current_user, speech=speech, language=language) db.session.add(post) db.session.commit() return redirect(url_for('main.hansard', date=realdate)) if form2.identifier.data == 'FORM2' and form2.validate_on_submit( ): #if the POST request is for navigation to a new hansard, reload with new hansard date = form2.date.data.strftime("%Y-%m-%d") return redirect(url_for('main.hansard', date=date)) return render_template('hansard.html', you=False, subtitle="representative debates 🗣", url="/hansard/rep/" + realdate + "/extra?page=", data=hansard, pages=heading.pages, majorheading=heading.items, dates=dates, form1=form1, form2=form2)
def program(program_id): specialty2 = session.get('specialty') program = Program.query.filter_by(id=program_id).first_or_404() interviews = program.interviews.order_by(Interview.date.desc()) page = request.args.get('page', 1, type=int) postform = PostForm() postivform = PostIVForm() if postform.validate_on_submit() and current_user.is_authenticated: interview_impression = Interview_Impression( body=postform.post.data, author=current_user, program=program, name_and_shame=postform.name_and_shame.data) db.session.add(interview_impression) db.session.commit() flash(_('Your interview impression is now live!')) return redirect(url_for('main.program', program_id=program_id)) if postivform.validate_on_submit() and current_user.is_authenticated: postiv = PostInterviewCommunication( date_of_communication=postivform.date.data, author=current_user, program=program, type_of_communication=postivform.type_of_communication.data, personalized=postivform.personalized.data, content=postivform.content.data) db.session.add(postiv) db.session.commit() flash("Your post-interview impression is now live!") return redirect(url_for('main.program', program_id=program_id)) interview_impressions = program.interview_impressions.order_by( Interview_Impression.timestamp.desc()).paginate( page, current_app.config['POSTS_PER_PAGE'], False) next_url = url_for( 'main.program', id=program_id, page=interview_impressions.next_num ) if interview_impressions.has_next else None prev_url = url_for( 'main.program', id=program_id, page=interview_impressions.prev_num ) if interview_impressions.has_prev else None form = EmptyForm() return render_template( 'program.html', specialty2=specialty2, next_url=next_url, prev_url=prev_url, program=program, interviews=program.interviews, postform=postform, postivform=postivform, form=form, interview_impressions=interview_impressions.items, postiv_communications=program.postiv_communications.order_by( PostInterviewCommunication.timestamp.desc()))
def edit(id): post = Post.query.get_or_404(id) if current_user != post.author and \ not current_user.can(Permission.ADMINISTER): abort(403) form = PostForm() if form.validate_on_submit(): post.body = form.body.data db.session.add(post) flash('The post has been update') return redirect(url_for('main.post', id=post.id)) form.body.data = post.body return render_template('edit_post.html', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) post.save() flash('Your post has been created!', 'success') return redirect(url_for('main.index')) return render_template('new_post.html', title='New Post', form=form, legend='New Post')
def edit(id): post = Post.query.get_or_404(id) if current_user != post.author: abort(403) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data db.session.add(post) return redirect(url_for('.index', id=post.id)) form.title.data = post.title form.body.data = post.body return render_template('edit_post.html', form=form)
def user(username): user = User.query.filter_by(username=username).first_or_404() post_form = PostForm() if post_form.validate_on_submit(): post = Post(body=post_form.post.data, author=current_user) db.session.add(post) db.session.commit() flash(_('Your blog is now live')) return redirect(url_for('main.user', username=current_user.username)) posts = user.posts.order_by(Post.timestamp.desc()) form = EmptyForm() return render_template('user.html', user=user, form=form, post_form=post_form, \ posts=posts)
def edit(id): post = Post.query.get_or_404(id) if current_user != post.author: abort(403) form = PostForm() if form.validate_on_submit(): post.body = form.body.data db.session.add(post) db.session.commit() flash('The post has been updated.') return redirect(url_for('.post', id=post.id)) form.body.data = post.body return render_template('edit_post.html', form=form)
def submit(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, url=form.url.data, author=current_user) print(post.url) post.set_source(post.url) db.session.add(post) db.session.commit() flash(f"Your Post is now live") return redirect(url_for("main.index")) return render_template("submit.html", title="Create Post", form=form)
def index(): form = PostForm() if form.validate_on_submit(): # Attempt to identify language used in post language = guess_language(form.post.data) if language == 'UNKNOWN' or len(language) > 5: # Don't understand language, store as blank - no translation # will be offered language = '' post = Post(body=form.post.data, author=current_user, language=language) db.session.add(post) db.session.commit() flash(_('Your post is now live!')) # Redirect instead of render template because after post should always # redirect to prevent accidental form re-submission return redirect(url_for('main.index')) # Temporarily create fake/mock user # user = {'username': '******'} # Create fack/mock blog posts ''' posts = [ {'author': {'username': '******'}, 'body': 'Beautiful day in Paris!'}, {'author': {'username': '******'}, 'body': 'The dance competition was fantastic!'} ] ''' # Check query parameter for which page to start at page = request.args.get('page', 1, type=int) # Replace with database posts # Unpaginated version: # posts = current_user.followed_posts().all() # With pagination - page to render, how many posts/page, should we return 404 # if go past the end? (False = return empty list) posts = current_user.followed_posts().paginate(page, current_app.config['POSTS_PER_PAGE'], False) # Passing a keyword parameter to url_for causes it to use it as a query # parameter for the generated URL if it's not consumed (e.g., used for # dynamic parameter) next_url = url_for('main.index', page=posts.next_num) if posts.has_next else None prev_url = url_for('main.index', page=posts.prev_num) if posts.has_prev else None # Test without title to see Jinja2/template conditional: # return render_template('index.html', user=user) # return render_template('index.html', title='Home', user=user, posts=posts) # When eliminate fake user, no longer need to pass to template # When add pagination, posts is now a pagination object - to get lists of # posts, need to add .items # return render_template('index.html', title='Home', form=form, posts=posts) return render_template('index.html', title=_('Home'), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def feed(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() return redirect(url_for("main.feed")) page = request.args.get("page", 1, type=int) posts = current_user.followed_posts().paginate( page, current_app.config["PAGINATION_LIMIT"], False) return render_template("feed.html", title="Feed", form=form, posts=posts.items)
def staff_post(): if not current_user.is_authenticated: return redirect(url_for('auth.login')) form = PostForm() if form.validate_on_submit(): user = current_user is_dev = form.is_dev.data text = form.text.data project = form.project.data post = Post(is_dev=is_dev, text=text, project=project, user_id=user.id) db.session.add(post) db.session.commit() return redirect(url_for('main.index')) return render_template('staff/post.html', title="Staff Post", form=form)
def edit(slug): """ Used to edit a particular post. If the checkbox labeled ``Publish Now`` is ticked at the moment of posting the form the post will be published. Else it's going to be saved as a draft. Parameters ---------- slug : str The slug is the part of the URL which identifies a particular post on our blog in an easy to read form. """ title = 'Edit Post' post = Post.query.filter_by(slug=slug, is_page=False).first_or_404() form = PostForm() if form.validate_on_submit(): categories = set_categories(form.categories_field.data) post.title = form.title_field.data post.content = form.content_field.data post.is_published = form.publish.data post.is_page = False post.categories = categories post.slugify_title() if request.form.get('preview'): session['post'] = post session['categories'] = categories return redirect(url_for('main.preview', slug=post.slug)) else: try: db.session.commit() except exc.IntegrityError: db.session.rollback() flash('Error: This title is already in use.', 'danger') else: del_unused_categories() if post.is_published: flash('Post successfully edited and published.', 'success') return redirect(url_for('main.index')) else: flash('Post successfully edited and saved as draft.', 'success') return redirect(url_for('main.drafts')) elif request.method == 'GET': form.title_field.data = post.title form.content_field.data = post.content form.publish.data = post.is_published for i in range(len(post.categories.all())): form.categories_field.entries[i].data = post.categories[i].name return render_template('create.html', form=form, title=title, post=post)
def editpost(id): # pylint: disable=redefined-builtin qry = Post.query.filter_by(id=id).first() form = PostForm(request.form, obj=qry) if form.validate_on_submit(): if form.submit.data: form.populate_obj(qry) db.session.commit() flash('Your changes have been saved.') return redirect(url_for('main.index', id=id)) else: return redirect(url_for('main.index')) return render_template('editpost.html', title='Edit post', form=form, id=id)
def recipe(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('main.recipe')) posts = current_user.followed_posts().all() return render_template("recipe.html", title='Recipe', active_page="recipe", form=form, posts=posts)
def edit_post(id: int): post = Post.query.get(id) if post is None: post = Post(userid=current_user.id) elif post not in current_user.posts: return redirect(url_for('main.post', id=id)) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.add(post) db.session.commit() return redirect(url_for('main.post', id=id)) form.title.data = post.title form.content.data = post.content return render_template('edit_post.html', form=form)
def new_post(): form = PostForm() post = Post() if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data post.author = current_user db.session.add(post) db.session.commit() tag_ids = form.tags.data for tag_id in tag_ids: post_tags = PostTags(post_id=post.id, tag_id=tag_id) db.session.add(post_tags) flash('文章已发布.') return redirect(url_for('.post', title=post.url_title)) return render_template('edit_post.html', form=form, is_new=True)
def index(): form = PostForm() if current_user.can(Permission.WRITE_ARTICLES) and form.validate_on_submit(): post = Post(body=form.body.data, author=current_user._get_current_object()) db.session.add(post) return redirect(url_for('.index')) page = request.args.get('page', 1, type=int) show_followed = False if current_user.is_authenticated: show_followed = bool(request.cookies.get('show_followed', '')) if show_followed: query = current_user.followed_posts else: query = Post.query pagination = query.order_by(Post.timestamp.desc()).paginate(page, per_page=current_app.config[ 'FLASKY_POSTS_PER_PAGE'], error_out=False) posts = pagination.items return render_template('index.html', form=form, posts=posts, pagination=pagination, show_followed=show_followed)
def edit(id): post = Post.query.get_or_404(id) if current_user != post.author and not current_user.can(Permission.ADMINISTER): abort(403) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data post.last_modified = datetime.utcnow() # print("传过来的标签有:%s" % form.tag.data) post.updateTag(form.tag.data) db.session.add(post) flash("The post has been Update!") return redirect(url_for('.post')) form.title.data = post.title form.body.data = post.body form.tag.data = post.getTagByString() return render_template('auth/edit_post.html', form=form)
def edit_post(): post_form = PostForm() post_id = request.args.get('post') img_url = None if post_form.validate_on_submit() and 'image' in request.files: if post_id and Post.query.get(post_id): post = Post.query.get(post_id) post.title = post_form.title.data post.body = post_form.body.data img = Image.query.filter_by(post_id=post_id).first() images = save_image(request.files.getlist('image')) for url in images: img.url = url[0] img.url_t = url[1] db.session.add(img) db.session.add(post) flash('文章修改成功') else: post = Post(title=post_form.title.data, body=post_form.body.data, author=current_user._get_current_object()) db.session.add(post) images = save_image(request.files.getlist('image')) for url in images: img = Image(url=url[0], url_t=url[1], post=post) db.session.add(img) flash('文章创建成功') return redirect(url_for('.index')) if post_id: post = Post.query.get_or_404(post_id) post_form.title.data = post.title post_form.body.data = post.body img_url = post.images.first().url_t return render_template('create_post.html', post_form=post_form, img_url=img_url)
def index(): form = PostForm() if form.validate_on_submit(): language = detect_lang(form.post.data)['lang'] if language == 'UNKNOWN' or len(language) > 5: language = '' post = Post(body=form.post.data, author=current_user, language=language) db.session.add(post) db.session.commit() flash(_('Your post is now live!')) return redirect(url_for('main.index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, current_app.config['POSTS_PER_PAGE'], False) next_url = url_for('main.index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('main.index', page=posts.prev_num) \ if posts.has_prev else None return render_template('index.html', title=_('Home'), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def edit_post(title): post = Post.query.filter_by(url_title=title).first() if not post: abort(404) if current_user != post.author: abort(403) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data db.session.add(post) for tag in post.tags: db.session.delete(tag) tag_ids = form.tags.data for tag_id in tag_ids: post_tags = PostTags(post_id=post.id, tag_id=tag_id) db.session.add(post_tags) flash('文章已更新.') return redirect(url_for('.post', title=post.url_title)) form.title.data = post.title form.tags.data = [tag.id for tag in post.tags] form.body.data = post.body return render_template('edit_post.html', form=form, is_new=False)
def index(): form = PostForm() if current_user.can(Permission.WRITE_ARTICLES) and \ form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, user=current_user) db.session.add(post) db.session.commit() return redirect(url_for('main.index')) show_followed = False if current_user.is_authenticated: show_followed = bool(request.cookies.get('show_followed', '')) if show_followed: query = current_user.following_posts else: query = Post.query page = request.args.get('page', 1, type=int) pagination = query.order_by(Post.timestamp.desc()).paginate(page, per_page=current_app.config[ 'FLASK_PER_PAGE'] or 10, error_out=False) posts = pagination.items return render_template('index.html', form=form, posts=posts, show_followed=show_followed, pagination=pagination, Permission=Permission)
from flask import render_template, abort, flash, redirect, url_for, request, current_app