def thread(request, thread_id): ''' A view for rendering the threads ''' thread_id = int(thread_id) try: current_thread = Thread.objects.get(id=thread_id) except Thread.DoesNotExist: return HttpResponse("404 Thread does not exist") context_dict = { 'thread': current_thread, 'thread_report_form': ThreadReportForm(), 'post_report_form': PostReportForm() } if request.method == 'POST': post_form = PostForm(request.POST) if post_form.is_valid(): post = post_form.save(commit = False) post.thread = current_thread post.poster = request.META['REMOTE_ADDR'] post.save() current_thread.last_post_time = post.time_posted current_thread.post_count += 1 current_thread.save() print(str(current_thread.last_post_time)) else: print(post_form.errors) context_dict['post_form'] = PostForm() context_dict['posts'] = Post.objects.filter(thread = current_thread) context_dict['num_posts'] = len(context_dict['posts']) return render(request, 'app/thread.html', context_dict)
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_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_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(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 test_form_valid(self): Syntax.objects.create(syntax_name="syntax_name1") post_obj_data = { u'title': u'title', u'code': u'code', u'ttl_option': u'minutes=10', u'syntax': "1" } post_form = PostForm(data=post_obj_data) self.assertTrue(post_form.is_valid())
def post_edit(request, pk): post = get_object_or_404(Post, pk=pk) if request.method == "POST": form = PostForm(request.POST, instance=post) if form.is_valid(): post = form.save(commit=False) post.author = request.user post.save() return redirect('app.views.post_detail', pk=post.pk) else: form = PostForm(instance=post) return render(request, 'app/post_edit.html', {'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 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 dbPost(request): args = {} if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): post = form.save( commit = False) post.feature_id = request.POST.get("feature_id", "") specId = request.POST.get("species", "") citeId = request.POST.get("citation", "") post.username = request.POST.get("username", "") post.dt = request.POST.get("dt", "") post.species_id = Species.objects.get(species_id_html = specId) post.traits = request.POST.get("traits", "") if request.POST.get("mean","") == "": post.mean = None else: post.mean = float(request.POST.get("mean","")) if request.POST.get("range0","") == "": r1 = None else: r1 = float(request.POST.get("range0","")) if request.POST.get("range1","") == "": r2 = None else: r2 = float(request.POST.get("range1","")) post.range = [r1,r2] post.uncertainty = request.POST.get("uncertainty","") post.units = request.POST.get("units","") post.cite = Citation.objects.get(citation_name = citeId) post.save() cnt = CitationNumerictraitSpecies() cnt.relation_id = CitationNumerictraitSpecies.objects.all().order_by('-relation_id').values_list()[0][0] + 1 cnt.feature = NumericTraits.objects.get(feature_id = request.POST.get("feature_id", "")) cnt.cite = Citation.objects.get(citation_name = citeId) cnt.species = Species.objects.get(species_id_html = specId) cnt.save() return HttpResponse('') else: print form.errors else: form = PostFormOther() args['form'] = form return render(request, 'app/lifehistory.html', args)
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 post_new(request): if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.author = request.user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] post.save() return redirect('app.views.post_detail', pk=post.pk) else: form = PostForm() return render(request, 'app/post_edit.html', {'form': form})
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 test_form_creates_model(self): Syntax.objects.create(syntax_name="syntax_name1") post_obj_data = { u'title': u'post_1', u'code': u'code_1', u'ttl_option': u'minutes=10', u'syntax': "1" } post_form = PostForm(data=post_obj_data) post_form.save() post_obj = Post.objects.get(title=post_obj_data['title']) self.assertTrue(isinstance(post_obj, Post)) self.assertEqual(post_obj.title, post_obj_data['title'])
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 home(request): """Renders the home page.""" assert isinstance(request, HttpRequest) if request.method == 'GET': form = PostForm() else: form = PostForm(request.POST) # Bind data from request.POST into a PostForm if form.is_valid(): imgURL = form.cleaned_data['content'] app_id = "DbZ4NzfrPL-K_CHHf4y4srnvBUSgMo4Dz9BIbeXt" app_secret = "crjTy-8St_kiFkL0wZZCFyrcoWJyOdets8Fa1BNi" clarifai_api = ClarifaiApi(app_id,app_secret) tags = '' embedLink = '' try: result = clarifai_api.tag_image_urls(imgURL) except: #if url is invalid based on clarifai API call tags = 'invalid url' imgURL = '' if tags!='invalid url': tagList = result['results'][0]['result']['tag']['classes'] bestGenre = imgscore(tagList,genres) r = requests.get('https://api.spotify.com/v1/search?q=%22'+bestGenre+'%22&type=playlist') jsonStuff = r.json() uri = jsonStuff['playlists']['items'][0]['uri'] embedLink = "https://embed.spotify.com/?uri="+uri return render( request, 'app/index.html', { 'form': form, 'imgsrc': imgURL, 'debugText': tags, 'playlistURI': embedLink, 'year':datetime.now().year, } ) return render( request, 'app/index.html', { 'form': form, 'imgsrc': '', 'debugText': '', 'playlistURI': '', 'year':datetime.now().year, } )
def add_post(): form = PostForm(request.form) if request.method == 'POST' and form.validate(): existing_post = Post.query.filter_by(title=form.title.data).first() if existing_post: form.title.errors.append('Post with such title already exists') else: post = Post(form.title.data, form.content.data, current_user.id, form.tags.data) db.session.add(post) db.session.commit() flash('Successfully added post') return redirect(url_for('show_post', post_id=post.id)) return render_template('add_post.html', form=form)
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 editpost(postkey): form = PostForm() client = Client() post = client.get(Key('post', int(postkey), project=config.Config.GOOGLE_PROJECT)) if post['user_id'] != current_user.id: flash('Can not edit other user''s post') return redirect(url_for('index')) form.subject.data = post['subject'] form.text.data = post['text'] session['update_post_id'] = postkey return render_template('editpost.html', title='Home', form=form)
def reviews(): 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('reviews')) 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('reviews', page=posts.next_num) if posts.has_next else None prev_url = url_for('reviews', page=posts.prev_num) if posts.has_prev else None return render_template('reviews.html', title=_('Відгуки'), 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')) 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"), posts=posts.items, form=form, next_url=next_url, prev_url=prev_url)
def index(): form = PostForm() page = request.args.get('page', 1, type=int) if form.validate_on_submit(): post = Post(body=form.post.data, user_id=current_user.id) flash('Your post successfully added') db.session.add(post) db.session.commit() return redirect(url_for('index')) posts = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_page = url_for('index', page=posts.next_num) if posts.has_next else None prev_page = 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_page=next_page, prev_page=prev_page)
def index(): form = PostForm() if form.validate_on_submit(): if current_user.is_authenticated: u = current_user._get_current_object() p = Posts(content=form.content.data, user=u) db.session.add(p) db.session.commit() return redirect(url_for('main.index')) else: flash('请先登录') return redirect(url_for('users.login')) # posts = Posts.query.filter_by(rid=0).all() page = request.args.get('p', 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('main/index.html', form=form, posts=posts, pagination=pagination)
def index(): users = User.query.all() print("users: {}".format(users)) 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!') 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, users=users)
def index(): """Function to list posts and create new ones for Hardcoded User""" form = PostForm() new_post = Posts(body=form.body.data) user = {'username': '******'} if form.validate_on_submit(): post.body = form.body.data db.session.add(new_post) db.session.commit() flash('įrasas paskelbtas') return redirect(url_for('index')) posts = Posts.query.order_by(Posts.date_posted.desc()) if form.validate_on_submit(): flash(f'Naujas {user["username"]} įrasas įkeltas') return redirect(url_for('index')) return render_template('index.html', title='Home page', user=user, posts=posts, form=form)
def make_posts(): id = current_user.get_id() #same ID user = User.query.get(id) if not user.confirmed: flash('Please confirm your email first!') return redirect(url_for('index')) form = PostForm() if form.validate_on_submit(): post = Post(name=form.name.data, email=form.email.data, gender=form.gender.data, body=form.body.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('index')) return render_template('make_posts.html', title='Make your Post', form=form)
def history(): 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('history')) 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('history', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('history', page=posts.prev_num) \ if posts.has_prev else None return render_template('history.html', title='Background - js', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): form = PostForm() print(current_user) if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user.username) post.save() flash("Your post has been saved") return redirect(url_for("index")) page = request.args.get('page', 1, type=int) posts = Post.objects().order_by("-timestamp").paginate(page=page, per_page=2) 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():# this is called a view function form = PostForm() if form.validate_on_submit(): base_entity = BaseEntity() db_instance.session.add(base_entity) db_instance.session.commit() db_instance.session.refresh(base_entity) post = Post(body=form.post.data, author=current_user, entity_id=base_entity.id) db_instance.session.add(post) db_instance.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_instance.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", postslist = posts.items, form=form, next_url=next_url,prev_url=prev_url)
def submit_post(request): """Create a form to submit post. """ context = RequestContext(request) if request.POST: postform = PostForm(data=request.POST) if postform.is_valid(): postform.save(commit=True) return HttpResponseRedirect('/app/blog') else: print postform.errors else: postform = PostForm() print postform context_dict = { 'postform': postform, } return render_to_response("app/submitpost.html", context_dict, context)
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 = [{ 'author': { 'username': '******' }, 'body': 'Some beautiful text' }, { 'author': { 'username': '******' }, 'body': 'Susan B Anthony is the GREATEST!' }] return render_template('index.html', title='Home', posts=posts)
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 index(): # ..../index?page=3 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')) # Post - Redirect - Get 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 # next_num = 4 生成去往下一页的url ..../index?page=4 prev_url = url_for('index', page=posts.prev_num) \ if posts.has_prev else None # next_num = 2 生成去往上一页的url ..../index?page=2 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(): 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 show(id): form = PostForm() show = Show.query.filter_by(id=id).first_or_404() reviews = Review.query.filter_by(show_id=id) if form.validate_on_submit(): review = Review(body=form.post.data, author=current_user, show=show, rating=form.rating.data) db.session.add(review) totalRating = show.rating * show.watched newTotalRating = totalRating + form.rating.data newWatched = show.watched + 1 newRating = newTotalRating / newWatched show.rating = newRating show.watched = newWatched db.session.commit() return redirect(url_for('index')) return render_template('show.html', show=show, reviews=reviews, form=form)
def blog_upload(): if not current_user.is_authenticated: return redirect(url_for('login')) form = PostForm() if form.validate_on_submit(): post = Post(body=form.post.data, author=current_user) db.session.add(post) db.session.commit() if request.files['fpost'].filename == '': print('no') else: fpost = request.files['fpost'] post_content = fpost.read().decode("utf-8") with open( "/scratch/ruhan/web/microblog/blogs/" + str(form.post.data) + ".txt", "a+") as fblog: fblog.write(post_content) flash('Your blog is now live!') #return redirect(url_for('blog')) posts = Post.query.order_by(Post.timestamp.desc()).all() return render_template('blog_upload.html', posts=posts, form=form)
def index(page=1): '''############################### Index page ################################## ''' #posts = g.user.followed_posts().all() posts = g.user.followed_posts().paginate(page, POSTS_PER_PAGE, False) return render_template('index.html', title='Home', form=PostForm(), posts=posts)
def post_update(post_id): post = Post.query.get_or_404(post_id) update_form = PostForm() if post.author.id != current_user.id: flash("You cannot update another users post!", 'danger') return redirect(url_for('myposts')) if request.method == 'POST' and update_form.validate(): post_title = update_form.title.data content = update_form.content.data post.title = post_title if content: post.content = content db.session.commit() flash("Your post has been updated", 'info') return redirect(url_for('post_detail', post_id=post.id)) return render_template('post_update.html', form=update_form, post=post)
def add_post(id): print("THIS IS THE POST FORM ROUTE") form = PostForm() # form = "hello!" print("THIS IS THE POST FORM", request.data) form['csrf_token'].data = request.cookies['csrf_token'] # if form.validate_on_submit(): post = Post(body=form.data["body"], party_id=id, user_id=current_user.id) print("THIS IS THE POST ITSELF ----------->", post) db.session.add(post) db.session.commit() return {"post": post.to_dict()}
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 = [ { 'author': {'username': '******'}, 'body': 'Rainy day in Miami!' }, { 'author': {'username': '******'}, 'body': 'I ate too much Guava Cheesecake' } ] 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('views.index')) page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate(page, 3, False) next_url = url_for('views.index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('views.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 )
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('Successfully posted!') return redirect(url_for('index')) page = request.args.get('page', default=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 add_post(topic_id, subtopic_id): session = create_session() if not session.query(SubTopic).filter( SubTopic.id == subtopic_id, SubTopic.topic_id == topic_id).first(): abort(404) form = PostForm() if form.validate_on_submit(): new_post = Post(title=form.title.data, description=form.description.data, author_id=current_user.id, lvl_access=form.lvl_access.data, subtopic_id=subtopic_id, published=True if current_user.role >= 1 else False) session.add(new_post) session.commit() return redirect( f'/topic/{topic_id}/subtopic/{subtopic_id}/post/{new_post.id}') return render_template('edit_post.html', title='Post creating', form=form, title_form='Create new post')
def index(): form = PostForm() if form.validate_on_submit(): entry = Entry(body=form.post.data, author=current_user) db.session.add(entry) db.session.commit() flash('Post has been published') return redirect(url_for('index')) page = request.args.get('page', 1, type=int) entries = current_user.followed_posts().paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('index', page=entries.next_num) \ if entries.has_next else None prev_url = url_for('index', page=entries.prev_num) \ if entries.has_prev else None return render_template('index.html', title='Index', form=form, entries=entries.items, next_url=next_url, prev_url=prev_url)
def update_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.commit() flash('Your post has been updated', 'success') return redirect(url_for('posts.post', post_id=post_id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content return render_template('create_post.html', title='Update Post', form=form, legend='Update Post')
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(): 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')) # user = {'username':'******'} # 포스트 뷰 및 페이지네이션&네비게이션 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="bright`s home", form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def post(): title="写文章" form = PostForm() if form.validate_on_submit(): #basepath = os.path.dirname(__file__) # 当前文件所在路径 #fileGet='uploads/assignment{}'.format(HOMEWORK_TIME) #upload_path = os.path.join(basepath,fileGet,secure_filename(fpy.filename)) savepic_path = 'app/static/assets/img/'+form.file.data.filename form.file.data.save(savepic_path) cate=Category.query.filter_by(name=dict(form.categories.choices).get(form.categories.data)).first_or_404() cate.number=cate.number+1 article=Article(title=form.title.data,body = form.body.data,create_time = datetime.now(),pic_path='static/assets/img/'+form.file.data.filename,category_id=category.id) db.session.add(article) db.session.commit() flash('上传成功!') return redirect(url_for('index')) #if request.method=='POST': # fpic=request.files['editormd-image-file'] # bodypic_path='app/static/pic/'+fpic.filename # fpic.save(bodypic_path) return render_template('/admin/post.html',title=title, form=form,category=category)
def index(): # 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("You post has been added.") 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.pre_num) if posts.has_prev else None return render_template('index.html', title='Home', form=form, posts=posts.items, nex_url=next_url, prev_url=prev_url)
def coment(postid): form = PostForm() postn = Post.query.filter_by(id=postid).first() if form.validate_on_submit(): post = Coments(body=form.post.data, author=current_user, coment=postn) db.session.add(post) db.session.commit() flash('Your coment is now live!') return redirect(url_for('coment', postid=postid)) page = request.args.get('page', 1, type=int) posts = postn.coments.order_by(Coments.timestamp.desc()).paginate( page, app.config['POSTS_PER_PAGE'], False) next_url = url_for('coments', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('coments', page=posts.prev_num) \ if posts.has_prev else None return render_template('coments.html', form=form, posts=posts.items, next_url=next_url, prev_url=prev_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('Your post is now live!') return redirect(url_for('index')) page = request.args.get('page', 1, type=int) posts = Post.query.order_by(Post.timestamp.desc()).paginate( page=page, per_page=app.config['POSTS_PER_PAGE']) 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(request): if request.method == "POST": post_pk = request.POST.get("post_id", 0) post_obj = Post.objects.filter(pk=post_pk).first() form = PostForm(request.POST, instance=post_obj) if form.is_valid(): post = form.save() return HttpResponseRedirect(reverse('post-view', args=(post.slug,))) else: form.add_error(None, forms.ValidationError( _(u"Кожне поле повинно бути заповненим!"))) elif request.method == "GET": post_pk = request.GET.get("post_id", 0) post_obj = Post.objects.filter(pk=post_pk).first() form = PostForm(instance=post_obj) context = { "latest_posts": Post.objects.all().order_by('-created')[:5], "form": form, "post_on_edit": (post_pk if request.GET.get("mark") else 0) } return render(request, "app/index.html", context)
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 add(request): if not request.user.is_authenticated(): return admin(request) if request.method == 'POST': if request.user.is_anonymous(): return redirect('/') form = PostForm(request.POST) if form.is_valid(): ftitle = form.cleaned_data['title'] ftext = form.cleaned_data['text'] newpost = Post(author=request.user, title=ftitle, text=ftext) newpost.save() return redirect('/') else: form = PostForm() return render_to_response("add.html", { 'form':form, }, context_instance = RequestContext(request) )