本文整理汇总了Python中models.Article.find_by_pk方法的典型用法代码示例。如果您正苦于以下问题:Python Article.find_by_pk方法的具体用法?Python Article.find_by_pk怎么用?Python Article.find_by_pk使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Article
的用法示例。
在下文中一共展示了Article.find_by_pk方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_article
# 需要导入模块: from models import Article [as 别名]
# 或者: from models.Article import find_by_pk [as 别名]
def get_article(id, user=None):
"""Gets a single article (formatted by json_data) from redis or postgresql
And performs some pre-filters for the current user
Args:
id (int): id
user (User): the current user
Returns:
None if the article is not existed or a dict of article data
"""
id = int(id)
key = Article.cache_key(id)
redis_client = get_client(key)
article = None
if not redis_client.exists(key):
article = Article.find_by_pk(id)
if article:
data = article.json_data()
redis_client.set(key, json.dumps(data))
article = data
else:
if redis_client.type(key) == 'hash':
print redis_client
article = json.loads(redis_client.get(key))
article['user_vote'] = Article.check_vote(user, article['id'])
return article
示例2: update_article
# 需要导入模块: from models import Article [as 别名]
# 或者: from models.Article import find_by_pk [as 别名]
def update_article(id, article=None):
"""Updates the cache of article :id
Args:
id (int): article id
article (Article): if not provided, an article will be queried from postgresql
Returns:
Article
"""
id = int(id)
if not article:
article = Article.find_by_pk(id)
if article:
key = Article.cache_key(id)
redis_client = get_client(key)
data = article.json_data()
redis_client.set(key, json.dumps(data))
return article
示例3: decorated_function
# 需要导入模块: from models import Article [as 别名]
# 或者: from models.Article import find_by_pk [as 别名]
def decorated_function(*args, **kwargs):
data = request.get_json()
# support both json and normal request
if not data:
data = request.args
# now kiss
data = dict(request.args.items() + kwargs.items())
article_id = data.get('id')
article_id = data.get('article_id', article_id)
article = Article.find_by_pk(article_id)
if not article:
if json:
return json_error_invalid_request()
else:
return abort(400)
g.article = article
return f(*args, **kwargs)