예제 #1
0
    def spotlight(self):
        if (isinstance(c.site, DefaultSR)
            and (not c.user_is_loggedin
                 or (c.user_is_loggedin and c.user.pref_organic))):

            spotlight_links = organic.organic_links(c.user)
            pos = organic_pos()

            if not spotlight_links:
                pos = 0
            elif pos != 0:
                pos = pos % len(spotlight_links)

            if c.user.pref_show_sponsors or not c.user.gold:
                spotlight_links, pos = promote.insert_promoted(spotlight_links, pos)
            trial = populate_spotlight()

            # Need to do this again, because if there was a duplicate removed,
            # pos might be pointing outside the list.
            if not spotlight_links:
                pos = 0
            elif pos != 0:
                pos = pos % len(spotlight_links)

            if trial:
                spotlight_links.insert(pos, trial._fullname)

            if not spotlight_links:
                return None

            # get links in proximity to pos
            num_tl = len(spotlight_links)
            if num_tl <= 3:
                disp_links = spotlight_links
            else:
                left_side = max(-1, min(num_tl - 3, 8))
                disp_links = [spotlight_links[(i + pos) % num_tl]
                              for i in xrange(-2, left_side)]

            def trial_keep_fn(item):
                if trial and trial._fullname == item._fullname:
                    return True
                return organic.keep_fresh_links(item)

            def trial_wrap(item):
               if item is trial:
                   w = Wrapped(item)
                   w.trial_mode = True
                   w.render_class = LinkOnTrial
                   return w
               return self.builder_wrapper(item)

            b = IDBuilder(disp_links,
                          wrap = trial_wrap if trial else self.builder_wrapper,
                          num = organic.organic_length,
                          skip = True,
                          keep_fn = trial_keep_fn if trial else organic.keep_fresh_links)

            try:
                vislink = spotlight_links[pos]
            except IndexError:
                g.log.error("spotlight_links = %r" % spotlight_links)
                g.log.error("pos = %d" % pos)
                raise

            s = SpotlightListing(b, spotlight_links = spotlight_links,
                                 visible_link = vislink,
                                 max_num = self.listing_obj.max_num,
                                 max_score = self.listing_obj.max_score).listing()

            if len(s.things) > 0:
                # only pass through a listing if the links made it
                # through our builder
                organic.update_pos(pos+1)
                return s

        # no organic box on a hot page, then show a random promoted link
        elif not isinstance(c.site, DefaultSR) and c.user.pref_show_sponsors:
            link_ids = randomized_promotion_list(c.user, c.site)
            if link_ids:
                res = wrap_links(link_ids, wrapper = self.builder_wrapper,
                                 num = 1, keep_fn = lambda x: x.fresh, 
                                 skip = True)
                if res.things:
                    return res
예제 #2
0
    def spotlight(self):
        if (isinstance(c.site, DefaultSR)
                and (not c.user_is_loggedin or
                     (c.user_is_loggedin and c.user.pref_organic))):

            spotlight_links = organic.organic_links(c.user)
            pos = organic_pos()

            if not spotlight_links:
                pos = 0
            elif pos != 0:
                pos = pos % len(spotlight_links)

            if c.user.pref_show_sponsors or not c.user.gold:
                spotlight_links, pos = promote.insert_promoted(
                    spotlight_links, pos)
            trial = populate_spotlight()

            # Need to do this again, because if there was a duplicate removed,
            # pos might be pointing outside the list.
            if not spotlight_links:
                pos = 0
            elif pos != 0:
                pos = pos % len(spotlight_links)

            if trial:
                spotlight_links.insert(pos, trial._fullname)

            if not spotlight_links:
                return None

            # get links in proximity to pos
            num_tl = len(spotlight_links)
            if num_tl <= 3:
                disp_links = spotlight_links
            else:
                left_side = max(-1, min(num_tl - 3, 8))
                disp_links = [
                    spotlight_links[(i + pos) % num_tl]
                    for i in xrange(-2, left_side)
                ]

            def trial_keep_fn(item):
                if trial and trial._fullname == item._fullname:
                    return True
                return organic.keep_fresh_links(item)

            def trial_wrap(item):
                if item is trial:
                    w = Wrapped(item)
                    w.trial_mode = True
                    w.render_class = LinkOnTrial
                    return w
                return self.builder_wrapper(item)

            b = IDBuilder(
                disp_links,
                wrap=trial_wrap if trial else self.builder_wrapper,
                num=organic.organic_length,
                skip=True,
                keep_fn=trial_keep_fn if trial else organic.keep_fresh_links)

            try:
                vislink = spotlight_links[pos]
            except IndexError:
                g.log.error("spotlight_links = %r" % spotlight_links)
                g.log.error("pos = %d" % pos)
                raise

            s = SpotlightListing(
                b,
                spotlight_links=spotlight_links,
                visible_link=vislink,
                max_num=self.listing_obj.max_num,
                max_score=self.listing_obj.max_score).listing()

            if len(s.things) > 0:
                # only pass through a listing if the links made it
                # through our builder
                organic.update_pos(pos + 1)
                return s

        # no organic box on a hot page, then show a random promoted link
        elif not isinstance(c.site, DefaultSR) and c.user.pref_show_sponsors:
            link_ids = randomized_promotion_list(c.user, c.site)
            if link_ids:
                res = wrap_links(link_ids,
                                 wrapper=self.builder_wrapper,
                                 num=1,
                                 keep_fn=lambda x: x.fresh,
                                 skip=True)
                res.parent_name = "promoted"
                if res.things:
                    return res
