예제 #1
0
    def _replace_render(style = None, display = True):
        """
        A helper function for listings to set uncachable attributes on a
        rendered thing (item) to its proper display values for the current
        context.
        """
        style = style or c.render_style or 'html'
        replacements = {}

        if hasattr(item, 'child'):
            if item.child:
                replacements['childlisting'] = item.child.render(style=style)
            else:
                # Special case for when the comment tree wasn't built which
                # occurs both in the inbox and spam page view of comments.
                replacements['childlisting'] = None
        else:
            replacements['childlisting'] = ''

        #only LinkListing has a show_nums attribute
        if listing:
            if hasattr(listing, "show_nums"):
                if listing.show_nums:
                    num_str = str(item.num)
                    if hasattr(listing, "num_margin"):
                        num_margin = str(listing.num_margin)
                    else:
                        num_margin = "%.2fex" % (len(str(listing.max_num))*1.1)
                else:
                    num_str = ''
                    num_margin = "0px;display:none"

                replacements["numcolmargin"] = num_margin
                replacements["num"] = num_str

            if hasattr(listing, "max_score"):
                mid_margin = len(str(listing.max_score))
                if hasattr(listing, "mid_margin"):
                    mid_margin = str(listing.mid_margin)
                elif mid_margin == 1:
                    mid_margin = "15px"
                else:
                    mid_margin = "%dex" % (mid_margin+1)

                replacements["midcolmargin"] = mid_margin

            #$votehash is only present when voting arrows are present
            if c.user_is_loggedin:
                replacements['votehash'] = vote_hash(c.user, item,
                                                     listing.vote_hash_type)
        if hasattr(item, "num_comments"):
            com_label, com_cls = comment_label(item.num_comments)
            if style == "compact":
                com_label = unicode(item.num_comments)
            replacements['numcomments'] = com_label
            replacements['commentcls'] = com_cls

        replacements['display'] =  "" if display else "style='display:none'"

        if hasattr(item, "render_score"):
            # replace the score stub
            (replacements['scoredislikes'],
             replacements['scoreunvoted'],
             replacements['scorelikes'])  = item.render_score

        # compute the timesince here so we don't end up caching it
        if hasattr(item, "_date"):
            if hasattr(item, "promoted") and item.promoted is not None:
                from r2.lib import promote
                # promoted links are special in their date handling
                replacements['timesince'] = timesince(item._date -
                                                      promote.timezone_offset)
            else:
                replacements['timesince'] = timesince(item._date)

            replacements['time_period'] = calc_time_period(item._date)

        # compute the last edited time here so we don't end up caching it
        if hasattr(item, "editted") and not isinstance(item.editted, bool):
            replacements['lastedited'] = timesince(item.editted)

        # Set in front.py:GET_comments()
        replacements['previous_visits_hex'] = c.previous_visits_hex

        renderer = render_func or item.render
        res = renderer(style = style, **replacements)

        if isinstance(res, (str, unicode)):
            rv = unsafe(res)
            if g.debug:
                for leftover in re.findall('<\$>(.+?)(?:<|$)', rv):
                    print "replace_render didn't replace %s" % leftover

            return rv

        return res
