我们从Python开源项目中,提取了以下26个代码示例,用于说明如何使用flask.ext.login.current_user.can()。
def avatar(user_id): if current_user.id == user_id or current_user.can(Permission.UPDATE_OTHERS_INFORMATION): the_user = User.query.get_or_404(user_id) avatar_edit_form = AvatarEditForm() avatar_upload_form = AvatarUploadForm() if avatar_upload_form.validate_on_submit(): if 'avatar' in request.files: forder = str(user_id) avatar_name = avatars.save(avatar_upload_form.avatar.data, folder=forder) the_user.avatar = json.dumps({"use_out_url": False, "url": avatar_name}) db.session.add(the_user) db.session.commit() flash(u'??????!', 'success') return redirect(url_for('user.detail', user_id=user_id)) if avatar_edit_form.validate_on_submit(): the_user.avatar = json.dumps({"use_out_url": True, "url": avatar_edit_form.avatar_url.data}) db.session.add(the_user) db.session.commit() return redirect(url_for('user.detail', user_id=user_id)) return render_template('avatar_edit.html', user=the_user, avatar_edit_form=avatar_edit_form, avatar_upload_form=avatar_upload_form, title=u"????") else: abort(403)
def edit(user_id): if current_user.id == user_id or current_user.can(Permission.UPDATE_OTHERS_INFORMATION): the_user = User.query.get_or_404(user_id) form = EditProfileForm() if form.validate_on_submit(): the_user.name = form.name.data the_user.major = form.major.data the_user.headline = form.headline.data the_user.about_me = form.about_me.data db.session.add(the_user) db.session.commit() flash(u'??????!', "info") return redirect(url_for('user.detail', user_id=user_id)) form.name.data = the_user.name form.major.data = the_user.major form.headline.data = the_user.headline form.about_me.data = the_user.about_me return render_template('user_edit.html', form=form, user=the_user, title=u"????") else: abort(403)
def index(): search_word = request.args.get('search', None) search_form = SearchForm() page = request.args.get('page', 1, type=int) the_books = Book.query if not current_user.can(Permission.UPDATE_BOOK_INFORMATION): the_books = Book.query.filter_by(hidden=0) if search_word: search_word = search_word.strip() the_books = the_books.filter(db.or_( Book.title.ilike(u"%%%s%%" % search_word), Book.author.ilike(u"%%%s%%" % search_word), Book.isbn.ilike( u"%%%s%%" % search_word), Book.tags.any(Tag.name.ilike(u"%%%s%%" % search_word)), Book.subtitle.ilike( u"%%%s%%" % search_word))).outerjoin(Log).group_by(Book.id).order_by(db.func.count(Log.id).desc()) search_form.search.data = search_word else: the_books = Book.query.order_by(Book.id.desc()) pagination = the_books.paginate(page, per_page=8) result_books = pagination.items return render_template("book.html", books=result_books, pagination=pagination, search_form=search_form, title=u"????")
def tags(): search_tags = request.args.get('search', None) page = request.args.get('page', 1, type=int) the_tags = Tag.query.outerjoin(book_tag).group_by(book_tag.c.tag_id).order_by( db.func.count(book_tag.c.book_id).desc()).limit(30).all() search_form = SearchForm() search_form.search.data = search_tags data = None pagination = None if search_tags: tags_list = [s.strip() for s in search_tags.split(',') if len(s.strip()) > 0] if len(tags_list) > 0: the_books = Book.query if not current_user.can(Permission.UPDATE_BOOK_INFORMATION): the_books = Book.query.filter_by(hidden=0) the_books = the_books.filter( db.and_(*[Book.tags.any(Tag.name.ilike(word)) for word in tags_list])).outerjoin(Log).group_by( Book.id).order_by(db.func.count(Log.id).desc()) pagination = the_books.paginate(page, per_page=8) data = pagination.items return render_template('book_tag.html', tags=the_tags, title='Tags', search_form=search_form, books=data, 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 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 edit_post(id): post = Post.query.get_or_404(id) if current_user != post.author and not current_user.can(Permission.ADMINISTER): abort(403) form = PostForm() if form.validate_on_submit(): post.lable = Lable.query.get(form.lable.data) post.category = Category.query.get(form.category.data) post.title = form.title.data post.body = form.body.data post.update_last_stamp() db.session.add(post) flash('The post has been updated.') return redirect(url_for('.post', id=post.id)) form.title.data = post.title form.lable.data = post.lable_id form.category.data = post.category_id form.body.data = post.body return render_template('edit_post.html', form=form)
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 permission_required(permission): def decorator(f): @wraps(f) def decorated_function(*args, **kwargs): if not current_user.can(permission): abort(403) return f(*args, **kwargs) return decorated_function return decorator
def delete(comment_id): the_comment = Comment.query.get_or_404(comment_id) if current_user.id == the_comment.user_id or current_user.can(Permission.DELETE_OTHERS_COMMENT): the_comment.deleted = 1 book_id = the_comment.book_id db.session.add(the_comment) db.session.commit() flash(u'????????.', 'info') return redirect(request.args.get('next') or url_for('book.detail', book_id=book_id)) else: abort(403)
def edit(id): post = Post.query.get_or_404(id) if current_user != post.author and \ not current_user.can(Permission.ADMINISTER): abort(403) form = PostForm() if form.validate_on_submit(): post.body = form.body.data db.session.add(post) flash('The post has been updated.') return redirect(url_for('.post', id=post.id)) form.body.data = post.body return render_template('edit_post.html', form=form)
def permissions_required(permission): def decorator(f): @wraps(f) def decorated_function(*args, **kwargs): if not current_user.can(permission): abort(403) return f(*args, **kwargs) return decorated_function return decorator
def edit(id): question = Question.query.get_or_404(id) if current_user != question.author and not current_user.can(Permission.ADMINISTER): abort(403) form = QuestionForm() if form.validate_on_submit(): question.body = form.body.data question.qust = form.qust.data db.session.add(question) flash('The question has been update.') return redirect(url_for('.question',id=question.id)) form.body.data = question.body form.qust.data = question.qust return render_template('edit_post.html',form=form)
def delete(id): question = Question.query.get_or_404(id) if current_user != question.author and not current_user.can(Permission.ADMINISTER): abort(403) q = Question.query.filter_by(id=id).first() Potoca.query.filter_by(question_id=id).delete(synchronize_session=False) db.session.delete(q) return redirect(url_for('.index'))
def edit(id): post = Post.query.get_or_404(id) if current_user != post.author and \ not current_user.can(Permission.ADMINISTER): about(403) form = PostForm() if form.validate_on_submit(): post.body = form.body.data db.session.add(post) flash('The post has been updated.') return redirect(url_for('.post', id = post.id)) form.body.data = post.body return render_template('edit_post.html', form = form)
def 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 permission_required(permission): # f???????? def decorator(f): @wraps(f) # *args **kwargs?f??? def decorated_function(*args, **kwargs): # ???????????f???????? if not current_user.can(permission): abort(403) return f(*args, **kwargs) return decorated_function return decorator
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 edit(id): post = Post.query.get_or_404(id) #???????????????? if current_user != post.author and not current_user.can(Permission.ADMINISTER): abort(403) form = PostForm() if form.validate_on_submit(): post.body = form.body.data db.session.add(post) flash('The post has been updated!') return redirect(url_for('.post', id=post.id)) form.body.data = post.body return render_template('edit_post.html', form=form) #?????????
def permission_required(permission): def decorator(f): @wraps(f) def decorated_function(*args, **kwargs): if not current_user.can(permission): abort(403) return f(*args, **kwargs) return decorated_function return decorator #?????????
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)