예제 #3
0
    def spotlight(self):
        campaigns_by_link = {}
        if (self.requested_ad or
            not isinstance(c.site, DefaultSR) and c.user.pref_show_sponsors):

            link_ids = None

            if self.requested_ad:
                link = None
                try:
                    link = Link._by_fullname(self.requested_ad)
                except NotFound:
                    pass

                if not (link and link.promoted and
                        (c.user_is_sponsor or
                         c.user_is_loggedin and link.author_id == c.user._id)):
                    return self.abort404()

                # check if we can show the requested ad
                if promote.is_live_on_sr(link, c.site.name):
                    link_ids = [link._fullname]
                else:
                    return _("requested campaign not eligible for display")
            else:
                # no organic box on a hot page, then show a random promoted link
                promo_tuples = randomized_promotion_list(c.user, c.site)
                link_ids, camp_ids = zip(*promo_tuples) if promo_tuples else ([],[])

                # save campaign-to-link mapping so campaign can be added to 
                # link data later (for tracking.) Gotcha: assumes each link 
                # appears for only campaign
                campaigns_by_link = dict(promo_tuples)

            if link_ids:
                res = wrap_links(link_ids, wrapper=self.builder_wrapper,
                                 num=1, keep_fn=lambda x: x.fresh, skip=True)
                res.parent_name = "promoted"
                if res.things:
                    # store campaign id for tracking
                    for thing in res.things:
                        thing.campaign = campaigns_by_link.get(thing._fullname, None)
                    return res

        elif (isinstance(c.site, DefaultSR)
            and (not c.user_is_loggedin
                 or (c.user_is_loggedin and c.user.pref_organic))):

            spotlight_links = organic.organic_links(c.user)
            
            pos = organic_pos()

            if not spotlight_links:
                pos = 0
            elif pos != 0:
                pos = pos % len(spotlight_links)
            spotlight_keep_fn = organic.keep_fresh_links
            num_links = organic.organic_length

            # If prefs allow it, mix in promoted links and sr discovery content
            if c.user.pref_show_sponsors or not c.user.gold:
                if g.live_config['sr_discovery_links']:
                    spotlight_links.extend(g.live_config['sr_discovery_links'])
                    random.shuffle(spotlight_links)
                    spotlight_keep_fn = lambda l: promote.is_promo(l) or organic.keep_fresh_links(l)
                    num_links = len(spotlight_links)
                spotlight_links, pos, campaigns_by_link = promote.insert_promoted(spotlight_links,
                                                                                  pos) 

            # Need to do this again, because if there was a duplicate removed,
            # pos might be pointing outside the list.
            if not spotlight_links:
                pos = 0
            elif pos != 0:
                pos = pos % len(spotlight_links)

            if not spotlight_links:
                return None

            # get links in proximity to pos
            num_tl = len(spotlight_links)
            if num_tl <= 3:
                disp_links = spotlight_links
            else:
                left_side = max(-1, min(num_tl - 3, 8))
                disp_links = [spotlight_links[(i + pos) % num_tl]
                              for i in xrange(-2, left_side)]

            b = IDBuilder(disp_links,
                          wrap = self.builder_wrapper,
                          num = num_links,
                          keep_fn = spotlight_keep_fn,
                          skip = True)

            try:
                vislink = spotlight_links[pos]
            except IndexError:
                g.log.error("spotlight_links = %r" % spotlight_links)
                g.log.error("pos = %d" % pos)
                raise

            s = SpotlightListing(b, spotlight_items = spotlight_links,
                                 visible_item = vislink,
                                 max_num = self.listing_obj.max_num,
                                 max_score = self.listing_obj.max_score).listing()

            has_subscribed = c.user.has_subscribed
            promo_visible = promote.is_promo(s.lookup[vislink])
            if not promo_visible:
                prob = g.live_config['spotlight_interest_sub_p'
                                     if has_subscribed else
                                     'spotlight_interest_nosub_p']
                if random.random() < prob:
                    bar = InterestBar(has_subscribed)
                    s.spotlight_items.insert(pos, bar)
                    s.visible_item = bar

            if len(s.things) > 0:
                # only pass through a listing if the links made it
                # through our builder
                organic.update_pos(pos+1)
                # add campaign id to promoted links for tracking
                for thing in s.things:
                    thing.campaign = campaigns_by_link.get(thing._fullname, None)
                return s
