本文整理汇总了Python中r2.lib.template_helpers.add_attr函数的典型用法代码示例。如果您正苦于以下问题:Python add_attr函数的具体用法?Python add_attr怎么用?Python add_attr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了add_attr函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: add_props
def add_props(cls, user, wrapped):
from r2.lib.template_helpers import add_attr
from r2.lib import promote
from r2.lib.wrapped import CachedVariable
# fetch parent links
links = Link._byID(set(l.link_id for l in wrapped), data=True, return_dict=True, stale=True)
# get srs for comments that don't have them (old comments)
for cm in wrapped:
if not hasattr(cm, "sr_id"):
cm.sr_id = links[cm.link_id].sr_id
subreddits = Subreddit._byID(set(cm.sr_id for cm in wrapped), data=True, return_dict=False, stale=True)
cids = dict((w._id, w) for w in wrapped)
parent_ids = set(cm.parent_id for cm in wrapped if getattr(cm, "parent_id", None) and cm.parent_id not in cids)
parents = {}
if parent_ids:
parents = Comment._byID(parent_ids, data=True, stale=True)
can_reply_srs = set(s._id for s in subreddits if s.can_comment(user)) if c.user_is_loggedin else set()
can_reply_srs.add(promote.get_promote_srid())
min_score = user.pref_min_comment_score
profilepage = c.profilepage
user_is_admin = c.user_is_admin
user_is_loggedin = c.user_is_loggedin
focal_comment = c.focal_comment
for item in wrapped:
# for caching:
item.profilepage = c.profilepage
item.link = links.get(item.link_id)
if item.link._score <= 1 or item.score < 3 or item.link._spam or item._spam or item.author._spam:
item.nofollow = True
else:
item.nofollow = False
if not hasattr(item, "subreddit"):
item.subreddit = item.subreddit_slow
if item.author_id == item.link.author_id and not item.link._deleted:
add_attr(item.attribs, "S", link=item.link.make_permalink(item.subreddit))
if not hasattr(item, "target"):
item.target = None
if item.parent_id:
if item.parent_id in cids:
item.parent_permalink = "#" + utils.to36(item.parent_id)
else:
parent = parents[item.parent_id]
item.parent_permalink = parent.make_permalink(item.link, item.subreddit)
else:
item.parent_permalink = None
item.can_reply = False
if c.can_reply or (item.sr_id in can_reply_srs):
age = c.start_time - item._date
if age.days < g.REPLY_AGE_LIMIT:
item.can_reply = True
# not deleted on profile pages,
# deleted if spam and not author or admin
item.deleted = not profilepage and (
item._deleted or (item._spam and item.author != user and not item.show_spam)
)
extra_css = ""
if item.deleted:
extra_css += "grayed"
if not user_is_admin:
item.author = DeletedUser()
item.body = "[deleted]"
if focal_comment == item._id36:
extra_css += " border"
# don't collapse for admins, on profile pages, or if deleted
item.collapsed = (item.score < min_score) and not (profilepage or item.deleted or user_is_admin)
item.editted = getattr(item, "editted", False)
item.render_css_class = "comment %s" % CachedVariable("time_period")
# will get updated in builder
item.num_children = 0
item.score_fmt = Score.points
item.permalink = item.make_permalink(item.link, item.subreddit)
item.is_author = user == item.author
item.is_focal = focal_comment == item._id36
# will seem less horrible when add_props is in pages.py
from r2.lib.pages import UserText
item.usertext = UserText(
item,
item.body,
editable=item.is_author,
nofollow=item.nofollow,
#.........这里部分代码省略.........
开发者ID:ketralnis,项目名称:reddit,代码行数:101,代码来源:link.py
示例2: add_props
def add_props(cls, user, wrapped):
from r2.lib.template_helpers import add_attr
from r2.lib import promote
#fetch parent links
links = Link._byID(set(l.link_id for l in wrapped), data = True,
return_dict = True)
#get srs for comments that don't have them (old comments)
for cm in wrapped:
if not hasattr(cm, 'sr_id'):
cm.sr_id = links[cm.link_id].sr_id
subreddits = Subreddit._byID(set(cm.sr_id for cm in wrapped),
data=True,return_dict=False)
can_reply_srs = set(s._id for s in subreddits if s.can_comment(user)) \
if c.user_is_loggedin else set()
can_reply_srs.add(promote.PromoteSR._id)
min_score = user.pref_min_comment_score
cids = dict((w._id, w) for w in wrapped)
profilepage = c.profilepage
user_is_admin = c.user_is_admin
user_is_loggedin = c.user_is_loggedin
focal_comment = c.focal_comment
for item in wrapped:
# for caching:
item.profilepage = c.profilepage
item.link = links.get(item.link_id)
if (item.link._score <= 1 or item.score < 3 or
item.link._spam or item._spam or item.author._spam):
item.nofollow = True
else:
item.nofollow = False
if not hasattr(item, 'subreddit'):
item.subreddit = item.subreddit_slow
if item.author_id == item.link.author_id:
add_attr(item.attribs, 'S',
link = item.link.make_permalink(item.subreddit))
if not hasattr(item, 'target'):
item.target = None
if hasattr(item, 'parent_id'):
if cids.has_key(item.parent_id):
item.parent_permalink = '#' + utils.to36(item.parent_id)
else:
parent = Comment._byID(item.parent_id)
item.parent_permalink = parent.make_permalink(item.link, item.subreddit)
else:
item.parent_permalink = None
item.can_reply = (item.sr_id in can_reply_srs)
# not deleted on profile pages,
# deleted if spam and not author or admin
item.deleted = (not profilepage and
(item._deleted or
(item._spam and
item.author != user and
not item.show_spam)))
extra_css = ''
if item.deleted:
extra_css += "grayed"
if not user_is_admin:
item.author = DeletedUser()
item.body = '[deleted]'
if focal_comment == item._id36:
extra_css += " border"
# don't collapse for admins, on profile pages, or if deleted
item.collapsed = ((item.score < min_score) and
not (profilepage or
item.deleted or
user_is_admin))
item.editted = getattr(item, "editted", False)
#will get updated in builder
item.num_children = 0
item.score_fmt = Score.points
item.permalink = item.make_permalink(item.link, item.subreddit)
item.is_author = (user == item.author)
item.is_focal = (focal_comment == item._id36)
#will seem less horrible when add_props is in pages.py
from r2.lib.pages import UserText
item.usertext = UserText(item, item.body,
#.........这里部分代码省略.........
开发者ID:DFectuoso,项目名称:culter,代码行数:101,代码来源:link.py
示例3: wrap_items
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:
#.........这里部分代码省略.........
开发者ID:Acceto,项目名称:reddit,代码行数:101,代码来源:builder.py
示例4: wrap_items
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
#get authors
#TODO pull the author stuff into add_props for links and
#comments and messages?
aids = set(l.author_id for l in items if hasattr(l, 'author_id')
and l.author_id is not None)
authors = {}
cup_infos = {}
email_attrses = {}
friend_rels = None
if aids:
authors = Account._byID(aids, data=True, stale=self.stale) if aids else {}
cup_infos = Account.cup_info_multi(aids)
if c.user_is_admin:
email_attrses = admintools.email_attrs(aids, return_dict=True)
if user and user.gold:
friend_rels = user.friend_rels()
subreddits = Subreddit.load_subreddits(items, stale=self.stale)
if not user:
can_ban_set = set()
else:
can_ban_set = set(id for (id,sr) in subreddits.iteritems()
if sr.can_ban(user))
#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 = {}
uid = user._id if user else None
types = {}
wrapped = []
count = 0
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') %
dict(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)
#TODO pull the author stuff into add_props for links and
#comments and messages?
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'):
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])
#.........这里部分代码省略.........
开发者ID:Anenome,项目名称:reddit,代码行数:101,代码来源:builder.py
示例5: add_props
def add_props(cls, user, wrapped):
from r2.lib.template_helpers import add_attr, get_domain
from r2.lib import promote
from r2.lib.wrapped import CachedVariable
from r2.lib.pages import WrappedUser
#fetch parent links
links = Link._byID(set(l.link_id for l in wrapped), data = True,
return_dict = True, stale=True)
# fetch authors
authors = Account._byID(set(l.author_id for l in links.values()), data=True,
return_dict=True, stale=True)
#get srs for comments that don't have them (old comments)
for cm in wrapped:
if not hasattr(cm, 'sr_id'):
cm.sr_id = links[cm.link_id].sr_id
subreddits = Subreddit._byID(set(cm.sr_id for cm in wrapped),
data=True, return_dict=False, stale=True)
cids = dict((w._id, w) for w in wrapped)
parent_ids = set(cm.parent_id for cm in wrapped
if getattr(cm, 'parent_id', None)
and cm.parent_id not in cids)
parents = {}
if parent_ids:
parents = Comment._byID(parent_ids, data=True, stale=True)
can_reply_srs = set(s._id for s in subreddits if s.can_comment(user)) \
if c.user_is_loggedin else set()
can_reply_srs.add(promote.get_promote_srid())
min_score = user.pref_min_comment_score
profilepage = c.profilepage
user_is_admin = c.user_is_admin
user_is_loggedin = c.user_is_loggedin
focal_comment = c.focal_comment
cname = c.cname
site = c.site
for item in wrapped:
# for caching:
item.profilepage = c.profilepage
item.link = links.get(item.link_id)
if (item.link._score <= 1 or item.score < 3 or
item.link._spam or item._spam or item.author._spam):
item.nofollow = True
else:
item.nofollow = False
if not hasattr(item, 'subreddit'):
item.subreddit = item.subreddit_slow
if item.author_id == item.link.author_id and not item.link._deleted:
add_attr(item.attribs, 'S',
link = item.link.make_permalink(item.subreddit))
if not hasattr(item, 'target'):
item.target = "_top" if cname else None
if item.parent_id:
if item.parent_id in cids:
item.parent_permalink = '#' + utils.to36(item.parent_id)
else:
parent = parents[item.parent_id]
item.parent_permalink = parent.make_permalink(item.link, item.subreddit)
else:
item.parent_permalink = None
item.can_reply = False
if c.can_reply or (item.sr_id in can_reply_srs):
age = datetime.now(g.tz) - item._date
if age.days < g.REPLY_AGE_LIMIT:
item.can_reply = True
# not deleted on profile pages,
# deleted if spam and not author or admin
item.deleted = (not profilepage and
(item._deleted or
(item._spam and
item.author != user and
not item.show_spam)))
extra_css = ''
if item.deleted:
extra_css += "grayed"
if not user_is_admin:
item.author = DeletedUser()
item.body = '[deleted]'
if focal_comment == item._id36:
extra_css += " border"
if profilepage:
item.link_author = WrappedUser(authors[item.link.author_id])
item.subreddit_path = item.subreddit.path
if cname:
#.........这里部分代码省略.........
开发者ID:3river,项目名称:reddit,代码行数:101,代码来源:link.py
示例6: wrap_items
def wrap_items(self, items):
from r2.lib.template_helpers import add_attr
user = c.user if c.user_is_loggedin else None
#get authors
#TODO pull the author stuff into add_props for links and
#comments and messages?
try:
aids = set(l.author_id for l in items)
except AttributeError:
aids = None
authors = Account._byID(aids, True) if aids else {}
# srids = set(l.sr_id for l in items if hasattr(l, "sr_id"))
subdiggs = Subreddit.load_subreddits(items)
if not user:
can_ban_set = set()
else:
can_ban_set = set(id for (id,sr) in subdiggs.iteritems()
if sr.can_ban(user))
#get likes/dislikes
#TODO Vote.likes should accept empty lists
likes = Vote.likes(user, items) if user and items else {}
uid = user._id if user else None
types = {}
wrapped = []
count = 0
if isinstance(c.site, FakeSubdigg):
mods = []
else:
mods = c.site.moderators
modlink = ''
if c.cname:
modlink = '/about/moderators'
else:
modlink = '/r/%s/about/moderators' % c.site.name
modlabel = (_('moderator of /r/%(digg)s, speaking officially') %
dict(digg = c.site.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)
#TODO pull the author stuff into add_props for links and
#comments and messages?
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 == 'admin':
w.distinguished = 'admin'
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:
add_attr(w.attribs, 'F')
except AttributeError:
pass
if (w.distinguished == 'admin' and
w.author and w.author.name in g.admins):
add_attr(w.attribs, 'A')
if (w.distinguished == 'moderator' and
getattr(item, "author_id", None) in mods):
add_attr(w.attribs, 'M', label=modlabel, link=modlink)
if (g.show_awards and w.author
and w.author.should_show_cup()):
add_attr(w.attribs, 'trophy', label=
_("%(user)s recently won a trophy! click here to see it.")
% {'user':w.author.name},
link = "/user/%s" % w.author.name)
if hasattr(item, "sr_id"):
w.subdigg = subreddits[item.sr_id]
vote = likes.get((user, item))
#.........这里部分代码省略.........
开发者ID:kevinrose,项目名称:diggit,代码行数:101,代码来源:builder.py
示例7: wrap_items
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
# get authors
# TODO pull the author stuff into add_props for links and
# comments and messages?
aids = set(l.author_id for l in items if hasattr(l, "author_id") and l.author_id is not None)
authors = {}
cup_infos = {}
email_attrses = {}
friend_rels = None
if aids:
authors = Account._byID(aids, True) if aids else {}
cup_infos = Account.cup_info_multi(aids)
if c.user_is_admin:
email_attrses = admintools.email_attrs(aids, return_dict=True)
if c.user.gold:
friend_rels = c.user.friend_rels()
subreddits = Subreddit.load_subreddits(items)
if not user:
can_ban_set = set()
else:
can_ban_set = set(id for (id, sr) in subreddits.iteritems() if sr.can_ban(user))
# get likes/dislikes
likes = queries.get_likes(user, items)
uid = user._id if user else None
types = {}
wrapped = []
count = 0
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") % dict(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)
# TODO pull the author stuff into add_props for links and
# comments and messages?
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 == "admin":
w.distinguished = "admin"
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 = "%s (%s)" % (_("friend"), note)
add_attr(w.attribs, "F", label)
except AttributeError:
pass
if w.distinguished == "admin" and w.author and w.author.name in g.admins:
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 False and w.author and c.user_is_admin:
for attr in email_attrses[w.author._id]:
add_attr(w.attribs, attr[2], label=attr[1])
if w.author and w.author._id in cup_infos and not c.profilepage:
cup_info = cup_infos[w.author._id]
label = _(cup_info["label_template"]) % {"user": w.author.name}
add_attr(w.attribs, "trophy:" + cup_info["img_url"], label=label, link="/user/%s" % w.author.name)
#.........这里部分代码省略.........
开发者ID:rmasters,项目名称:reddit,代码行数:101,代码来源:builder.py
注:本文中的r2.lib.template_helpers.add_attr函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论