Example #1
0
    def add_props(cls, user, wrapped):
        #TODO global-ish functions that shouldn't be here?
        #reset msgtime after this request
        msgtime = c.have_messages
        
        #load the "to" field if required
        to_ids = set(w.to_id for w in wrapped)
        tos = Account._byID(to_ids, True) if to_ids else {}
        links = Link._byID(set(l.link_id for l in wrapped if l.was_comment),
                           data = True,
                           return_dict = True)
        subdiggs = Subreddit._byID(set(l.sr_id for l in links.values()),
                                     data = True, return_dict = True)
        parents = Comment._byID(set(l.parent_id for l in wrapped
                                  if hasattr(l, "parent_id") and l.was_comment),
                                data = True, return_dict = True)

        for item in wrapped:
            item.to = tos[item.to_id]
            if msgtime and item._date >= msgtime:
                item.new = True
            else:
                item.new = False
            item.score_fmt = Score.none

            item.message_style = ""
            if item.was_comment:
                link = links[item.link_id]
                sr = subdiggs[link.sr_id]
                item.link_title = link.title
                item.link_permalink = link.make_permalink(sr)
                if hasattr(item, "parent_id"):
                    item.subject = _('comment reply')
                    item.message_style = "comment-reply"
                    parent = parents[item.parent_id]
                    item.parent = parent._fullname
                    item.parent_permalink = parent.make_permalink(link, sr)
                else:
                    item.subject = _('post reply')
                    item.message_style = "post-reply"

        # Run this last
        Printable.add_props(user, wrapped)
Example #2
0
    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

        subdiggs = Subreddit._byID(set(cm.sr_id for cm in wrapped),
                                     data=True,return_dict=False)

        can_reply_srs = set(s._id for s in subdiggs 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, 'subdigg'):
                item.subdigg = item.subreddit_slow
            if item.author_id == item.link.author_id:
                add_attr(item.attribs, 'S',
                         link = item.link.make_permalink(item.subdigg))
            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.subdigg)
            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.subdigg)

            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)