예제 #4
0
    def spotlight(self):
        if (self.requested_ad or
            not isinstance(c.site, DefaultSR) and c.user.pref_show_sponsors):

            link_ids = None

            if self.requested_ad:
                link = None
                try:
                    link = Link._by_fullname(self.requested_ad)
                except NotFound:
                    pass

                if not (link and link.promoted and
                        (c.user_is_sponsor or
                         c.user_is_loggedin and link.author_id == c.user._id)):
                    return self.abort404()

                # check if we can show the requested ad
                if promote.is_live_on_sr(link, c.site.name):
                    link_ids = [link._fullname]
                else:
                    return _("requested campaign not eligible for display")
            else:
                # no organic box on a hot page, then show a random promoted link
                link_ids = randomized_promotion_list(c.user, c.site)

            if link_ids:
                res = wrap_links(link_ids, wrapper=self.builder_wrapper,
                                 num=1, keep_fn=lambda x: x.fresh, skip=True)
                res.parent_name = "promoted"
                if res.things:
                    return res

        elif (isinstance(c.site, DefaultSR)
            and (not c.user_is_loggedin
                 or (c.user_is_loggedin and c.user.pref_organic))):

            # Spotlight shows rising links. If available, mix in subreddit
            # discovery links as well. (These don't count towards ad bids)
            spotlight_links = organic.organic_links(c.user)
            if hasattr(g, 'sr_discovery_links'):
                spotlight_links.extend(g.sr_discovery_links)
                random.shuffle(spotlight_links)
                spotlight_keep_fn = lambda l: promote.is_promo(l) or organic.keep_fresh_links(l)
                num_links = len(spotlight_links)
            else:
                spotlight_keep_fn = organic.keep_fresh_links
                num_links = organic.organic_length

            pos = organic_pos()

            if not spotlight_links:
                pos = 0
            elif pos != 0:
                pos = pos % len(spotlight_links)

            if c.user.pref_show_sponsors or not c.user.gold:
                spotlight_links, pos = promote.insert_promoted(spotlight_links, pos)

            # Need to do this again, because if there was a duplicate removed,
            # pos might be pointing outside the list.
            if not spotlight_links:
                pos = 0
            elif pos != 0:
                pos = pos % len(spotlight_links)

            if not spotlight_links:
                return None

            # get links in proximity to pos
            num_tl = len(spotlight_links)
            if num_tl <= 3:
                disp_links = spotlight_links
            else:
                left_side = max(-1, min(num_tl - 3, 8))
                disp_links = [spotlight_links[(i + pos) % num_tl]
                              for i in xrange(-2, left_side)]

            b = IDBuilder(disp_links,
                          wrap = self.builder_wrapper,
                          num = num_links,
                          keep_fn = spotlight_keep_fn,
                          skip = True)

            try:
                vislink = spotlight_links[pos]
            except IndexError:
                g.log.error("spotlight_links = %r" % spotlight_links)
                g.log.error("pos = %d" % pos)
                raise

            s = SpotlightListing(b, spotlight_links = spotlight_links,
                                 visible_link = vislink,
                                 max_num = self.listing_obj.max_num,
                                 max_score = self.listing_obj.max_score).listing()

            if len(s.things) > 0:
                # only pass through a listing if the links made it
                # through our builder
                organic.update_pos(pos+1)
                return s
