本文整理汇总了Python中models.Category.get_by_id方法的典型用法代码示例。如果您正苦于以下问题:Python Category.get_by_id方法的具体用法?Python Category.get_by_id怎么用?Python Category.get_by_id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Category
的用法示例。
在下文中一共展示了Category.get_by_id方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: put
# 需要导入模块: from models import Category [as 别名]
# 或者: from models.Category import get_by_id [as 别名]
def put(self, id):
category = Category.get_by_id(id)
if category is None:
flash(gettext('The category was not found'), 'error')
return redirect(url_for('CategoriesView:index'))
if not category.can_edit():
abort(401)
if request.method in ['POST', 'PUT']:
form = CategoryForm()
if form.validate_on_submit():
try:
form.populate_obj(category)
category.save()
flash(gettext('Category was succesfully saved'))
return util.redirect_json_or_html(url_for('CategoriesView:index'), 'category')
except:
return util.redirect_json_or_html(url_for('CategoriesView:index'), 'category', gettext('Error while updating the category'))
else:
if request.is_xhr:
return util.redirect_json_or_html(url_for('CategoriesView:index'), 'category', gettext('Invalid submission, please check the message below'))
else:
flash(gettext('Invalid submission, please check the message below'), 'error')
else:
form = NewCategoryForm(category)
return render_template('admin/categories/edit.html',
title=gettext('Edit Category: %(name)s', name=category.name),
form=form,
category=category)
示例2: transfer_post
# 需要导入模块: from models import Category [as 别名]
# 或者: from models.Category import get_by_id [as 别名]
def transfer_post(self):
trans = TranferCatForm()
if trans.validate_on_submit():
cat_from = Category.get_by_id(trans.from_id.data)
cat_to = Category.get_by_id(trans.to_id.data)
if cat_from and cat_to:
Category.transfer_posts(cat_from, cat_to)
flash(gettext('The posts were transfered from %(from_name)s to %(to_name)s',
from_name=cat_from.name, to_name=cat_to.name))
else:
flash(gettext('Either category was not found'), 'error')
else:
flash(trans.get_errors(), 'error')
return redirect(url_for('CategoriesView:index'))
示例3: list_category_post
# 需要导入模块: from models import Category [as 别名]
# 或者: from models.Category import get_by_id [as 别名]
def list_category_post(request, category_id):
category = Category.get_by_id(int(category_id))
if not category:
raise Http404
posts = Post.all().filter('category', category).order('-create_time')
return object_list(request, queryset=posts, allow_empty=True,
template_name='list_category_post.html', extra_context={'is_author': is_author(), 'category': category},
paginate_by=settings.POST_LIST_PAGE_SIZE)
示例4: question
# 需要导入模块: from models import Category [as 别名]
# 或者: from models.Category import get_by_id [as 别名]
def question(request, qid, **kwargs):
session_data = {}
if local.request.session['uid']:
session_data['user_name'] = User.get_by_id(local.request.session['uid'])[0].name
try:
question = Question.get_by_id(qid)[0]
except TypeError:
return not_found(request)
question.views += 1
question.update()
edit = question.latest_edit()[0]
category = Category.get_by_id(question.category_id)[0]
user = User.get_by_id(question.user_id)[0]
question_data = {
'title' : str(edit.title),
'category' : str(category.name),
'votes' : str(question.votes),
'author' : str(user.name),
'author_id' : str(user.id),
'avatar' : str(user.avatar),
'views' : str(question.views),
'created' : str(question.created),
'body' : str(edit.body),
}
try:
answers_list = Answer.get(where=('question_id', qid), order=('votes', 'desc'))
answer_data_list = []
for answer in answers_list:
answer_user = User.get_by_id(answer.user_id)[0]
answer_edit = AnswerEdit.get(where=('answer_id', answer.id))[0]
answer_data = {
'votes' : str(answer.votes),
'author' : str(answer_user.name),
'author_id' : str(answer_user.id),
'avatar' : str(answer_user.avatar),
'body' : str(answer_edit.body),
}
answer_data_list.append(answer_data)
except TypeError:
answer_data_list = False
page = Page(session_data)
page.title = str(edit.title) + ' - Meno'
content = Thread(question_data, answer_data_list)
local.request.session['last'] = request.base_url
return respond(page.render(content))
示例5: questions
# 需要导入模块: from models import Category [as 别名]
# 或者: from models.Category import get_by_id [as 别名]
def questions(request, **kwargs):
page_data = []
session_data = {}
if local.request.session['uid']:
session_data['user_name'] = User.get_by_id(local.request.session['uid'])[0].name
page = Page(session_data)
if 'search' in request.args:
questions_list = Question.search(request.args['search'])
page.title = "Questions - '%s' - Meno" % request.args['search']
if 'sort' in request.args:
sorts = {
'new': 'date_created',
}
sort_attr = sorts[request.args['sort']]
questions_list = Question.get(order=(sort_attr, 'desc'), limit=30)
else:
page.title = 'Questions - Meno'
questions_list = Question.get_latest(30)
for question in questions_list:
edit = question.latest_edit()[0]
user = User.get_by_id(question.user_id)[0]
age = question.age()
stat = question.latest_status()[0]
question_data = {
'question_id': str(question.id),
'user_id': str(question.user_id),
'views': str(question.views),
'votes': str(question.votes),
'date_created': str(question.created),
'category': str(Category.get_by_id(question.category_id)[0].name),
'answers_count': str(count(question.answers())),
'title': str(edit.title),
'user': str(user.name),
'status': str(stat.status),
'age': str("Asked %sh %sm %ss ago" % (age[0], age[0], age[1])),
}
page_data.append(question_data)
content = QuestionsList(page_data)
local.request.session['last'] = request.base_url
return respond(page.render(content))
示例6: delete
# 需要导入模块: from models import Category [as 别名]
# 或者: from models.Category import get_by_id [as 别名]
def delete(self, id):
category = Category.get_by_id(id)
if category is None:
flash(gettext('The category was not found'), 'error')
return redirect(url_for('CategoriesView:index'))
if not category.can_edit():
abort(401)
try:
if not Category.transfer_posts(category):
return util.redirect_json_or_html(url_for('CategoriesView:index'),
'category',
gettext('Sorry, the last category can not be removed'))
name = category.name
Category.delete(category.id)
flash(gettext('The category "%(name)s" was removed', name=name))
except:
return util.redirect_json_or_html(url_for('CategoriesView:index'),
'category',
gettext('Error while removing the category'))
return util.redirect_json_or_html(url_for('CategoriesView:index'), 'category')