我们从Python开源项目中,提取了以下32个代码示例,用于说明如何使用flask_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['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 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['CIRCULATE_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['CIRCULATE_COMMENTS_PER_PAGE'] + 1 pagination = post.comments.order_by(Comment.timestamp.asc()).paginate( page, per_page=current_app.config['CIRCULATE_COMMENTS_PER_PAGE'], error_out=False) comments = pagination.items return render_template('post.html', posts=[post], form=form, comments=comments, pagination=pagination)
def write(): form = PostForm() if current_user.operation(Permission.WRITE_ARTICLES) and \ form.validate_on_submit(): if 'save_draft' in request.form and form.validate(): post = Post(body=form.body.data, title=form.title.data, draft= True, author = current_user._get_current_object()) db.session.add(post) flash('?????') elif 'submit' in request.form and form.validate(): post = Post(body=form.body.data, title=form.title.data, author=current_user._get_current_object()) db.session.add(post) flash('?????') return redirect(url_for('user.write')) return render_template('user/write.html', form=form, post=form.body.data, title='???') # ??
def reply(id): comment = Comment.query.get_or_404(id) post = Post.query.get_or_404(comment.post_id) page = request.args.get('page', 1, type=int) form = ReplyForm() if form.validate_on_submit(): reply_comment = Comment(body=form.body.data, unread=True, post=post,comment_type='reply', reply_to=comment.author.nickname, author=current_user._get_current_object()) db.session.add(reply_comment) flash('?????????') return redirect(url_for('user.post', id=comment.post_id, page=page)) return render_template('user/reply.html', form=form, comment=comment, title='??') # ???? # ????????Comment???disabled??????Flase
def sentiments(): form = SentimentForm() if form.validate_on_submit(): sentiment = Sentiment(author=current_user._get_current_object(), language_id=form.language.data, body=form.body.data, score=form.score.data, timestamp=form.timestamp.data) db.session.add(sentiment) flash('Your sentiment has been published.', 'success') return redirect(url_for('.sentiments')) page = request.args.get('page', 1, type=int) pagination = Sentiment.query.order_by(Sentiment.timestamp.desc()).paginate( page, per_page=current_app.config['APP_ITEMS_PER_PAGE'], error_out=False ) sentiments_paginated = pagination.items return render_template('admin/sentiments.html', form=form, sentiments=sentiments_paginated, datetimepicker=datetime.utcnow(), pagination=pagination)
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() 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) db.session.commit() 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=showfollowed, 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) db.session.commit() 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['COMMENTS_PER_PAGE']+1 pagination = post.comments.order_by(Comment.timestamp.asc()).paginate( page,per_page=current_app.config['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',post=[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')) 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 page = request.args.get('page', 1, type=int) pagination = query.order_by(Post.timestamp.desc()).paginate( page, per_page=current_app.config['FLASKY2_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 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['FLASKY2_COMMENTS_PER_PAGE']+1 pagination = post.comments.order_by(Comment.timestamp.asc()).paginate( page, per_page=current_app.config['FLASKY2_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(title=form.title.data, category=Category.query.get(form.category.data), body=form.body.data, summury=form.summury.data, author=current_user._get_current_object()) db.session.add(post) return redirect(url_for('.index')) page = request.args.get('page', 1, type=int) pagination = Post.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)
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(u'????') 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', post=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()) # print current_user._get_current_object() # print type(current_user) 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.desc()).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 global_user(): g.user = current_user._get_current_object()
def post(id): post = Post.query.get_or_404(id) post.view_num += 1 db.session.add(post) # ?? form = CommentForm() if form.validate_on_submit(): comment = Comment(body=form.body.data, post=post, unread=True, author=current_user._get_current_object()) db.session.add(comment) flash('???????????') return redirect(url_for('user.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['COMMENTS_PER_PAGE'] + 1 pagination = post.comments.order_by(Comment.timestamp.asc()).paginate( page, per_page=current_app.config['COMMENTS_PER_PAGE'], error_out=False ) comments = pagination.items return render_template('user/post.html', posts=[post], title=post.title, id=post.id, post=post, form=form, comments=comments, pagination=pagination) # ??
def like(id): post = Post.query.get_or_404(id) if post.like_num.filter_by(liker_id=current_user.id).first() is not None: flash('??????') return redirect(url_for('user.post', id=post.id)) like = Like(post=post, unread=True, user=current_user._get_current_object()) db.session.add(like) flash('?????') return redirect(url_for('user.post', id=post.id)) # ???
def unlike(id): post = Post.query.get_or_404(id) if post.like_num.filter_by(liker_id=current_user.id).first() is None: flash('??????') return redirect(url_for('user.post', id=post.id)) # like = Like(post=post, # user=current_user._get_current_object()) else: f = post.like_num.filter_by(liker_id=current_user.id).first() db.session.delete(f) flash('??????') return redirect(url_for('user.post', id=post.id)) # ??????
def current_user(self): """ This method returns the currently logged user (``models.User``). We forward the flask_login current_user converted from a proxy object to our ``models.User``. We import the flask_login current_user inside the method to forbid a direct import from flask_login. Returns: models.User: currently logged user """ from flask_login import current_user return current_user._get_current_object()
def edit_profile(): form = EditProfileForm() if form.validate_on_submit(): current_user.name = form.name.data current_user.location = form.location.data current_user.about_me = form.about_me.data db.session.add(current_user._get_current_object()) db.session.commit() flash('Your profile has been updated.') return redirect(url_for('.user', username=current_user.username)) form.name.data = current_user.name form.location.data = current_user.location form.about_me.data = current_user.about_me return render_template('edit_profile.html', form=form)
def handle_request(self, lid, *args, **kwargs): league = League.query.filter_by(id=lid).first() if not league: abort(404) user = current_user._get_current_object() league.add_member(user) return redirect(url_for('league', lid=lid))
def handle_request(self, *args, **kwargs): user = current_user._get_current_object() leagues = user.get_leagues() leagues_created = user.created_leagues.all() return render_template('myleagues.html', leagues=leagues, leagues_created=leagues_created)
def add(): if request.args.get('notebook'): notebook = Notebook.query.filter_by( id=int(request.args.get('notebook'))).first() form = NoteForm(notebook=notebook.id) else: form = NoteForm() form.notebook.choices = [ (n.id, n.title) for n in Notebook.query.filter_by( author_id=current_user.id, is_deleted=False).all()] if form.validate_on_submit(): note = Note( title=form.title.data, body=form.body.data, notebook_id=form.notebook.data, author=current_user._get_current_object()) db.session.add(note) db.session.commit() tags = [] if not len(form.tags.data) == 0: for tag in form.tags.data.split(','): tags.append(tag.replace(" ", "")) note.str_tags = (tags) db.session.commit() return redirect(url_for('main.notebook', id=note.notebook_id)) return render_template('app/add.html', form=form)
def preprocess_request(self): g.user = current_user request.user = current_user._get_current_object() if request.method == 'GET': request.data = request.args.to_dict() else: request.data = request.json if request.data is None: request.data = request.form.to_dict()
def timetable(week=1): if not current_user.is_authenticated: return redirect(url_for('helper.jw_login', next=url_for('helper.timetable', week=week))) if not current_user.remember_me: try: res = current_user.spd.get_course() current_app.logger.info(res) if res ==[]: if current_user.wechat_id: return redirect(url_for('helper.jw_wechat_login', openid=current_user.wechat_id, next=url_for('helper.timetable', week=week))) else: return redirect(url_for('helper.jw_login', next=url_for('helper.timetable', week=week))) except: return redirect(url_for('helper.jw_wechat_login', openid=current_user.wechat_id, next=url_for('helper.timetable', week=week))) else: res = current_user.get_courses_from_database() if res == []: res = current_user.spd.get_course() if res == []: flash('??????') return redirect(url_for('helper.jw_wechat_login', openid=current_user.wechat_id, next=url_for('helper.get_wechat_timetable_start'))) current_user.save_courses_into_database(res, stu=current_user._get_current_object()) # res = current_user.spd.get_course() # res=[['??????????????????????????', '??', '?? ?5-7? 1-18? ??-314', '00000030A'], # ['???????', '??, ???, ??', '?? ?3-4? 1-14? ??-314\n?? ?3-4? 1-14? ??-314', '17010040'], # ['??????', '???, ???', '?? ?1-2? ?? ?502.506\n?? ?1-2? ?? ??-314\n?? ?3-4? 1-18? ??-314', '17010060'], # ['??????', '???, ???', '?? ?3-4? 1-18? ??-314\n?? ?1-2? ?? ?502.506\n?? ?1-2? ?? ??-314', '17010070'], # ['?????????', '???', '?? ?5-8? 1-15? ???\n?? ?1-4? 1-15? ???', '17010150'], ['123', '?', '?? ?9-11? 1-15? ???\n?? ?9-11? 1-15? ???', '17010150']] # random.shuffle(color_list) courses = current_user.spd.create_courses(week=week, course_result=res)[0] courses_except = current_user.spd.create_courses(week=week, course_result=res)[1] for r in res: r[2] = r[2].replace('\n', '<br>') color_list = ['#29B6F6', '#9CCC65', '#EC407A', '#FFA726', '#AB47BC', '#FF7043', '#7E57C2', '#26C6DA', '#66BB6A', '#8D6E63', '#42A5F5', '#BDBDBD', '#5C6BC0', '#26A69A', '#ef5350', '#D4E157'] return render_template('helper/course.html', courses=courses, week_now=week, color_list=color_list, msg=res, len=len, courses_except=courses_except)
def global_user(): # evaluate proxy value g.user = current_user._get_current_object()
def index(): # form = NameForm() # if form.validate_on_submit(): # user = User.query.filter_by(username=form.name.data).first() # if user is None: # user = User(username=form.name.data) # db.session.add(user) # session['known'] = False # if current_app.config['FLASKY_ADMIN']: # send_email(current_app.config['FLASKY_ADMIN'],'New User','mail/new_user',user=user) # # else: # session['known'] = True # session['name'] = form.name.data # return redirect(url_for('.index')) # return render_template('index.html',form = form ,name =session.get('name'),known = session.get('known',False)) 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()) # print type(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 print posts return render_template('index.html',form=form,posts=posts,show_followed=show_followed,pagination=pagination)