Ejemplo n.º 1
0
 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]
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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