本文整理汇总了Python中r2.models.Subreddit.load_subreddits方法的典型用法代码示例。如果您正苦于以下问题:Python Subreddit.load_subreddits方法的具体用法?Python Subreddit.load_subreddits怎么用?Python Subreddit.load_subreddits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类r2.models.Subreddit
的用法示例。
在下文中一共展示了Subreddit.load_subreddits方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wrap_items
# 需要导入模块: from r2.models import Subreddit [as 别名]
# 或者: from r2.models.Subreddit import load_subreddits [as 别名]
def wrap_items(self, items):
from r2.lib.db import queries
from r2.lib.template_helpers import add_attr
user = c.user if c.user_is_loggedin else None
aids = set(l.author_id for l in items if hasattr(l, 'author_id')
and l.author_id is not None)
authors = Account._byID(aids, data=True, stale=self.stale)
now = datetime.datetime.now(g.tz)
cakes = {a._id for a in authors.itervalues()
if a.cake_expiration and a.cake_expiration >= now}
friend_rels = user.friend_rels() if user and user.gold else {}
subreddits = Subreddit.load_subreddits(items, stale=self.stale)
can_ban_set = set()
can_flair_set = set()
can_own_flair_set = set()
if user:
for sr_id, sr in subreddits.iteritems():
if sr.can_ban(user):
can_ban_set.add(sr_id)
if sr.is_moderator_with_perms(user, 'flair'):
can_flair_set.add(sr_id)
if sr.link_flair_self_assign_enabled:
can_own_flair_set.add(sr_id)
#get likes/dislikes
try:
likes = queries.get_likes(user, items)
except tdb_cassandra.TRANSIENT_EXCEPTIONS as e:
g.log.warning("Cassandra vote lookup failed: %r", e)
likes = {}
types = {}
wrapped = []
modlink = {}
modlabel = {}
for s in subreddits.values():
modlink[s._id] = '/r/%s/about/moderators' % s.name
modlabel[s._id] = (_('moderator of /r/%(reddit)s, '
'speaking officially') % {'reddit': s.name})
for item in items:
w = self.wrap(item)
wrapped.append(w)
# add for caching (plus it should be bad form to use _
# variables in templates)
w.fullname = item._fullname
types.setdefault(w.render_class, []).append(w)
w.author = None
w.friend = False
# List of tuples (see add_attr() for details)
w.attribs = []
w.distinguished = None
if hasattr(item, "distinguished"):
if item.distinguished == 'yes':
w.distinguished = 'moderator'
elif item.distinguished in ('admin', 'special',
'gold', 'gold-auto'):
w.distinguished = item.distinguished
try:
w.author = authors.get(item.author_id)
if user and item.author_id in user.friends:
# deprecated old way:
w.friend = True
# new way:
label = None
if friend_rels:
rel = friend_rels[item.author_id]
note = getattr(rel, "note", None)
if note:
label = u"%s (%s)" % (_("friend"),
_force_unicode(note))
add_attr(w.attribs, 'F', label)
except AttributeError:
pass
if (w.distinguished == 'admin' and w.author):
add_attr(w.attribs, 'A')
if w.distinguished == 'moderator':
add_attr(w.attribs, 'M', label=modlabel[item.sr_id],
link=modlink[item.sr_id])
if w.distinguished == 'special':
args = w.author.special_distinguish()
args.pop('name')
if not args.get('kind'):
args['kind'] = 'special'
add_attr(w.attribs, **args)
if w.author and w.author._id in cakes and not c.profilepage:
#.........这里部分代码省略.........
示例2: wrap_items
# 需要导入模块: from r2.models import Subreddit [as 别名]
# 或者: from r2.models.Subreddit import load_subreddits [as 别名]
def wrap_items(self, items):
from r2.lib.db import queries
from r2.lib.template_helpers import (
add_friend_distinguish,
add_admin_distinguish,
add_moderator_distinguish,
add_cakeday_distinguish,
add_special_distinguish,
)
user = c.user if c.user_is_loggedin else None
aids = set(l.author_id for l in items if hasattr(l, "author_id") and l.author_id is not None)
authors = Account._byID(aids, data=True, stale=self.stale)
now = datetime.datetime.now(g.tz)
cakes = {a._id for a in authors.itervalues() if a.cake_expiration and a.cake_expiration >= now}
friend_rels = user.friend_rels() if user and user.gold else {}
subreddits = Subreddit.load_subreddits(items, stale=self.stale)
can_ban_set = set()
if user:
for sr_id, sr in subreddits.iteritems():
if sr.can_ban(user):
can_ban_set.add(sr_id)
# get likes/dislikes
try:
likes = queries.get_likes(user, items)
except tdb_cassandra.TRANSIENT_EXCEPTIONS as e:
g.log.warning("Cassandra vote lookup failed: %r", e)
likes = {}
types = {}
wrapped = []
for item in items:
w = self.wrap(item)
wrapped.append(w)
# add for caching (plus it should be bad form to use _
# variables in templates)
w.fullname = item._fullname
types.setdefault(w.render_class, []).append(w)
w.author = None
w.friend = False
w.distinguished = None
if hasattr(item, "distinguished"):
if item.distinguished == "yes":
w.distinguished = "moderator"
elif item.distinguished in ("admin", "special", "gold", "gold-auto"):
w.distinguished = item.distinguished
if getattr(item, "author_id", None):
w.author = authors.get(item.author_id)
if hasattr(item, "sr_id") and item.sr_id is not None:
w.subreddit = subreddits[item.sr_id]
distinguish_attribs_list = []
# if display_author exists, then w.author is unknown to the
# receiver, so we can't check for friend or cakeday
author_is_hidden = hasattr(item, "display_author")
if not author_is_hidden and user and w.author and w.author._id in user.friends:
w.friend = True # TODO: deprecated?
if item.author_id in friend_rels:
note = getattr(friend_rels[w.author._id], "note", None)
else:
note = None
add_friend_distinguish(distinguish_attribs_list, note)
if w.distinguished == "admin" and w.author:
add_admin_distinguish(distinguish_attribs_list)
if w.distinguished == "moderator":
add_moderator_distinguish(distinguish_attribs_list, w.subreddit)
if w.distinguished == "special":
add_special_distinguish(distinguish_attribs_list, w.author)
if not author_is_hidden and w.author and w.author._id in cakes and not c.profilepage:
add_cakeday_distinguish(distinguish_attribs_list, w.author)
w.attribs = distinguish_attribs_list
user_vote_dir = likes.get((user, item))
if user_vote_dir == Vote.DIRECTIONS.up:
w.likes = True
elif user_vote_dir == Vote.DIRECTIONS.down:
w.likes = False
else:
w.likes = None
w.upvotes = item._ups
w.downvotes = item._downs
#.........这里部分代码省略.........