예제 #2
0
    def _replace_render(style = None, display = True):
        """
        A helper function for listings to set uncachable attributes on a
        rendered thing (item) to its proper display values for the current
        context.
        """
        style = style or c.render_style or 'html'
        replacements = {}

        child_txt = ( hasattr(item, "child") and item.child )\
            and item.child.render(style = style) or ""
        replacements["childlisting"] = child_txt


        #only LinkListing has a show_nums attribute
        if listing:
            if hasattr(listing, "show_nums"):
                if listing.show_nums:
                    num_str = str(item.num)
                    if hasattr(listing, "num_margin"):
                        num_margin = str(listing.num_margin)
                    else:
                        num_margin = "%.2fex" % (len(str(listing.max_num))*1.1)
                else:
                    num_str = ''
                    num_margin = "0px;display:none"

                replacements["numcolmargin"] = num_margin
                replacements["num"] = num_str

            if hasattr(listing, "max_score"):
                mid_margin = len(str(listing.max_score))
                if hasattr(listing, "mid_margin"):
                    mid_margin = str(listing.mid_margin)
                elif mid_margin == 1:
                    mid_margin = "15px"
                else:
                    mid_margin = "%dex" % (mid_margin+1)

                replacements["midcolmargin"] = mid_margin

            #$votehash is only present when voting arrows are present
            if c.user_is_loggedin:
                replacements['votehash'] = vote_hash(c.user, item,
                                                     listing.vote_hash_type)
        if hasattr(item, "num_comments"):
            if not item.num_comments:
                # generates "comment" the imperative verb
                com_label = _("comment {verb}")
                com_cls = 'comments empty'
            else:
                # generates "XX comments" as a noun
                com_label = ungettext("comment", "comments", item.num_comments)
                com_label = strings.number_label % dict(num=item.num_comments,
                                                        thing=com_label)
                com_cls = 'comments'
            if style == "compact":
                com_label = unicode(item.num_comments)
            replacements['numcomments'] = com_label
            replacements['commentcls'] = com_cls

        replacements['display'] =  "" if display else "style='display:none'"

        if hasattr(item, "render_score"):
            # replace the score stub
            (replacements['scoredislikes'],
             replacements['scoreunvoted'],
             replacements['scorelikes'])  = item.render_score

        # compute the timesince here so we don't end up caching it
        if hasattr(item, "_date"):
            if hasattr(item, "promoted") and item.promoted is not None:
                from r2.lib import promote
                # promoted links are special in their date handling
                replacements['timesince'] = timesince(item._date -
                                                      promote.timezone_offset)
            else:
                replacements['timesince'] = timesince(item._date)

            replacements['time_period'] = calc_time_period(item._date)

        # Set in front.py:GET_comments()
        replacements['previous_visits_hex'] = c.previous_visits_hex

        renderer = render_func or item.render
        res = renderer(style = style, **replacements)

        if isinstance(res, (str, unicode)):
            rv = unsafe(res)
            if g.debug:
                for leftover in re.findall('<\$>(.+?)(?:<|$)', rv):
                    print "replace_render didn't replace %s" % leftover

            return rv

        return res
예제 #3
0
    def _replace_render(style=None, display=True):
        """
        A helper function for listings to set uncachable attributes on a
        rendered thing (item) to its proper display values for the current
        context.
        """
        style = style or c.render_style or 'html'
        replacements = {}

        if hasattr(item, 'child'):
            if item.child:
                replacements['childlisting'] = item.child.render(style=style)
            else:
                # Special case for when the comment tree wasn't built which
                # occurs both in the inbox and spam page view of comments.
                replacements['childlisting'] = None
        else:
            replacements['childlisting'] = ''

        #only LinkListing has a show_nums attribute
        if listing:
            if hasattr(listing, "show_nums"):
                if listing.show_nums:
                    num_str = str(item.num)
                    if hasattr(listing, "num_margin"):
                        num_margin = str(listing.num_margin)
                    else:
                        num_margin = "%.2fex" % (len(str(listing.max_num)) *
                                                 1.1)
                else:
                    num_str = ''
                    num_margin = "0px;display:none"

                replacements["numcolmargin"] = num_margin
                replacements["num"] = num_str

            if hasattr(listing, "max_score"):
                mid_margin = len(str(listing.max_score))
                if hasattr(listing, "mid_margin"):
                    mid_margin = str(listing.mid_margin)
                elif mid_margin == 1:
                    mid_margin = "15px"
                else:
                    mid_margin = "%dex" % (mid_margin + 1)

                replacements["midcolmargin"] = mid_margin

            #$votehash is only present when voting arrows are present
            if c.user_is_loggedin:
                replacements['votehash'] = vote_hash(c.user, item,
                                                     listing.vote_hash_type)
        if hasattr(item, "num_comments"):
            com_label, com_cls = comment_label(item.num_comments)
            if style == "compact":
                com_label = unicode(item.num_comments)
            replacements['numcomments'] = com_label
            replacements['commentcls'] = com_cls

        replacements['display'] = "" if display else "style='display:none'"

        if hasattr(item, "render_score"):
            # replace the score stub
            (replacements['scoredislikes'], replacements['scoreunvoted'],
             replacements['scorelikes']) = item.render_score

        # compute the timesince here so we don't end up caching it
        if hasattr(item, "_date"):
            if hasattr(item, "promoted") and item.promoted is not None:
                from r2.lib import promote
                # promoted links are special in their date handling
                replacements['timesince'] = timesince(item._date -
                                                      promote.timezone_offset)
            else:
                replacements['timesince'] = timesince(item._date)

            replacements['time_period'] = calc_time_period(item._date)

        # compute the last edited time here so we don't end up caching it
        if hasattr(item, "editted") and not isinstance(item.editted, bool):
            replacements['lastedited'] = timesince(item.editted)

        # Set in front.py:GET_comments()
        replacements['previous_visits_hex'] = c.previous_visits_hex

        renderer = render_func or item.render
        res = renderer(style=style, **replacements)

        if isinstance(res, (str, unicode)):
            rv = unsafe(res)
            if g.debug:
                for leftover in re.findall('<\$>(.+?)(?:<|$)', rv):
                    print "replace_render didn't replace %s" % leftover

            return rv

        return res
