我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用flask.ext.login.current_user._get_current_object()。
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, show_followed=show_followed, pagination=pagination)
def post(id): post = Post.query.get_or_404(id) form = CommentForm() if form.validate_on_submit(): comment = Comment(body=form.body.data, post=post, author=current_user._get_current_object()) db.session.add(comment) flash('Your comment has been published.') return redirect(url_for('.post', id=post.id, page=-1)) page = request.args.get('page', 1, type=int) if page == -1: page = (post.comments.count() - 1) // \ current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1 pagination = post.comments.order_by(Comment.timestamp.asc()).paginate( page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'], error_out=False) comments = pagination.items return render_template('post.html', posts=[post], form=form, comments=comments, pagination=pagination)
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, show_followed = show_followed, pagination = pagination)
def post(id): post = Post.query.get_or_404(id) form = CommentForm() if form.validate_on_submit(): comment = Comment(body = form.body.data, post = post, author = current_user._get_current_object()) db.session.add(comment) flash('Your comment has been published.') return redirect(url_for('.post', id = post.id, page = -1)) page = request.args.get('page', 1, type = int) if page == -1: page = (post.comments.count() - 1) / \ current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1 pagination = post.comments.order_by(Comment.timestamp.asc()).paginate( page, per_page = current_app.config['FLASKY_COMMENTS_PER_PAGE'], error_out = False) comments = pagination.items return render_template('post.html', posts = [post], form = form, comments = comments, pagination = pagination)
def post(id): post = Post.query.get_or_404(id) # ???? form = CommentForm() if form.validate_on_submit(): comment = Comment(body=form.body.data, post=post, author=current_user._get_current_object()) db.session.add(comment) flash('Your comment has been published.') return redirect(url_for('.post', id=post.id, page=-1)) page = request.args.get('page', 1, type=int) if page == -1: page = (post.comments.count() - 1) // \ current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1 pagination = post.comments.order_by(Comment.timestamp.asc()).paginate( page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'], error_out=False) comments = pagination.items return render_template('post.html', posts=[post], form=form, comments=comments, pagination=pagination)
def index(): form = PostForm() if form.validate_on_submit() and current_user.can(Permission.WRITE_ARTICLES): #?????????????? post = Post(body=form.body.data, author=current_user._get_current_object()) #_get_current_object()??????? db.session.add(post) return redirect(url_for('.index')) #posts = Post.query.order_by(Post.timestamp.desc()).all() #???????????? 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, show_followed=show_followed, pagination=pagination) #?????????
def post(id): post = Post.query.get_or_404(id) form = CommentForm() if form.validate_on_submit(): comment = Comment(body=form.body.data, post=post, author=current_user._get_current_object()) db.session.add(comment) flash('Your comment has been published!') return redirect(url_for('.post',id=post.id, page=-1)) page = request.args.get('page', 1, type=int) if page == -1: page = (post.comments.count() - 1)//current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1 pagination = post.comments.order_by(Comment.timestamp.asc()).paginate(page, per_page=\ current_app.config['FLASKY_COMMENTS_PER_PAGE'], error_out=False) comments = pagination.items return render_template('post.html', posts=[post], form=form, comments=comments, pagination=pagination) #??????????
def index(): form = PostForm() recent_posts = Post.query.order_by(Post.timestamp.desc()).limit(10) recent_comments = Comment.query.order_by(Comment.timestamp.desc()).limit(10) categories = Category.query.all() if form.validate_on_submit(): title = (form.title.data).encode('UTF-8') body = (form.body.data).encode('UTF-8') category = Category.query.get(form.category.data) post = Post(title=title, body=body, category=category, author=current_user._get_current_object()) db.session.add(post) db.session.commit() flash('????!') return redirect(url_for('main.index')) page = request.args.get('page', 1, type=int) pagination = Post.query.order_by(Post.timestamp.desc()).paginate( page,per_page=int(os.environ.get('POST_PER_PAGE', 5)), error_out=False) posts = pagination.items return render_template('index.html', form=form, posts=posts, pagination=pagination, recent_posts=recent_posts, recent_comments=recent_comments, categories=categories, ) # ??
def post(id): post = Post.query.get_or_404(id) comments = Comment.query.order_by(Comment.timestamp.desc()) if current_user.is_authenticated: form = CommentForm() author = current_user._get_current_object() else: form = VisitorCommentForm() author = User(username=form.name.data) if form.validate_on_submit(): comment = Comment(body=form.body.data, post=post, author=author) db.session.add(comment) db.session.commit() flash('??????!') return redirect(url_for('.post', id=post.id, page=-1)) if post is None: abort(404) else: post.view_count += 1 db.session.add(post) db.session.commit() page = request.args.get('page', 1, type=int) if page == -1: page = (post.comments.count() - 1) / current_app.config['COMMENT_PER_PAGE'] + 1 pagination = post.comments.order_by(Comment.timestamp.asc()).paginate(page, per_page=current_app.config['COMMENT_PER_PAGE'], error_out=False) comments=pagination.items return render_template('post.html', post=post, form=form, comments=comments, pagination=pagination) # ????
def add_numbers(): a = request.args.get('catgr', 0, type=str) categories = Usertoca(author=current_user._get_current_object(), category_id=a) db.session.add(categories) catgr = Category.query.filter_by(id=a).first() return jsonify(result=catgr.name)
def question(id): question = Question.query.get_or_404(id) checkup = query = Vote.query.filter(Vote.author_id==current_user.id , Vote.question_id==id).first() minus = Vote.query.with_entities(Vote.disabled, db.func.count()).filter((Vote.disabled == 0)\ & (Vote.question_id == id)).all() plus = Vote.query.with_entities(Vote.disabled, db.func.count()).filter((Vote.disabled == 1)\ & (Vote.question_id == id)).all() question.votat = plus[0][1] - minus[0][1] db.session.add(question) form = CommentForm() if form.validate_on_submit(): if form.submit.data: comment = Comment(body=form.body.data, question=question, author=current_user._get_current_object()) db.session.add(comment) flash('Your comment has been published.') return redirect(url_for('.question', id=question.id, page=-1)) question.views += 1 db.session.add(question) tags = {} lines = Category.query.join(Potoca, Potoca.question_id == question.id)\ .filter( Category.id == Potoca.category_id) ls = [] for line in lines: ls.append(line.name) tags[question.id] = ls page = request.args.get('page', 1, type=int) if page == -1: page = (question.comments.count() - 1) // \ current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1 pagination = question.comments.order_by(Comment.timestamp.asc()).paginate( page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'], error_out=False) comments = pagination.items return render_template('question.html', questions=[question], form=form, comments=comments, pagination=pagination, tags=tags,state=True,checkup=checkup)
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) # ??followed?posts 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, show_followed=show_followed, pagination=pagination)
def new_post(): form = PostForm() if current_user.can(Permission.WRITE_ARTICLES) and \ form.validate_on_submit(): post = Post( title=form.title.data,category= Category.query.get(form.category.data), lable = Lable.query.get(form.lable.data), body = form.body.data, author = current_user._get_current_object()) db.session.add(post) return redirect(url_for('.index')) return render_template('new_post.html', form=form)
def index(): form = QuestionForm() if current_user.can(Permission.WRITE_ARTICLES) and \ form.validate_on_submit(): question = Question(body=form.body.data,qust=form.qust.data, author=current_user._get_current_object()) db.session.add(question) db.session.flush() s = form.categories.data categories = s.replace(' ','').split(',') for category in categories: cat = Category.query.filter_by(name=category).first() if cat is not None: tags = Potoca(question=question,category_id=cat.id) return redirect(url_for('.index')) page = request.args.get('page', 1, type=int) clist = [] for x in xrange(1,13): cat = Category.query.filter_by(id=x).first() clist.append(cat.name.encode("utf-8")) if current_user.is_authenticated: categori = Category.query.join(Usertoca, Category.id == Usertoca.category_id)\ .filter(Usertoca.author_id == current_user.id) else: categori = [] show_followed = 0 if current_user.is_authenticated: show_followed = str(request.cookies.get('show_followed', '')) if show_followed == '1': query = current_user.followed_cat elif show_followed == '2': query = current_user.followed_question elif show_followed == '3': query = Question.query.outerjoin(Comment, Question.id == Comment.question_id)\ .filter(Comment.question_id == None) else: query = Question.query pagination = query.order_by(Question.timestamp.desc()).paginate( page,per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],error_out=False) questions = pagination.items tags = {} for x in questions: lines = Category.query.join(Potoca, Potoca.question_id == x.id)\ .filter( Category.id == Potoca.category_id) ls = [] for line in lines: ls.append(line.name) tags[x.id] = ls return render_template('index.html',form=form,questions=questions,\ show_followed=show_followed,pagination=pagination,categori=categori,tags=tags,clist=clist,state=False)