def set_recent_clicks(): c.recent_clicks = [] if not c.user_is_loggedin: return click_cookie = read_user_cookie('recentclicks2') if click_cookie: if valid_click_cookie(click_cookie): names = [x for x in UniqueIterator(click_cookie.split(',')) if x] if len(click_cookie) > 1000: names = names[:20] set_user_cookie('recentclicks2', ','.join(names)) #eventually this will look at the user preference names = names[:5] try: c.recent_clicks = Link._by_fullname(names, data=True, return_dict=False) except NotFound: # clear their cookie because it's got bad links in it set_user_cookie('recentclicks2', '') else: #if the cookie wasn't valid, clear it set_user_cookie('recentclicks2', '')
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_tuples(self, t): self.fetch() # insert the new items, remove the duplicates (keeping the one # being inserted over the stored value if applicable), and # sort the result data = itertools.chain(t, self.data) data = UniqueIterator(data, key=lambda x: x[0]) data = sorted(data, key=lambda x: x[1:], reverse=True) data = list(data) data = data[:precompute_limit] self.data = data query_cache.set(self.iden, self.data)
def __init__(self, results): self.cached_results = results CachedResults.fetch_multi( [r for r in results if isinstance(r, CachedResults)]) self._fetched = True self.sort = results[0].sort # make sure they're all the same assert all(r.sort == self.sort for r in results[1:]) # if something is 'top' for the year *and* for today, it would # appear in both listings, so we need to filter duplicates all_items = UniqueIterator( (item for cr in results for item in cr.data), key=lambda x: x[0]) all_items = sorted(all_items, cmp=self._thing_cmp) self.data = list(all_items)