예제 #4
0
def replace_render(listing, item, style = None, display = True):
    style = style or c.render_style or 'html'
    rendered_item = item.render(style = style)

    # for string rendered items
    def string_replace(x, y):
        return rendered_item.replace(x, y)

    # for JSON responses
    def dict_replace(x, y):
        try:
            res = rendered_item['data']['content']
            rendered_item['data']['content'] = res.replace(x, y)
        except AttributeError:
            pass
        except TypeError:
            pass
        return rendered_item

    child_txt = ( hasattr(item, "child") and item.child )\
        and item.child.render(style = style) or ""

    # handle API calls differently from normal request: dicts not strings are passed around
    if isinstance(rendered_item, dict):
        replace_fn = dict_replace
        try:
            rendered_item['data']['child'] = child_txt
        except AttributeError:
            pass
        except TypeError:
            pass
    else:
        replace_fn = string_replace
        rendered_item = replace_fn(u"$child", child_txt)

    #only LinkListing has a show_nums attribute
    if listing: 
        if hasattr(listing, "show_nums"):
            if listing.show_nums:
                num_str = str(item.num) 
                if hasattr(listing, "num_margin"):
                    num_margin = listing.num_margin
                else:
                    num_margin = "%5.2fex" % (len(str(listing.max_num))*1.1)
            else:
                num_str = ''
                num_margin = "0px"
    
            rendered_item = replace_fn(u"$numcolmargin", num_margin)
            rendered_item = replace_fn(u"$num", num_str)

        if hasattr(listing, "max_score"):
            mid_margin = len(str(listing.max_score)) 
            if hasattr(listing, "mid_margin"):
                mid_margin = listing.mid_margin
            elif mid_margin == 1:
                mid_margin = "15px"
            else:
                mid_margin = "%dex" % (mid_margin+1)

            rendered_item = replace_fn(u"$midcolmargin", mid_margin)

        # TODO: one of these things is not like the other.  We should & ->
        # $ elsewhere as it plays nicer with the websafe filter.
        rendered_item = replace_fn(u"$ListClass", listing._js_cls)

        #$votehash is only present when voting arrows are present
        if u'$votehash' in rendered_item:
            hash = vote_hash(c.user, item, listing.vote_hash_type)
            rendered_item = replace_fn(u'$votehash', hash)
            
    rendered_item = replace_fn(u"$display", "" if display else "style='display:none'")
    return rendered_item
