def add_props(cls, user, wrapped): names = ('subscriber', 'moderator', 'contributor') rels = (SRMember._fast_query(wrapped, [user], names) if c.user_is_loggedin else {}) defaults = Subreddit.default_subreddits() target = "_top" if c.cname else None for item in wrapped: if not user or not user.has_subscribed: item.subscriber = item._id in defaults else: item.subscriber = bool(rels.get((item, user, 'subscriber'))) item.moderator = bool(rels.get((item, user, 'moderator'))) item.contributor = bool(item.type != 'public' and (item.moderator or rels.get( (item, user, 'contributor')))) # Don't reveal revenue information via /r/lounge's subscribers if (g.lounge_reddit and item.name == g.lounge_reddit and not c.user_is_admin): item._ups = 0 item.score = item._ups # override "voting" score behavior (it will override the use of # item.score in builder.py to be ups-downs) item.likes = item.subscriber or None base_score = item.score - (1 if item.likes else 0) item.voting_score = [(base_score + x - 1) for x in range(3)] item.score_fmt = Score.subscribers #will seem less horrible when add_props is in pages.py from r2.lib.pages import UserText item.usertext = UserText(item, item.description, target=target) Printable.add_props(user, wrapped)
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: item.subreddit_path = ("http://" + get_domain( cname=(site == item.subreddit), subreddit=False)) if site != item.subreddit: item.subreddit_path += item.subreddit.path item.full_comment_path = item.link.make_permalink(item.subreddit) # don't collapse for admins, on profile pages, or if deleted item.collapsed = False if ((item.score < min_score) and not (profilepage or item.deleted or user_is_admin)): item.collapsed = True item.collapsed_reason = _("comment score below threshold") if user_is_loggedin and item.author_id in c.user.enemies: if "grayed" not in extra_css: extra_css += " grayed" item.collapsed = True item.collapsed_reason = _("blocked user") 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) item_age = c.start_time - item._date if item_age.days > g.VOTE_AGE_LIMIT: item.votable = False else: item.votable = True #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, target=item.target, extra_css=extra_css) # Run this last Printable.add_props(user, wrapped)
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) 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) 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) #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, target = item.target, extra_css = extra_css) # Run this last Printable.add_props(user, wrapped)