本文整理汇总了Python中db.Share.by_slug方法的典型用法代码示例。如果您正苦于以下问题:Python Share.by_slug方法的具体用法?Python Share.by_slug怎么用?Python Share.by_slug使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类db.Share
的用法示例。
在下文中一共展示了Share.by_slug方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_share_by_slug
# 需要导入模块: from db import Share [as 别名]
# 或者: from db.Share import by_slug [as 别名]
def get_share_by_slug(slug):
# 特殊id ramdom
if slug == 'random':
cond = {}
cond['status'] = {'$gte': 1}
# shares = Share.find(cond, {'_id': 0})
shares = Share.find(cond)
share = random.choice(list(shares))
elif slug.isdigit():
share = Share.by_sid(slug)
else:
share = Share.by_slug(slug)
if share:
share.hitnum += 1
if isinstance(share.tags, str):
share.tags = share.tags.split()
share.save()
share.pop('_id')
return share
示例2: get
# 需要导入模块: from db import Share [as 别名]
# 或者: from db.Share import by_slug [as 别名]
def get(self, slug):
share = None
if slug.isdigit():
share = Share.by_sid(slug)
else:
share = Share.by_slug(slug)
if share:
share.hitnum += 1
share.save()
share.markdown = markdown2.markdown(share.markdown)
user = User.by_sid(share.user_id)
share.user_name = user.user_name
share.user_domain = user.user_domain
tags = ''
if share.tags:
tags += 'tags:'
for i in share.tags.split(' '):
tags += '<a href="/tag/%s">%s</a> ' % (i, i)
share.tags = tags
user_id = int(
self.current_user["user_id"]) if self.current_user else None
like = Like.find_one(
{'share_id': share.id, 'user_id': user_id})
share.is_liking = bool(like.likenum % 2) if like else None
share.is_disliking = bool(like.dislikenum % 2) if like else None
comments = []
comment_res = Comment.find({'share_id': share.id})
for comment in comment_res:
user = User.by_sid(comment.user_id)
comment.name = user.user_name
comment.domain = user.user_domain
comment.gravatar = get_avatar(user.user_email, 50)
comments.append(comment)
if user_id:
hit = Hit.find(
{'share_id': share.id},
{'user_id': int(self.current_user["user_id"])},
)
if hit.count() == 0:
hit = Hit
hit['share_id'] = share.id
hit['user_id'] = int(self.current_user["user_id"])
hit.save()
else:
if not self.get_cookie(share.id):
self.set_cookie(str(share.id), "1")
posts = Share.find()
suggest = []
for post in posts:
post.score = 100 + post.id - post.user_id + post.commentnum * 3
post.score += post.likenum * 4 + post.hitnum * 0.01
post.score += randint(1, 999) * 0.001
common_tags = [i for i in post.tags.split(
' ') if i in share.tags.split(' ')]
# list(set(b1) & set(b2))
post.score += len(common_tags)
if post.sharetype == share.sharetype:
post.score += 5
if self.current_user:
is_hitted = Hit.find(
{'share_id': share._id},
{'user_id': int(self.current_user["user_id"])},
).count() > 0
else:
is_hitted = self.get_cookie(share.id)
if is_hitted:
post.score -= 50
suggest.append(post)
suggest.sort(key=lambda obj: obj.get('score'))
suggest = suggest[:5]
self.render(
"sharee.html", share=share, comments=comments,
suggest=suggest)
else:
old = 'http://blog.anwensf.com/'
for i in options.old_links:
if slug in i:
self.redirect('%s%s' % (old, i), permanent=True)
break
return
self.redirect("/404")