예제 #5
0
def replace_render(listing, item, style=None, display=True):
    style = style or c.render_style or 'html'
    rendered_item = item.render(style=style)

    # for string rendered items
    def string_replace(x, y):
        return rendered_item.replace(x, y)

    # for JSON responses
    def dict_replace(x, y):
        try:
            res = rendered_item['data']['content']
            rendered_item['data']['content'] = res.replace(x, y)
        except AttributeError:
            pass
        except TypeError:
            pass
        return rendered_item

    child_txt = ( hasattr(item, "child") and item.child )\
        and item.child.render(style = style) or ""

    # handle API calls differently from normal request: dicts not strings are passed around
    if isinstance(rendered_item, dict):
        replace_fn = dict_replace
        try:
            rendered_item['data']['child'] = child_txt
        except AttributeError:
            pass
        except TypeError:
            pass
    else:
        replace_fn = string_replace
        rendered_item = replace_fn(u"$child", child_txt)

    #only LinkListing has a show_nums attribute
    if listing:
        if hasattr(listing, "show_nums"):
            if listing.show_nums:
                num_str = str(item.num)
                if hasattr(listing, "num_margin"):
                    num_margin = listing.num_margin
                else:
                    num_margin = "%5.2fex" % (len(str(listing.max_num)) * 1.1)
            else:
                num_str = ''
                num_margin = "0px"

            rendered_item = replace_fn(u"$numcolmargin", num_margin)
            rendered_item = replace_fn(u"$num", num_str)

        if hasattr(listing, "max_score"):
            mid_margin = len(str(listing.max_score))
            if hasattr(listing, "mid_margin"):
                mid_margin = listing.mid_margin
            elif mid_margin == 1:
                mid_margin = "15px"
            else:
                mid_margin = "%dex" % (mid_margin + 1)

            rendered_item = replace_fn(u"$midcolmargin", mid_margin)

        # TODO: one of these things is not like the other.  We should & ->
        # $ elsewhere as it plays nicer with the websafe filter.
        rendered_item = replace_fn(u"$ListClass", listing._js_cls)

        #$votehash is only present when voting arrows are present
        if c.user_is_loggedin and u'$votehash' in rendered_item:
            hash = vote_hash(c.user, item, listing.vote_hash_type)
            rendered_item = replace_fn(u'$votehash', hash)

    rendered_item = replace_fn(u"$display",
                               "" if display else "style='display:none'")
    return rendered_item
예제 #6
0
    def _replace_render(style=None, display=True):
        """
        A helper function for listings to set uncachable attributes on a
        rendered thing (item) to its proper display values for the current
        context.
        """
        style = style or c.render_style or 'html'
        replacements = {}

        child_txt = ( hasattr(item, "child") and item.child )\
            and item.child.render(style = style) or ""
        replacements["childlisting"] = child_txt

        #only LinkListing has a show_nums attribute
        if listing:
            if hasattr(listing, "show_nums"):
                if listing.show_nums:
                    num_str = str(item.num)
                    if hasattr(listing, "num_margin"):
                        num_margin = str(listing.num_margin)
                    else:
                        num_margin = "%.2fex" % (len(str(listing.max_num)) *
                                                 1.1)
                else:
                    num_str = ''
                    num_margin = "0px;display:none"

                replacements["numcolmargin"] = num_margin
                replacements["num"] = num_str

            if hasattr(listing, "max_score"):
                mid_margin = len(str(listing.max_score))
                if hasattr(listing, "mid_margin"):
                    mid_margin = str(listing.mid_margin)
                elif mid_margin == 1:
                    mid_margin = "15px"
                else:
                    mid_margin = "%dex" % (mid_margin + 1)

                replacements["midcolmargin"] = mid_margin

            #$votehash is only present when voting arrows are present
            if c.user_is_loggedin:
                replacements['votehash'] = vote_hash(c.user, item,
                                                     listing.vote_hash_type)
        if hasattr(item, "num_comments"):
            if not item.num_comments:
                # generates "comment" the imperative verb
                com_label = _("comment {verb}")
                com_cls = 'comments empty'
            else:
                # generates "XX comments" as a noun
                com_label = ungettext("comment", "comments", item.num_comments)
                com_label = strings.number_label % dict(num=item.num_comments,
                                                        thing=com_label)
                com_cls = 'comments'
            if style == "compact":
                com_label = unicode(item.num_comments)
            replacements['numcomments'] = com_label
            replacements['commentcls'] = com_cls

        replacements['display'] = "" if display else "style='display:none'"

        if hasattr(item, "render_score"):
            # replace the score stub
            (replacements['scoredislikes'], replacements['scoreunvoted'],
             replacements['scorelikes']) = item.render_score

        # compute the timesince here so we don't end up caching it
        if hasattr(item, "_date"):
            if hasattr(item, "promoted") and item.promoted is not None:
                from r2.lib import promote
                # promoted links are special in their date handling
                replacements['timesince'] = timesince(item._date -
                                                      promote.timezone_offset)
            else:
                replacements['timesince'] = timesince(item._date)

            replacements['time_period'] = calc_time_period(item._date)

        # Set in front.py:GET_comments()
        replacements['previous_visits_hex'] = c.previous_visits_hex

        renderer = render_func or item.render
        res = renderer(style=style, **replacements)

        if isinstance(res, (str, unicode)):
            rv = unsafe(res)
            if g.debug:
                for leftover in re.findall('<\$>(.+?)(?:<|$)', rv):
                    print "replace_render didn't replace %s" % leftover

            return rv

        return res
