def index(): #print("helloworld...") user = g.user # fake user' form = PostForm() if form.validate_on_submit(): post = Post(body = form.post.data, timestamp = datetime.now(), author = user) db.session.add(post) db.session.commit() flash('your post is now live!') return redirect(url_for('index')) #posts = [ # { # 'author': {'nickname': 'John'}, # 'body': 'Beautiful day in Portland!' # }, # { # 'author': {'nickname': 'Susan'}, # 'body': 'The Avengers movie was so cool!' # } #] posts = user.followed_posts().all() app.logger.info('posts size %s' %(posts)) print('posts size %s' %(posts)) return render_template('index.html',title='Home', form = form, user = user, posts = posts)
def news(): form = PostForm() posts = Post.query.all() if form.validate_on_submit(): post = Post(body=form.text.data, timestamp=datetime.utcnow(), author=g.user) db.session.add(post) db.session.commit() return redirect(url_for('news')) return render_template('news.html', form=form, posts=posts)
def index(): form = PostForm() if form.validate_on_submit(): post = Post(body = form.body.data) db.session.add(post) db.session.commit() return redirect(url_for('.index')) posts = Post.query.order_by(Post.timestamp.desc()).all() return render_template('index.html', current_time = datetime.utcnow(), form = form, posts = posts)
def index(): posts = Post.query.all() form = PostForm() if form.validate_on_submit(): new_post = Post(request.form['title'], request.form['body']) db.session.add(new_post) db.session.commit() return redirect('/') return render_template('index.html', posts=posts, form=form)
def post_add(): form = PostForm() if form.validate_on_submit(): p = Post(user=g.user) form.populate_obj(p) db.session.add(p) db.session.commit() flash("新增文章成功", "success") return redirect(url_for("post_list")) return render_template("admin/postEdit.html", form=form, acurl='/admin/post/add')
def post_edit(post_id): p = Post.query.get_or_404(post_id) form = PostForm(obj=p) if form.validate_on_submit(): form.populate_obj(p) db.session.commit() flash("更新文章成功", "success") return redirect(url_for("post.post_list")) return render_template('admin/postEdit.html', post=p, form=form, acurl="/admin/post/" + str(post_id) + "/edit/")
def post(self): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, body=form.post.data, timestamp=datetime.utcnow(), author=g.user) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('IndexView:get_0')) posts = Post.query.all() return render_template('index.html', title='Home', user=g.user, posts=posts, form=form)
def edit(post_id): post = Post.query.get_or_404(post_id) post.permissions.edit.test(403) form = PostForm(obj=post) if form.validate_on_submit(): form.populate_obj(post) post.save() flash(u"你的条目已更新", "successfully") return redirect(url_for("post.view", post_id=post_id)) return render_template("post/edit_post.html", post=post, form=form)
def index(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash('Ваш пост скоро появится!') return redirect(url_for('index')) posts = current_user.followed_posts().all() return render_template('index.html', title='Home', form=form, posts=posts)
def index(): 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('index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate(page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) if posts.has_next else None prev_url = url_for('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 index(page = 1): form = PostForm() if form.validate_on_submit(): post = Post(body = form.post.data, timestamp = datetime.utcnow(), author = g.user) db.session.add(post) db.session.commit() flash('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 edit_post(id): """Show edit post page. Allows user to update details for specified existing post. If the specified post could not be found a HTTP 404 response will be generated. """ post = Post.query.get_or_404(id) form = PostForm(title=post.title, markup=post.markup, visible=post.visible) if form.validate_on_submit(): post.update(form.title.data, form.markup.data, form.visible.data) db.session.commit() flash('Saved changes!', 'info') return render_template('admin/post/edit.html', post=post, form=form)
def theme(section_name, theme_name, page=1): user = g.user section = Section.query.filter_by(name=section_name).first() theme = Themes.query.filter_by(section_id=section.id, name=theme_name).first() posts = Post.query.filter_by(theme_id=theme.id).paginate(page, 5, False) form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, timestamp=datetime.utcnow(), author=g.user, theme=theme) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('theme', section_name=section.name, theme_name=theme.name, page=page)) return render_template("theme.html", title=theme.name, section_name=section.name, theme_name=theme.name, user=user, form=form, posts=posts)
def new_post(): """Displays new page form and handles post creation""" form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, body=form.body.data, user_id=g.user.id, publish_time=form.start_time.data, difficulty=form.difficulty.data) post.add_ingredients(form.ingredients.data) db.session.add(post) db.session.commit() return redirect((url_for('post', id=post.id))) return render_template('new-post.html', title='New post', form=form)
def post(): '''############################### Submit a new post ################################## ''' form = PostForm() if form.validate_on_submit(): #save the post post = Post(body=form.post.data, timestamp=datetime.utcnow(), author=g.user) db.session.add(post) db.session.commit() flash('Your post is live!') res = es.index(index="microblog", doc_type='post', body=dict(id=post.id, body=post.body, user_id=post.user_id, timestamp=post.timestamp)) app.logger.debug(res['created']) return redirect(url_for('.index'))
def new_post(): """Show create new post page. Allows user to create a new post. After creating a post the user will be redirected to the edit post page. """ form = PostForm() if form.validate_on_submit(): post = Post( form.title.data, form.markup.data, current_user.id, form.visible.data ) db.session.add(post) db.session.commit() flash('Created post <strong>%s</strong>!' % post.title, 'success') return redirect(url_for('admin.edit_post', id=post.id)) return render_template('admin/post/new.html', form=form)
def index(): title = 'logStream' subtitle = 'The cake is a lie!!' form = PostForm() if form.validate_on_submit(): if current_user.is_authenticated() is False: flash('no login no BB') return redirect(url_for('index')) tags = map(lambda x:x.strip(' '), form.tags.data.split(',')) tags = list(set(tags)) if '' in tags: tags.remove('') post_new(body=form.body.data, user=current_user._get_current_object(), tagnames=tags) pdb.set_trace() flash('post a new post success') return redirect(url_for('index')) posts = Post.query.order_by(Post.timestamp.desc()).all() return render_template('index.html', title=title, subtitle=subtitle, form=form, posts=posts)
def admin_add_post(): form = PostForm() if request.method == 'POST' and form.validate_on_submit(): post = Post(title=request.form['title'], content=request.form['content'], summary=request.form['summary']) post.author_id = ModelManager.current_user().id post.category_id = form.category.data post.before_save() db.session.add(post) db.session.commit() db.session.flush() flask.flash('Your post has been created', 'success') return flask.render_template(Theme.get_template(page='admin_add_post'), manager=ModelManager, post_form=form)
def index(): 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('index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('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 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, #anguage=language) ) db.session.add(post) db.session.commit() flash('您的模式已发布成功!') return redirect(url_for('index')) '''if form2.validate_on_submit(): language = guess_language(form2.pattern.data) if language == 'UNKNOWN' or len(language) > 5: language = '' pattern = Post(pattern=form2.pattern.pattern2, author=current_user, language=language) db.session.add(pattern) db.session.commit() flash('Your pattern is now live!') return redirect(url_for('index'))''' page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('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 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('index')) posts = current_user.followed_posts().all() return render_template("index.html", title='Home Page', form=form, posts=posts) return render_template("index.html", title='Home Page', form=form, posts=posts)
def index(): form = PostForm() if form.validate_on_submit(): web = PostURL(url=form.url.data, website_name=form.web_name.data) db.session.add(web) db.session.commit() flash(form.web_name.data + ' ' + form.url.data) flash(_('Your post is now live!')) return redirect(url_for('main.index')) page = request.args.get('page', 1, type=int) posts_urls = PostURL.query.order_by(PostURL.timestamp.desc()).paginate( page, current_app.config['POSTS_PER_PAGE'], False) next_url = url_for('main.explore', page=posts_urls.next_num) \ if posts_urls.has_next else None prev_url = url_for('main.explore', page=posts_urls.prev_num) \ if posts_urls.has_prev else None return render_template('index.html', title=_('Home'), form=form, posts_url=posts_urls.items, next_url=next_url, prev_url=prev_url)
def index(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, timestamp=datetime.datetime.utcnow(), author=g.user) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('index')) posts = [{ 'author': { 'nickname': 'John' }, 'body': 'Beautiful day in Portland!' }, { 'author': { 'nickname': 'Susan' }, 'body': 'The Avengers movie was so cool!' }] return render_template('index.html', title='Home', form=form, posts=posts)
def blog1(): form = PostForm() if form.validate_on_submit(): if current_user.is_authenticated: # 获取当前登录的用户 u = current_user._get_current_object() p = Posts(title=form.title.data, content=form.content.data, user=u) db.session.add(p) db.session.commit() return redirect(url_for('users.blog1')) else: flash("请先登录") print(form.errors) return redirect(url_for('users.login')) page = request.args.get('page', 1, type=int) pagination = Posts.query.filter_by(rid=0).order_by( Posts.timestamp.desc()).paginate(page, per_page=5, error_out=False) posts = pagination.items return render_template('user/blog1.html', form=form, posts=posts, pagination=pagination)
def posts(username): form = PostForm() # query database for proper person person = User.query.filter_by(username=username).first() # when form is submitted append to posts list, re-render posts page if form.validate_on_submit(): tweet = form.tweet.data post = Post(tweet=tweet, user_id=current_user.id) # add post variable to database stage, then commit db.session.add(post) db.session.commit() return redirect(url_for('posts', username=username)) return render_template('posts.html', person=person, title='Posts', form=form, username=username)
def index(): 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!') """ After we process the form data, we end the request by issuing a redirect to the homepage. It's standard practice to respond to a POST request generated by a web form submission with a redirect. It helps mitigate an annoyance with how the refresh command is implemented in web browsers. When you refresh a page, the web browser is just re-issuing the last request. If a POST request with a form submission returns a regular response, then a refresh will re-submit the form. Since this behaviour is unexpected the browser is going to ask the user to confirm the duplicate submission. If a POST request is answered with a redirect, the browser is now instructed to send a GET request to grab the page indicated in the redirect, so the latest request is no longer a POST, its a GET. And the refresh command as a result, works in a more predictable way """ return redirect(url_for('index')) # return render_template('index.html', title='Home', user=user) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('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 index(): # step 75 in the Workflow form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash("You post has been submitted") # POST/redirect/GET pattern return redirect(url_for("index")) # user = {"username": "******"} # posts = [ # {"author": {"username": "******"}, "body": "Mufasani man oldirganman"}, # {"author": {"username": "******"}, "body": "Shut up, you stupid donkey!"}, # ] # step 75 in the Workflow # posts = current_user.followed_posts().all() # step 83 in the Workflow - modify the step 75 page = request.args.get("page", 1, type=int) posts = current_user.followed_posts().paginate( page, app.config["POSTS_PER_PAGE"], False ) # step 85 in the Workflow next_url = url_for("index", page=posts.next_num) if posts.has_next else None prev_url = url_for("index", page=posts.prev_num) if posts.has_prev else None return render_template( "index.html", title="Home", posts=posts.items, form=form, next_url=next_url, prev_url=prev_url, ) # read the step 39
def index(): 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('index')) posts = current_user.followed_posts().all() return render_template("index.html", title='Home Page', form=form, posts=posts) # limit number of posts appearing from followed friend page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) return render_template('index.html', title='Home', form=form, posts=posts.items)
def myaccount(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash('Din post är skickat!') return redirect(url_for('myaccount')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('myaccount', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('myaccount', page=posts.prev_num) \ if posts.has_prev else None return render_template('myaccount.html', drop_down_cats=drop_down_cats, title='Mitt Konto', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def explore(): """Funkcja wyświetlająca strone główną aplikacji wraz z postami wszystkich użytkowników.""" form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash('Succesfully added a post!') return redirect(url_for('explore')) page = request.args.get('page', 1, type=int) posts = Post.query.order_by(Post.timestamp.desc()).paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('explore', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('explore', page=posts.prev_num) \ if posts.has_prev else None return render_template('index.html', title=_("Explore"), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def profile(username): form = PostForm() if form.validate_on_submit(): # step 1: create an instance of the db model post = Post(tweet=form.tweet.data, user_id=current_user.id) #step 2: add the record to the stage db.session.add(post) #step 3: commit the stage to the database db.session.commit() return redirect(url_for('profile', username=username)) #retrieve all posts and pass in to view #pass in user via the username taken in user = User.query.filter_by(username=username).first() return render_template('profile.html', title='Profile', form=form, user=user)
def index(): 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('index')) page = request.args.get('page', 1, type=int) # line 22 should be changes as just shows your own followers. Alternatively remove this functionality? posts = current_user.posts.paginate(page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) \ if posts.has_prev else None return render_template("index.html", title='Home Page', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def write(): form = PostForm() if form.validate_on_submit() and request.method == 'POST': temp_store = 0 if request.form.get('temp'): temp_store = 1 post = Post(title=form.title.data, content=form.content.data, user_id=current_user.id, cat_id=form.cat.data, temp_store=temp_store) db.session.add(post) db.session.commit() soup_post = BeautifulSoup(post.content, 'lxml') img_set = {img['src'] for img in soup_post.find_all('img')} if img_set: for img in img_set: f_fullname = os.path.basename(img) f_path = os.path.join(app.config['UPLOADED_PATH'], str(current_user.id), f_fullname) db_image = UploadImage.query.filter_by( user_id=current_user.id, image_path=f_path).first() if db_image: db_image.mark = 1 try: db.session.commit() except: flash('服务异常') return render_template('write.html', title='写作ing', form=form) Use_Redis.eval('index', '*', disable=flag) Use_Redis.eval('cat', str(form.cat.data), '*', disable=flag) Use_Redis.eval('cat', '0', '*', disable=flag) Use_Redis.eval('srh', '*', disable=flag) Use_Redis.delete('total', 'post', disable=flag) flash('提交成功!') if current_user.id == 1: return redirect(url_for('user', username=current_user.username)), 301 return render_template('write.html', title='写作ing', form=form)
def edit_review(showID): show = Show.query.filter_by(id=showID).first_or_404() review = show.reviews.filter(Review.user_id == current_user.id).one() form = PostForm() if form.validate_on_submit(): totalRating = show.rating * show.watched newTotalRating = totalRating + form.rating.data - review.rating newRating = newTotalRating / show.watched show.rating = newRating review.body = form.post.data review.rating = form.rating.data flash('Your changes have been saved.') db.session.commit() return redirect(url_for('index')) elif request.method == 'GET': form.rating.data = review.rating form.post.data = review.body return render_template('edit_review.html', title='Edit Review', form=form, show=show)
def user(nickname, page=1): user_tmp = User.query.filter_by(nickname=nickname).first() if user_tmp is None: flash(gettext('User %(user)s don\'t found.',user=nickname), 'warning') return redirect(url_for('index')) posts = Post.query.filter_by(wall_id=user_tmp.id).paginate(page, POSTS_PER_PAGE, False) post_delete_form = PostDeleteForm() post_form = PostForm() if post_delete_form.validate_on_submit(): post_tmp = Post.query.get(post_delete_form.post_id.data) if post_tmp.author == current_user or current_user.have_role('admin'): try: post_tmp.delete() except Exception as e: flash(gettext('Can\'t delete post, error - %(error)s', error=e), 'danger') else: flash(gettext('Post has been deleted'), 'info') else: flash(gettext('You don\'t have an access to do this'), 'danger') return redirect(request.path) if post_form.validate_on_submit(): user_tmp.post_on_the_wall(user=current_user, body=post_form.post.data) return redirect(request.path) return render_template('user.html', title='User :' + nickname, user=user_tmp, post_form=post_form, post_delete_form=post_delete_form, posts=posts, current_page=page )
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, body2=form.post2.data, author=current_user, language=language) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('index')) posts = [{ 'author': { 'username': '******' }, 'body': 'Beautiful day in Portland!' }, { 'author': { 'username': '******' }, 'body': 'The Avengers movie was so cool!' }] page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('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 index(): form = PostForm() if form.validate_on_submit(): # datetime.strptime(str(datetime.now(timezone.utc)), '%Y-%m-%d %H:%M:%S.ffffff%Z').replace(microsecond=0).isoformat(), \ #curdt = datetime.utcnow().isoformat curdt = datetime.utcnow().isoformat() #*** get form data and write it out to a file *** postdict = { 'videourl': form.videourl.data, \ 'uniqueid': form.videourl.data, \ 'title': form.title.data, \ 'description': form.description.data, \ 'pubdate': curdt } #'uniqueid': form.uniqueid.data, \ #'pubdate': datetime.strptime(str(form.pubdate.data), '%Y-%m-%d').isoformat(), \ #} #flash(postdict) #*** If output file doesn't exist, create it and write to new file *** feeds = [] if not os.path.isfile('MSNIngest.json'): feeds.append(postdict) with open('MSNIngest.json', mode='w', encoding='utf-8') as f: f.write(json.dumps(feeds, indent=2)) else: #*** otherwise, open the file, read in the contents and append new data *** with open('MSNIngest.json', mode='r', encoding='utf-8') as feedsjson: feeds = json.load(feedsjson) feeds.append(postdict) with open('MSNIngest.json', mode='w', encoding='utf-8') as f: f.write(json.dumps(feeds, indent=2)) flash('Your post has been saved to an output file!') return redirect(url_for('index')) return render_template('index.html', title='Home Page', form=form)
def index(): # Add post to DB if this is a post 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('index')) # Get the page of posts page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) # Generate next and previous page urls if posts.has_next: next_url = url_for('index', page=posts.next_num) else: next_url = None if posts.has_prev: prev_url = url_for('index', page=posts.prev_num) else: prev_url = None return render_template('index.html', title='Home', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): form = PostForm() if form.validate_on_submit(): language = guess_language(form.post.data) flash( 'I guessed that this language is {} and has a length of {}. The locale is {}' .format(language, len(language), g.locale)) 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!')) '''It is a standard practice to respond to a POST request generated by a web form submission with a redirect. This helps mitigate an annoyance with how the refresh command is implemented in web browsers. The browser is now instructed to send a GET request to grab the page indicated in the redirect.''' return redirect(url_for('index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('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 index(): """ Home Page view function, where you can see posts and make posts of your own. """ form = PostForm() if form.validate_on_submit(): post.Post(body=form.post.data, author=current_user) db.session.add(post) db.session.comit() flash("Your post is now live!") # we use the simple Post/Redirect/Get pattern trick to avoid # inserting duplicate posts when a user inadvertently refreshes the page # after submitting a webform. return redirect(url_for("index")) # pagination of posts on the front page of all posts # of users current_user is following, including own, # ordered retro-chronoclogically page = requests.args.get("page", 1, type=int) # load N posts per page using pagination posts = current_user.followed_posts().paginate( page, app.config["POSTS_PER_PAGE"], False ) # previous page url prev_url = url_for("index", page=posts.prev_num) if posts.has_prev else None # next page url next_url = url_for("index", page=posts.next_num) if posts.has_next else None return render_template( "index.html", title="Home", form=form, posts=posts.items, prev_url=prev_url, next_url=next_url, )
def index(): form = PostForm() if form.validate_on_submit( ): # allows users to post on blog which are then saved to database post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post is now up!') return redirect(url_for('index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) # shows posts of user and the users that they follow next_url = url_for('index', page=posts.next_num) if posts.has_next else None prev_url = url_for('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 index(): form = PostForm() if form.validate_on_submit(): res = mysql.Add("post", ['NULL', "'%s'" % form.post.data, "'%s'" % current_user.id, "'%s'" % now()]) if res == 1: flash('Your post is now live!') return redirect(url_for('index')) page = request.args.get('page', 1, type=int) all_posts = current_user.followed_posts() post_per_page = app.config['POSTS_PER_PAGE'] posts = all_posts[(page - 1) * post_per_page:page * post_per_page if len( all_posts) >= page * post_per_page else len(all_posts)] next_url = url_for('explore', page=page + 1) \ if len(all_posts) > page * post_per_page else None prev_url = url_for('explore', page=page - 1) \ if (page > 1 and len(all_posts) > page * post_per_page) else None usernames = [] for i in posts: usernames.append(load_user(i[2])) return render_template('index.html', title='Home', form=form, posts=posts, next_url=next_url, prev_url=prev_url, usernames=usernames, izip=izip, avatars=avatar, dt=datetime.strptime)
def home(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash( _('%(username)s just posted a message', username=current_user.username)) return redirect(url_for('home')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('home', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('home', page=posts.prev_num) \ if posts.has_prev else None return render_template('home.html', title=_('Home'), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def home(): page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('home', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('home', page=posts.prev_num) \ if posts.has_prev else None form = PostForm() if form.validate_on_submit(): db.session.add( Post(body=form.body.data, user_id=current_user.id, timestamp=datetime.utcnow())) db.session.commit() flash('You\'ve just added a new post') return redirect(url_for('home')) return render_template('home.html', title='Homepage', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def profile(username=''): # If username is empty if not username: return redirect(url_for('login')) form = PostForm() person = User.query.filter_by(username=username).first() if form.validate_on_submit(): tweet = form.tweet.data post = Post(user_id=person['id'], tweet=tweet) # Commit to database db.session.add(post) db.session.commit() return redirect(url_for('profile', username=username)) return render_template('profile.html', title='Profile', person=person, form=form)
def index(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash("Seu post agora está no ar!") return redirect(url_for("index")) page = request.args.get("page", 1, type=int) posts = current_user.followed_posts().paginate( page, app.config["POSTS_PER_PAGE"], False) next_url = url_for("index", page=posts.next_num) if posts.has_next else None prev_url = url_for("index", page=posts.prev_num) if posts.has_prev else None return render_template( "index.html", title="Página Inicial", posts=posts.items, form=form, next_url=next_url, prev_url=prev_url, )
def edit_post(slug): post = Post.query.filter_by(slug=slug).first() form = PostForm() if post is None: flash('Bài này có đâu mà sửa.') return redirect(url_for('index')) if post.author == current_user: if form.validate_on_submit(): post.title = form.title.data post.description = form.description.data post.content = form.content.data db.session.commit() flash('Đã lưu') return redirect(url_for('edit_post', slug=post.slug)) elif request.method == 'GET': form.title.data = post.title form.description.data = post.description form.content.data = post.content else: flash('Không thể sửa bài của người khác') return redirect(url_for('index')) return render_template('post.html', post=post, title=post.title, form=form)
def index(): 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 live now!')) return redirect(url_for('index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, microblogapp.config['POSTS_PER_PAGE'], False) next_url = url_for('index',page=posts.next_num) \ if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) \ if posts.has_prev else None #posts = current_user.followed_posts().all() #Calling all() on the query triggers its execution, #with the return value being a list with all the results return render_template('index.html', title='Home', posts=posts.items, form=form, prev_url=prev_url, next_url=next_url)
def index(): form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash('Ditt inlägg är nu skickat!') return redirect(url_for('index')) posts = [{ 'author': { 'username': '******' }, 'body': 'Jag kan jobba på torsdag och fredag.' }, { 'author': { 'username': '******' }, 'body': 'Jag är i Magaluf!' }] return render_template("index.html", title='Dashboard', form=form, posts=posts)
def post(id): """Handles displaying the page, and all operations done on the page: edit, delete, submit an entry, and choose winner """ def reload_page(): return redirect(url_for('post', id=id)) post = Post.query.filter_by(id=id).first() if post is None or \ (not post.is_visible() and not is_current_user(post.author.id)): flash('Post not found', 'error') return redirect(url_for('home')) if is_current_user(post.author.id): if request.args.get('edit', '') == "1": if post.is_archived(): flash('Archived posts cannot be modified', 'error') return reload_page() form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data post.difficulty = form.difficulty.data post.publish_time = form.start_time.data post.remove_ingredients() post.add_ingredients(form.ingredients.data) db.session.commit() return reload_page() form.title.data = post.title form.body.data = post.body form.start_time.data = post.publish_time form.difficulty.data = post.difficulty form.ingredients.pop_entry() for ingredient in post.get_ingredients(): form.ingredients.append_entry(ingredient) return render_template('new-post.html', title='Edit post', form=form) if request.args.get('delete', '') == "1": if post.is_archived(): flash('Archived posts cannot be deleted', 'error') return reload_page() db.session.delete(post) db.session.commit() return redirect(url_for('home')) if request.args.get('submit', '') == "1": flash('Cannot submit entry to own challenge', 'error') return reload_page() winner = request.args.get('winner', '') if winner: submission = Submission.query.filter_by(id=winner).first() try: submission.make_winner() db.session.commit() except IntegrityError: flash("There is a winner already" 'error') return reload_page() else: if request.args.get('submit', '') == "1": if not is_user_logged_in(): flash('Please log in to submit', 'error') return redirect(url_for('login')) if Submission.query.filter_by(user_id=g.user.id, post_id=id).all(): flash('Submission already exists', 'error') return reload_page() if not post.are_submissions_open(): flash('Submissions are not open for this post', 'error') return reload_page() form = SubmissionForm() if form.validate_on_submit(): path = 'uploads/' + secure_filename(form.image.data.filename) form.image.data.save(path) text = form.body.data submission = Submission(path, text, g.user.id, id) db.session.add(submission) db.session.commit() os.remove(path) flash('Submission successful', 'info') return reload_page() return render_template('submit.html', title='Submit entry', form=form) context = { 'post': post, 'title': post.title, # populates the title tag in the head of the page 'is_logged_in': is_user_logged_in(), 'is_author': is_current_user(post.author.id), 'can_edit': is_current_user(post.author.id) and not post.is_archived(), } return render_template('post.html', **context )