def _send_things(self, things, action=None): from r2.models import IDBuilder things = tup(things) if not all(isinstance(t, Wrapped) for t in things): b = IDBuilder([t._fullname for t in things]) things = b.get_items()[0] self.object = [self._thing(thing, action=action) for thing in things]
def insert_promoted(link_names, pos, promoted_every_n=5): """ Inserts promoted links into an existing organic list. Destructive on `link_names' """ promo_tuples = randomized_promotion_list(c.user, c.site) promoted_link_names, campaign_ids = zip( *promo_tuples) if promo_tuples else ([], []) if not promoted_link_names: return link_names, pos, {} campaigns_by_link = dict(promo_tuples) # no point in running the builder over more promoted links than # we'll even use max_promoted = max(1, len(link_names) / promoted_every_n) builder = IDBuilder(promoted_link_names, keep_fn=keep_fresh_links, skip=True) promoted_items = builder.get_items()[0] focus = None if promoted_items: focus = promoted_items[0]._fullname # insert one promoted item for every N items for i, item in enumerate(promoted_items): p = i * (promoted_every_n + 1) if p > len(link_names): break p += pos if p > len(link_names): p = p % len(link_names) link_names.insert(p, item._fullname) link_names = filter(None, link_names) if focus: try: pos = link_names.index(focus) except ValueError: pass # don't insert one at the head of the list 50% of the time for # logged in users, and 50% of the time for logged-off users when # the pool of promoted links is less than 3 (to avoid showing the # same promoted link to the same person too often) if ((c.user_is_loggedin or len(promoted_items) < 3) and random.choice( (True, False))): pos = (pos + 1) % len(link_names) return list(UniqueIterator(link_names)), pos, campaigns_by_link
def insert_promoted(link_names, pos, promoted_every_n=5): """ Inserts promoted links into an existing organic list. Destructive on `link_names' """ promo_tuples = randomized_promotion_list(c.user, c.site) promoted_link_names, campaign_ids = zip(*promo_tuples) if promo_tuples else ([], []) if not promoted_link_names: return link_names, pos, {} campaigns_by_link = dict(promo_tuples) # no point in running the builder over more promoted links than # we'll even use max_promoted = max(1, len(link_names) / promoted_every_n) builder = IDBuilder(promoted_link_names, keep_fn=keep_fresh_links, skip=True) promoted_items = builder.get_items()[0] focus = None if promoted_items: focus = promoted_items[0]._fullname # insert one promoted item for every N items for i, item in enumerate(promoted_items): p = i * (promoted_every_n + 1) if p > len(link_names): break p += pos if p > len(link_names): p = p % len(link_names) link_names.insert(p, item._fullname) link_names = filter(None, link_names) if focus: try: pos = link_names.index(focus) except ValueError: pass # don't insert one at the head of the list 50% of the time for # logged in users, and 50% of the time for logged-off users when # the pool of promoted links is less than 3 (to avoid showing the # same promoted link to the same person too often) if ((c.user_is_loggedin or len(promoted_items) < 3) and random.choice((True, False))): pos = (pos + 1) % len(link_names) return list(UniqueIterator(link_names)), pos, campaigns_by_link
def filter_links(links, filter_spam=False, multiple=True): # run the list through a builder to remove any that the user # isn't allowed to see from pylons import c from r2.models import IDBuilder, Link, Subreddit, NotFound links = IDBuilder([link._fullname for link in links], skip=False).get_items()[0] if not links: return if filter_spam: # first, try to remove any spam links_nonspam = [link for link in links if not link._spam] if links_nonspam: links = links_nonspam # if it occurs in one or more of their subscriptions, show them # that one first subs = set(Subreddit.user_subreddits(c.user, limit=None)) def cmp_links(a, b): if a.sr_id in subs and b.sr_id not in subs: return -1 elif a.sr_id not in subs and b.sr_id in subs: return 1 else: return cmp(b._hot, a._hot) links = sorted(links, cmp=cmp_links) # among those, show them the hottest one return links if multiple else links[0]
def url_links_builder(url, exclude=None, num=None, after=None, reverse=None, count=None): from r2.lib.template_helpers import add_sr from r2.models import IDBuilder, Link, NotFound from operator import attrgetter if url.startswith('/'): url = add_sr(url, force_hostname=True) try: links = tup(Link._by_url(url, None)) except NotFound: links = [] links = [ link for link in links if link._fullname != exclude ] links.sort(key=attrgetter('num_comments'), reverse=True) # don't show removed links in duplicates unless admin or mod # or unless it's your own post def include_link(link): return (not link._spam or (c.user_is_loggedin and (link.author_id == c.user._id or c.user_is_admin or link.subreddit.is_moderator(c.user)))) builder = IDBuilder([link._fullname for link in links], skip=True, keep_fn=include_link, num=num, after=after, reverse=reverse, count=count) return builder
def GET_wiki_discussions(self, page, num, after, reverse, count): page_url = add_sr("%s/%s" % (c.wiki_base_url, page.name)) links = url_links(page_url) builder = IDBuilder([ link._fullname for link in links ], num = num, after = after, reverse = reverse, count = count, skip = False) listing = LinkListing(builder).listing() return WikiDiscussions(listing).render()
def wrap_links(links, wrapper = default_thing_wrapper(), listing_cls = LinkListing, num = None, show_nums = False, nextprev = False, **kw): links = tup(links) if not all(isinstance(x, basestring) for x in links): links = [x._fullname for x in links] b = IDBuilder(links, num = num, wrap = wrapper, **kw) l = listing_cls(b, nextprev = nextprev, show_nums = show_nums) return l.listing()
def wrap_links(links, wrapper = default_thing_wrapper(), listing_cls = LinkListing, num = None, show_nums = False, nextprev = False, num_margin = None, mid_margin = None, **kw): links = tup(links) if not all(isinstance(x, str) for x in links): links = [x._fullname for x in links] b = IDBuilder(links, num = num, wrap = wrapper, **kw) l = listing_cls(b, nextprev = nextprev, show_nums = show_nums) if num_margin is not None: l.num_margin = num_margin if mid_margin is not None: l.mid_margin = mid_margin return l.listing()
def __init__(self, link = None, comment = None, link_title = '', is_canonical = False, *a, **kw): link.render_full = True # TODO: temp hack until we find place for builder_wrapper from r2.controllers.listingcontroller import ListingController if comment: link_wrapper = lambda link: self.comment_permalink_wrapper(comment, link) else: link_wrapper = ListingController.builder_wrapper link_builder = IDBuilder(link._fullname, wrap = link_wrapper) # link_listing will be the one-element listing at the top self.link_listing = LinkListing(link_builder, nextprev=False).listing() # link is a wrapped Link object self.link = self.link_listing.things[0] link_title = ((self.link.title) if hasattr(self.link, 'title') else '') if comment: title = comment.make_permalink_title(link) # Comment permalinks should not be indexed, there's too many of them self.robots = 'noindex' if is_canonical == False: self.canonical_link = comment.make_permalink(link) else: params = {'title':_force_unicode(link_title), 'site' : c.site.title} title = strings.link_info_title % params if not (c.default_sr and is_canonical): # Not on the main page, so include a pointer to the canonical URL for this link self.canonical_link = link.canonical_url Reddit.__init__(self, title = title, body_class = 'post', robots = self.robots, *a, **kw)
def get_discussions(event, limit, show_hidden=False): """Return a builder providing Links that point at the given live thread.""" hidden_links = event.hidden_discussions def _keep_discussion_link(link): if link._spam or link._deleted: return False # just don't allow any private subreddits so we don't get into a # situation where an abusive link is posted in a private subreddit and # contributors can't do anything about it because they can't see it. if link.subreddit_slow.type in Subreddit.private_types: return False if not link.subreddit_slow.discoverable: return False if not getattr(link, "allow_liveupdate", True): return False if link._score < g.liveupdate_min_score_for_discussions: return False link.is_hidden_discussion = link._id in hidden_links if not show_hidden and link.is_hidden_discussion: return False return True link_fullnames = _get_related_link_ids(event._id) link_fullnames = link_fullnames[:limit] return IDBuilder( query=link_fullnames, skip=True, keep_fn=_keep_discussion_link, )
def __init__(self): sr_ids = Subreddit.user_subreddits(c.user if c.user_is_loggedin else None) srs = Subreddit._byID(sr_ids, True, return_dict = False) srs.sort(key = lambda sr: sr.name.lower()) b = IDBuilder([sr._fullname for sr in srs]) self.reddits = LinkListing(b).listing().things