예제 #7
0
    def _replace_render(style = None, display = True):
        """
        A helper function for listings to set uncachable attributes on a
        rendered thing (item) to its proper display values for the current
        context.
        """
        style = style or c.render_style or 'html'
        replacements = {}
    
        child_txt = ( hasattr(item, "child") and item.child )\
            and item.child.render(style = style) or ""
        replacements["childlisting"] = child_txt
        
    
        #only LinkListing has a show_nums attribute
        if listing: 
            if hasattr(listing, "show_nums"):
                if listing.show_nums:
                    num_str = str(item.num) 
                    if hasattr(listing, "num_margin"):
                        num_margin = str(listing.num_margin)
                    else:
                        num_margin = "%.2fex" % (len(str(listing.max_num))*1.1)
                else:
                    num_str = ''
                    num_margin = "0px"
    
                replacements["numcolmargin"] = num_margin
                replacements["num"] = num_str
    
            if hasattr(listing, "max_score"):
                mid_margin = len(str(listing.max_score)) 
                if hasattr(listing, "mid_margin"):
                    mid_margin = str(listing.mid_margin)
                elif mid_margin == 1:
                    mid_margin = "15px"
                else:
                    mid_margin = "%dex" % (mid_margin+1)
    
                replacements["midcolmargin"] = mid_margin
    
            #$votehash is only present when voting arrows are present
            if c.user_is_loggedin:
                replacements['votehash'] = vote_hash(c.user, item,
                                                     listing.vote_hash_type)
        if hasattr(item, "num_comments"):
            if not item.num_comments:
                # generates "comment" the imperative verb
                com_label = _("comment {verb}")
                com_cls = 'comments empty'
            else:
                # generates "XX comments" as a noun
                com_label = ungettext("comment", "comments", item.num_comments)
                com_label = strings.number_label % dict(num=item.num_comments,
                                                        thing=com_label)
                com_cls = 'comments'
            replacements['numcomments'] = com_label
            replacements['commentcls'] = com_cls
    
        replacements['display'] =  "" if display else "style='display:none'"
    
        if hasattr(item, "render_score"):
            # replace the score stub
            (replacements['scoredislikes'],
             replacements['scoreunvoted'],
             replacements['scorelikes'])  = item.render_score
    
        # compute the timesince here so we don't end up caching it
        if hasattr(item, "_date"):
            replacements['timesince'] = timesince(item._date)

        renderer = render_func or item.render
        res = renderer(style = style, **replacements)
        if isinstance(res, (str, unicode)):
            return unsafe(res)
        return res