예제 #5
0
    def spotlight(self):
        campaigns_by_link = {}
        if (self.requested_ad or not isinstance(c.site, DefaultSR)
                and c.user.pref_show_sponsors):

            link_ids = None

            if self.requested_ad:
                link = None
                try:
                    link = Link._by_fullname(self.requested_ad)
                except NotFound:
                    pass

                if not (link and link.promoted and
                        (c.user_is_sponsor or c.user_is_loggedin
                         and link.author_id == c.user._id)):
                    return self.abort404()

                # check if we can show the requested ad
                if promote.is_live_on_sr(link, c.site.name):
                    link_ids = [link._fullname]
                else:
                    return _("requested campaign not eligible for display")
            else:
                # no organic box on a hot page, then show a random promoted link
                promo_tuples = randomized_promotion_list(c.user, c.site)
                link_ids, camp_ids = zip(
                    *promo_tuples) if promo_tuples else ([], [])

                # save campaign-to-link mapping so campaign can be added to
                # link data later (for tracking.) Gotcha: assumes each link
                # appears for only campaign
                campaigns_by_link = dict(promo_tuples)

            if link_ids:
                res = wrap_links(link_ids,
                                 wrapper=self.builder_wrapper,
                                 num=1,
                                 keep_fn=lambda x: x.fresh,
                                 skip=True)
                res.parent_name = "promoted"
                if res.things:
                    # store campaign id for tracking
                    for thing in res.things:
                        thing.campaign = campaigns_by_link.get(
                            thing._fullname, None)
                    return res

        elif (isinstance(c.site, DefaultSR)
              and (not c.user_is_loggedin or
                   (c.user_is_loggedin and c.user.pref_organic))):

            spotlight_links = organic.organic_links(c.user)

            pos = organic_pos()

            if not spotlight_links:
                pos = 0
            elif pos != 0:
                pos = pos % len(spotlight_links)
            spotlight_keep_fn = organic.keep_fresh_links
            num_links = organic.organic_length

            # If prefs allow it, mix in promoted links and sr discovery content
            if c.user.pref_show_sponsors or not c.user.gold:
                if g.live_config['sr_discovery_links']:
                    spotlight_links.extend(g.live_config['sr_discovery_links'])
                    random.shuffle(spotlight_links)
                    spotlight_keep_fn = lambda l: promote.is_promo(
                        l) or organic.keep_fresh_links(l)
                    num_links = len(spotlight_links)
                spotlight_links, pos, campaigns_by_link = promote.insert_promoted(
                    spotlight_links, pos)

            # Need to do this again, because if there was a duplicate removed,
            # pos might be pointing outside the list.
            if not spotlight_links:
                pos = 0
            elif pos != 0:
                pos = pos % len(spotlight_links)

            if not spotlight_links:
                return None

            # get links in proximity to pos
            num_tl = len(spotlight_links)
            if num_tl <= 3:
                disp_links = spotlight_links
            else:
                left_side = max(-1, min(num_tl - 3, 8))
                disp_links = [
                    spotlight_links[(i + pos) % num_tl]
                    for i in xrange(-2, left_side)
                ]

            b = IDBuilder(disp_links,
                          wrap=self.builder_wrapper,
                          num=num_links,
                          keep_fn=spotlight_keep_fn,
                          skip=True)

            try:
                vislink = spotlight_links[pos]
            except IndexError:
                g.log.error("spotlight_links = %r" % spotlight_links)
                g.log.error("pos = %d" % pos)
                raise

            s = SpotlightListing(
                b,
                spotlight_items=spotlight_links,
                visible_item=vislink,
                max_num=self.listing_obj.max_num,
                max_score=self.listing_obj.max_score).listing()

            has_subscribed = c.user.has_subscribed
            promo_visible = promote.is_promo(s.lookup[vislink])
            if not promo_visible:
                prob = g.live_config[
                    'spotlight_interest_sub_p'
                    if has_subscribed else 'spotlight_interest_nosub_p']
                if random.random() < prob:
                    bar = InterestBar(has_subscribed)
                    s.spotlight_items.insert(pos, bar)
                    s.visible_item = bar

            if len(s.things) > 0:
                # only pass through a listing if the links made it
                # through our builder
                organic.update_pos(pos + 1)
                # add campaign id to promoted links for tracking
                for thing in s.things:
                    thing.campaign = campaigns_by_link.get(
                        thing._fullname, None)
                return s