示例#1
0
    def get_wrapped_link(self, url, link=None, wrapper=None):
        try:
            links = []
            if link:
                links = [link]
            else:
                sr = None if isinstance(c.site, FakeSubreddit) else c.site
                try:
                    links = Link._by_url(url, sr)
                except NotFound:
                    pass

            if links:
                kw = {}
                if wrapper:
                    links = wrap_links(links, wrapper=wrapper)
                else:
                    links = wrap_links(links)
                links = list(links)
                links = max(links, key=lambda x: x._score) if links else None
            if not links and wrapper:
                return wrapper(None)
            return links
            # note: even if _by_url successed or a link was passed in,
            # it is possible link_listing.things is empty if the
            # link(s) is/are members of a private reddit
            # return the link with the highest score (if more than 1)
        except:
            #we don't want to return 500s in other people's pages.
            import traceback
            g.log.debug("FULLPATH: get_link error in buttons code")
            g.log.debug(traceback.format_exc())
            if wrapper:
                return wrapper(None)
示例#2
0
    def get_wrapped_link(self, url, link=None, wrapper=None):
        try:
            links = []
            if link:
                links = [link]
            else:
                sr = None if isinstance(c.site, FakeSubreddit) else c.site
                try:
                    links = Link._by_url(url, sr)
                except NotFound:
                    pass

            if links:
                kw = {}
                if wrapper:
                    links = wrap_links(links, wrapper=wrapper)
                else:
                    links = wrap_links(links)
                links = list(links)
                links = max(links, key=lambda x: x._score) if links else None
            if not links and wrapper:
                return wrapper(None)
            return links
            # note: even if _by_url successed or a link was passed in,
            # it is possible link_listing.things is empty if the
            # link(s) is/are members of a private reddit
            # return the link with the highest score (if more than 1)
        except:
            # we don't want to return 500s in other people's pages.
            import traceback

            g.log.debug("FULLPATH: get_link error in buttons code")
            g.log.debug(traceback.format_exc())
            if wrapper:
                return wrapper(None)
示例#3
0
    def GET_depmod(self):
        displayPane = PaneStack()

        active_trials = {}
        finished_trials = {}

        juries = Jury.by_account(c.user)

        trials = trial_info([j._thing2 for j in juries])

        for j in juries:
            defendant = j._thing2

            if trials.get(defendant._fullname, False):
                active_trials[defendant._fullname] = j._name
            else:
                finished_trials[defendant._fullname] = j._name

        if active_trials:
            fullnames = sorted(active_trials.keys(), reverse=True)

            def my_wrap(thing):
                w = Wrapped(thing)
                w.hide_score = True
                w.likes = None
                w.trial_mode = True
                w.render_class = LinkOnTrial
                w.juryvote = active_trials[thing._fullname]
                return w

            listing = wrap_links(fullnames, wrapper=my_wrap)
            displayPane.append(
                InfoBar(strings.active_trials, extra_class="mellow"))
            displayPane.append(listing)

        if finished_trials:
            fullnames = sorted(finished_trials.keys(), reverse=True)
            listing = wrap_links(fullnames)
            displayPane.append(
                InfoBar(strings.finished_trials, extra_class="mellow"))
            displayPane.append(listing)

        displayPane.append(
            InfoBar(
                strings.more_info_link % dict(link="/help/deputies"),
                extra_class="mellow"))

        return Reddit(content=displayPane).render()
示例#4
0
文件: front.py 项目: jesvs/reddit
    def GET_submit(self, url, title, then):
        """Submit form."""
        resubmit = request.get.get('resubmit')
        if url and not resubmit:
            # check to see if the url has already been submitted
            links = link_from_url(url)
            if links and len(links) == 1:
                return self.redirect(links[0].already_submitted_link)
            elif links:
                infotext = (strings.multiple_submitted
                            % links[0].resubmit_link())
                res = BoringPage(_("seen it"),
                                 content = wrap_links(links),
                                 infotext = infotext).render()
                return res

        if not c.user_is_loggedin:
            raise UserRequiredException

        if not (c.default_sr or c.site.can_submit(c.user)):
            abort(403, "forbidden")

        captcha = Captcha() if c.user.needs_captcha() else None
        sr_names = (Subreddit.submit_sr_names(c.user) or
                    Subreddit.submit_sr_names(None))

        return FormPage(_("submit"),
                        show_sidebar = True,
                        content=NewLink(url=url or '',
                                        title=title or '',
                                        subreddits = sr_names,
                                        captcha=captcha,
                                        resubmit=resubmit,
                                        then = then)).render()
示例#5
0
    def GET_submit(self, url, title, then):
        """Submit form."""
        if url and not request.get.get('resubmit'):
            # check to see if the url has already been submitted
            links = link_from_url(url)
            if links and len(links) == 1:
                return self.redirect(links[0].already_submitted_link)
            elif links:
                infotext = (strings.multiple_submitted
                            % links[0].resubmit_link())
                res = BoringPage(_("seen it"),
                                 content = wrap_links(links),
                                 infotext = infotext).render()
                return res

        captcha = Captcha() if c.user.needs_captcha() else None
        sr_names = (Subreddit.submit_sr_names(c.user) or
                    Subreddit.submit_sr_names(None))

        return FormPage(_("submit"),
                        show_sidebar = True,
                        content=NewLink(url=url or '',
                                        title=title or '',
                                        subreddits = sr_names,
                                        captcha=captcha,
                                        then = then)).render()
示例#6
0
文件: front.py 项目: Falmarri/reddit
    def GET_submit(self, url, title, then):
        """Submit form."""
        resubmit = request.get.get('resubmit')
        if url and not resubmit:
            # check to see if the url has already been submitted
            links = link_from_url(url)
            if links and len(links) == 1:
                return self.redirect(links[0].already_submitted_link)
            elif links:
                infotext = (strings.multiple_submitted
                            % links[0].resubmit_link())
                res = BoringPage(_("seen it"),
                                 content = wrap_links(links),
                                 infotext = infotext).render()
                return res

        if not c.user_is_loggedin:
            raise UserRequiredException

        if not (c.default_sr or c.site.can_submit(c.user)):
            abort(403, "forbidden")

        captcha = Captcha() if c.user.needs_captcha() else None
        sr_names = (Subreddit.submit_sr_names(c.user) or
                    Subreddit.submit_sr_names(None))

        return FormPage(_("submit"),
                        show_sidebar = True,
                        page_classes=['submit-page'],
                        content=NewLink(url=url or '',
                                        title=title or '',
                                        subreddits = sr_names,
                                        captcha=captcha,
                                        resubmit=resubmit,
                                        then = then)).render()
示例#7
0
    def GET_comments(self, link):
        if not link:
            self.abort404()
        if not link.subreddit_slow.can_view(c.user):
            abort(403, 'forbidden')

        links = list(wrap_links(link))
        if not links:
            # they aren't allowed to see this link
            return abort(403, 'forbidden')
        link = links[0]

        wrapper = make_wrapper(render_class = StarkComment,
                               target = "_top")
        b = TopCommentBuilder(link, CommentSortMenu.operator('confidence'),
                              num=10, wrap=wrapper)

        listing = NestedListing(b, parent_name=link._fullname)

        raw_bar = strings.comments_panel_text % dict(
            fd_link=link.permalink)

        md_bar = safemarkdown(raw_bar, target="_top")

        res = RedditMin(content=CommentsPanel(link=link,
                                              listing=listing.listing(),
                                              expanded=auto_expand_panel(link),
                                              infobar=md_bar))

        return res.render()
示例#8
0
 def GET_edit_promo(self, link):
     if not link or link.promoted is None:
         return self.abort404()
     rendered = wrap_links(link, skip=False)
     form = PromoteLinkForm(link, rendered)
     page = PromotePage('new_promo', content=form)
     return page.render()
示例#9
0
 def GET_edit_promo(self, link):
     if not link or link.promoted is None:
         return self.abort404()
     rendered = wrap_links(link, skip=False)
     form = PromoteLinkEdit(link, rendered)
     page = PromotePage(title=_("edit sponsored link"), content=form, show_sidebar=False, extension_handling=False)
     return page.render()
示例#10
0
 def GET_edit_promo(self, link):
     if not link or link.promoted is None:
         return self.abort404()
     rendered = wrap_links(link, wrapper=promote.sponsor_wrapper, skip=False)
     form = PromoteLinkForm(link, rendered)
     page = PromotePage('new_promo', content=form)
     return page.render()
示例#11
0
    def query(self):
        if isinstance(c.site, DefaultSR):
            if c.user_is_loggedin:
                srlimit = Subreddit.DEFAULT_LIMIT
                over18 = c.user.has_subscribed and c.over18
            else:
                srlimit = g.num_default_reddits
                over18 = False

            sr_ids = Subreddit.user_subreddits(c.user,
                                               limit=srlimit,
                                               over18=over18)
            return normalized_hot(sr_ids)

        elif isinstance(c.site, MultiReddit):
            return normalized_hot(c.site.kept_sr_ids, obey_age_limit=False)
        else:
            if c.site.sticky_fullname:
                link_list = [c.site.sticky_fullname]
                wrapped = wrap_links(link_list,
                                     wrapper=self.builder_wrapper,
                                     keep_fn=self.keep_fn(),
                                     skip=True)
                # add all other items and decrement count if sticky is visible
                if wrapped.things:
                    link_list += [l for l in c.site.get_links('hot', 'all')
                                    if l != c.site.sticky_fullname]
                    if not self.after:
                        self.count -= 1
                        self.num += 1
                    return link_list
            
            # no sticky or sticky hidden
            return c.site.get_links('hot', 'all')
示例#12
0
文件: toolbar.py 项目: AD42/reddit
    def GET_comments(self, link):
        if not link:
            self.abort404()
        if not link.subreddit_slow.can_view(c.user):
            abort(403, 'forbidden')

        links = list(wrap_links(link))
        if not links:
            # they aren't allowed to see this link
            return self.abort(403, 'forbidden')
        link = links[0]

        wrapper = make_wrapper(render_class = StarkComment,
                               target = "_top")
        b = TopCommentBuilder(link, CommentSortMenu.operator('confidence'),
                              wrap = wrapper)

        listing = NestedListing(b, num = 10, # TODO: add config var
                                parent_name = link._fullname)

        raw_bar = strings.comments_panel_text % dict(
            fd_link=link.permalink)

        md_bar = safemarkdown(raw_bar, target="_top")

        res = RedditMin(content=CommentsPanel(link=link,
                                              listing=listing.listing(),
                                              expanded=auto_expand_panel(link),
                                              infobar=md_bar))

        return res.render()
示例#13
0
    def GET_submit(self, url, title, then):
        """Submit form."""
        if url and not request.get.get('resubmit'):
            # check to see if the url has already been submitted
            links = link_from_url(url)
            if links and len(links) == 1:
                return self.redirect(links[0].already_submitted_link)
            elif links:
                infotext = (strings.multiple_submitted
                            % links[0].resubmit_link())
                res = BoringPage(_("seen it"),
                                 content = wrap_links(links),
                                 infotext = infotext).render()
                return res

        captcha = Captcha() if c.user.needs_captcha() else None
        sr_names = (Subreddit.submit_sr_names(c.user) or
                    Subreddit.submit_sr_names(None))

        return FormPage(_("submit"),
                        show_sidebar = True,
                        content=NewLink(url=url or '',
                                        title=title or '',
                                        subreddits = sr_names,
                                        captcha=captcha,
                                        then = then)).render()
示例#14
0
    def GET_toolbar(self, link, url):
        """The visible toolbar, with voting buttons and all"""
        if url:
            url = demangle_url(url)

        if link:
            wrapped = wrap_links(link, wrapper=FrameToolbar, num=1)
        elif url:
            listing = hot_links_by_url_listing(url, sr=c.site, num=1, skip=True)
            if listing.things:
                wrapped = listing.things[0]
            else:
                wrapped = None
        else:
            return self.abort404()

        res = None
        if wrapped:
            res = wrapped
        elif url:
            res = FrameToolbar(link=None, title=None, url=url, expanded=False)

        if res:
            return spaceCompress(res.render())
        else:
            return self.abort404()
示例#15
0
    def GET_depmod(self):
        displayPane = PaneStack()

        active_trials = {}
        finished_trials = {}

        juries = Jury.by_account(c.user)

        trials = trial_info([j._thing2 for j in juries])

        for j in juries:
            defendant = j._thing2

            if trials.get(defendant._fullname, False):
                active_trials[defendant._fullname] = j._name
            else:
                finished_trials[defendant._fullname] = j._name

        if active_trials:
            fullnames = sorted(active_trials.keys(), reverse=True)

            def my_wrap(thing):
                w = Wrapped(thing)
                w.hide_score = True
                w.likes = None
                w.trial_mode = True
                w.render_class = LinkOnTrial
                w.juryvote = active_trials[thing._fullname]
                return w

            listing = wrap_links(fullnames, wrapper=my_wrap)
            displayPane.append(InfoBar(strings.active_trials,
                                       extra_class="mellow"))
            displayPane.append(listing)

        if finished_trials:
            fullnames = sorted(finished_trials.keys(), reverse=True)
            listing = wrap_links(fullnames)
            displayPane.append(InfoBar(strings.finished_trials,
                                       extra_class="mellow"))
            displayPane.append(listing)

        displayPane.append(InfoBar(strings.more_info_link %
                                       dict(link="/help/deputies"),
                                   extra_class="mellow"))

        return Reddit(content = displayPane).render()
示例#16
0
 def GET_edit_promo(self, link):
     if not link or link.promoted is None:
         return self.abort404()
     rendered = wrap_links(link, skip=False)
     form = PromoteLinkEdit(link, rendered)
     page = PromotePage(title=_("edit sponsored link"), content=form,
                   show_sidebar=False, extension_handling=False)
     return page.render()
示例#17
0
def rendered_link(links, media, compress):
    with c.user.safe_set_attr:
        c.user.pref_compress = compress
        c.user.pref_media = media
    links = wrap_links(links, show_nums=True, num=1)
    delattr(c.user, 'pref_compress')
    delattr(c.user, 'pref_media')
    return links.render(style="html")
示例#18
0
def rendered_link(links, media, compress):
    with c.user.safe_set_attr:
        c.user.pref_compress = compress
        c.user.pref_media    = media
    links = wrap_links(links, show_nums = True, num = 1)
    delattr(c.user, 'pref_compress')
    delattr(c.user, 'pref_media') 
    return links.render(style = "html")
示例#19
0
 def _get_selfserve_links(self, count):
     links = Subreddit._by_name(g.advertising_links_sr).get_links(
         'new', 'all')
     items = Link._by_fullname(links, data=True, return_dict=False)
     id36s = map(
         lambda x: self.advertising_link_id36_re.match(x.url).group(1),
         items)
     ad_links = Link._byID36(id36s, return_dict=False, data=True)
     return wrap_links(ad_links, num=count)
示例#20
0
 def GET_current_promos(self):
     render_list = list(wrap_links(get_promoted()))
     for x in render_list:
         if x.promote_until:
             x.promote_expires = timetext(datetime.now(g.tz) - x.promote_until)
     page = PromotePage('current_promos',
                        content = PromotedLinks(render_list))
 
     return page.render()
示例#21
0
def rendered_link(links, media, compress, stickied=False):
    with c.user.safe_set_attr:
        c.user.pref_compress = compress
        c.user.pref_media = media
    links = wrap_links(links, show_nums=True, num=1)
    for wrapped in links:
        wrapped.stickied = stickied
    delattr(c.user, 'pref_compress')
    delattr(c.user, 'pref_media')
    return links.render(style="html")
示例#22
0
    def GET_edit_promo(self, link):
        if not link or link.promoted is None:
            return self.abort404()
        rendered = wrap_links(link, wrapper=promote.sponsor_wrapper, skip=False)

        form = PromoteLinkForm(link=link, listing=rendered, timedeltatext="")

        page = PromotePage("new_promo", content=form)

        return page.render()
示例#23
0
def rendered_link(links, media, compress, stickied=False):
    with c.user.safe_set_attr:
        c.user.pref_compress = compress
        c.user.pref_media    = media
    links = wrap_links(links, show_nums = True, num = 1)
    for wrapped in links:
        wrapped.stickied = stickied
    delattr(c.user, 'pref_compress')
    delattr(c.user, 'pref_media') 
    return links.render(style = "html")
示例#24
0
    def GET_toolbar(self, link, url):
        """The visible toolbar, with voting buttons and all"""
        if url:
            url = demangle_url(url)

        if link:
            wrapped = wrap_links(link, wrapper=FrameToolbar, num=1)
        else:
            return self.abort404()

        return spaceCompress(wrapped.render())
示例#25
0
    def GET_edit_promo(self, link):
        if not link or link.promoted is None:
            return self.abort404()
        rendered = wrap_links(link,
                              wrapper=promote.sponsor_wrapper,
                              skip=False)

        form = PromoteLinkForm(link=link, listing=rendered, timedeltatext="")

        page = PromotePage('new_promo', content=form)

        return page.render()
示例#26
0
    def GET_toolbar(self, link, url):
        """The visible toolbar, with voting buttons and all"""
        if not link:
            link = utils.link_from_url(url, multiple=False)

        if link:
            link = list(wrap_links(link, wrapper=FrameToolbar))
        if link:
            res = link[0]
        elif url:
            res = FrameToolbar(link=None, title=None, url=url, expanded=False)
        else:
            self.abort404()
        return spaceCompress(res.render())
示例#27
0
    def GET_edit_promo(self, link):
        if link.promoted is None:
            return self.abort404()
        rendered = wrap_links(link)
        timedeltatext = ''
        if link.promote_until:
            timedeltatext = timetext(link.promote_until - datetime.now(g.tz),
                                     resultion=2)

        form = PromoteLinkForm(link = link,
                               listing = rendered,
                               timedeltatext = timedeltatext)
        page = PromotePage('new_promo', content = form)

        return page.render()
示例#28
0
    def GET_edit_promo(self, link):
        sr = Subreddit._byID(link.sr_id)
        listing = wrap_links(link)

        timedeltatext = ''
        if link.promote_until:
            timedeltatext = timetext(link.promote_until - datetime.now(g.tz),
                                     resultion=2)

        form = PromoteLinkForm(sr = sr, link = link,
                               listing = listing,
                               timedeltatext = timedeltatext)
        page = PromotePage('new_promo', content = form)

        return page.render()
示例#29
0
    def make_requested_ad(self):
        try:
            link = Link._by_fullname(self.requested_ad, data=True)
        except NotFound:
            self.abort404()

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

        if not promote.is_live_on_sr(link, c.site):
            self.abort403()

        res = wrap_links([link._fullname], wrapper=self.builder_wrapper, skip=False)
        res.parent_name = "promoted"
        if res.things:
            return res
示例#30
0
    def GET_trending(self):
        """
        Return a list of trending items.
        """
        if not g.partner_api_enable_rss:
            abort(403)

        if not c.extension or c.extension.lower() not in RSS_TYPES:
            abort(400, 'Only supports RSS and XML.')

        # TODO(wting#CHAN-158|2016-08-26): The number 5000 was chosen based on
        # emperical testing to increase link diversity timing out.
        num_of_links = 5000

        listing = wrap_links(all_sr.get_all_hot_ids(), num=num_of_links)
        return MinimalRss(_content=listing).render(style='xmllite')
示例#31
0
    def _things(self, things, action, *a, **kw):
        """
        function for inserting/replacing things in listings.
        """
        things = tup(things)
        if not all(isinstance(t, Wrapped) for t in things):
            wrap = kw.pop('wrap', Wrapped)
            things = wrap_links(things, wrapper = wrap)
        data = [self.process_rendered(t.render()) for t in things]

        if kw:
            for d in data:
                if d.has_key('data'):
                    d['data'].update(kw)

        self._data['things'] = data
        return data
示例#32
0
    def _things(self, things, action, *a, **kw):
        """
        function for inserting/replacing things in listings.
        """
        things = tup(things)
        if not all(isinstance(t, Wrapped) for t in things):
            wrap = kw.pop("wrap", Wrapped)
            things = wrap_links(things, wrapper=wrap)
        data = [t.render() for t in things]

        if kw:
            for d in data:
                if d.has_key("data"):
                    d["data"].update(kw)

        self._data["things"] = data
        return data
示例#33
0
    def GET_toolbar(self, link, url):
        """The visible toolbar, with voting buttons and all"""
        if not link:
            link = utils.link_from_url(url, multiple = False)

        if link:
            link = list(wrap_links(link, wrapper = FrameToolbar))
        if link:
            res = link[0]
        elif url:
            res = FrameToolbar(link = None,
                               title = None,
                               url = url,
                               expanded = False)
        else:
            self.abort404()
        return spaceCompress(res.render())
示例#34
0
    def _things(self, things, action, *a, **kw):
        """
        function for inserting/replacing things in listings.
        """
        things = tup(things)
        if not all(isinstance(t, Wrapped) for t in things):
            wrap = kw.pop('wrap', Wrapped)
            things = wrap_links(things, wrapper = wrap)
        data = [self.process_rendered(t.render()) for t in things]

        if kw:
            for d in data:
                if d.has_key('data'):
                    d['data'].update(kw)

        self._data['things'] = data
        return data
示例#35
0
    def make_requested_ad(self):
        try:
            link = Link._by_fullname(self.requested_ad, data=True)
        except NotFound:
            self.abort404()

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

        if not promote.is_live_on_sr(link, c.site.name):
            self.abort403()

        res = wrap_links([link._fullname], wrapper=self.builder_wrapper,
                         skip=False)
        res.parent_name = "promoted"
        if res.things:
            return res
示例#36
0
    def query(self):

        if isinstance(c.site, DefaultSR):
            sr_ids = Subreddit.user_subreddits(c.user)
            return normalized_hot(sr_ids)
        elif isinstance(c.site, MultiReddit):
            return normalized_hot(c.site.kept_sr_ids, obey_age_limit=False)
        else:
            if c.site.sticky_fullname:
                link_list = [c.site.sticky_fullname]
                wrapped = wrap_links(link_list, wrapper=self.builder_wrapper, keep_fn=self.keep_fn(), skip=True)
                # add all other items and decrement count if sticky is visible
                if wrapped.things:
                    link_list += [l for l in c.site.get_links("hot", "all") if l != c.site.sticky_fullname]
                    if not self.after:
                        self.count -= 1
                        self.num += 1
                    return link_list

            # no sticky or sticky hidden
            return c.site.get_links("hot", "all")
示例#37
0
    def query(self):
        #no need to worry when working from the cache
        # TODO: just remove this then since we're always using the query cache
        self.fix_listing = False

        if isinstance(c.site, DefaultSR):
            if c.user_is_loggedin:
                srlimit = Subreddit.DEFAULT_LIMIT
                over18 = c.user.has_subscribed and c.over18
            else:
                srlimit = g.num_default_reddits
                over18 = False

            sr_ids = Subreddit.user_subreddits(c.user,
                                               limit=srlimit,
                                               over18=over18)
            return normalized_hot(sr_ids)

        elif isinstance(c.site, MultiReddit):
            return normalized_hot(c.site.kept_sr_ids, obey_age_limit=False)
        else:
            if c.site.sticky_fullname:
                link_list = [c.site.sticky_fullname]
                wrapped = wrap_links(link_list,
                                     wrapper=self.builder_wrapper,
                                     keep_fn=self.keep_fn(),
                                     skip=True)
                # add all other items and decrement count if sticky is visible
                if wrapped.things:
                    link_list += [
                        l for l in c.site.get_links('hot', 'all')
                        if l != c.site.sticky_fullname
                    ]
                    if not self.after:
                        self.count -= 1
                        self.num += 1
                    return link_list

            # no sticky or sticky hidden
            return c.site.get_links('hot', 'all')
示例#38
0
    def POST_link_from_id(self, responder, external_id, *a, **kw):
        if (responder.has_errors("external_id", errors.BAD_NUMBER)):
            return

        link = LinksByExternalId.get(external_id)

        if not link:
            try:
                creative = creatives_service.by_id(external_id)
            except:
                creative = None

            if not creative:
                abort(404)

            link = _create_link(creative)

            LinksByExternalId.add(link)

        listing = wrap_links(link)
        thing = listing.things[0]

        return thing.render()
示例#39
0
    def POST_link_from_id(self, responder, dfp_creative_id, *a, **kw):
        if (responder.has_errors("dfp_creative_id", errors.BAD_NUMBER)):
            return

        link = LinksByDfpCreativeId.get(dfp_creative_id)

        if link:
            _check_edits(link)

        if not link:
            try:
                creative = creatives_service.by_id(dfp_creative_id)
            except Exception as e:
                g.log.error("dfp error: %s" % e)
                abort(404)

            link = utils.dfp_creative_to_link(creative)

            LinksByDfpCreativeId.add(link)

        listing = wrap_links([link])
        thing = listing.things[0]

        return thing.render()
    def POST_link_from_id(self, responder, dfp_creative_id, *a, **kw):
        if (responder.has_errors("dfp_creative_id", errors.BAD_NUMBER)):
            return

        link = LinksByDfpCreativeId.get(dfp_creative_id)

        if link:
            _check_edits(link)

        if not link:
            try:
                creative = creatives_service.by_id(dfp_creative_id)
            except Exception as e:
                g.log.error("dfp error: %s" % e)
                abort(404)

            link = utils.dfp_creative_to_link(creative)

            LinksByDfpCreativeId.add(link)

        listing = wrap_links([link])
        thing = listing.things[0]

        return thing.render()
示例#41
0
def rendered_comment(comments):
    return wrap_links(comments, num=1).render(style="html")
示例#42
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
示例#43
0
    def spotlight(self):
        """Build the Spotlight or a single promoted link.

        The frontpage gets a Spotlight box that contains promoted and organic
        links from the user's subscribed subreddits and promoted links targeted
        to the frontpage. Other subreddits get a single promoted link. In either
        case if the user has disabled ads promoted links will not be shown.

        The content of the Spotlight box is a bit tricky because a single
        version of the frontpage is cached and displayed to all logged out
        users. Because of the caching we must include many promoted links and
        select one to display on the client side. Otherwise, each logged out
        user would see the same promoted link and we would not get the desired
        distribution of promoted link views. Most of the promoted links are
        included as stubs to reduce the size of the page. When a promoted link
        stub is selected by the lottery the full link is fetched and displayed.

        There are only ~1000 cache resets per day so it is necessary to use
        a large subset of the eligible promoted links when choosing stubs for
        the Spotlight box. Using 100 stubs works great when there are fewer than
        100 possible promoted links and allows room for growth.

        """

        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=organic.keep_fresh_links,
                                 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))):

            organic_fullnames = organic.organic_links(c.user)
            promoted_links = []

            # 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']:
                    organic_fullnames.extend(g.live_config['sr_discovery_links'])

                n_promoted = 100
                n_build = 10
                promo_tuples = promote.get_promoted_links(c.user, c.site,
                                                          n_promoted)
                promo_tuples = sorted(promo_tuples,
                                      key=lambda p: p.weight,
                                      reverse=True)
                promo_build = promo_tuples[:n_build]
                promo_stub = promo_tuples[n_build:]
                b = CampaignBuilder(promo_build,
                                    wrap=self.builder_wrapper,
                                    keep_fn=promote.is_promoted)
                promoted_links = b.get_items()[0]
                promoted_links.extend(promo_stub)

            if not (organic_fullnames or promoted_links):
                return None

            random.shuffle(organic_fullnames)
            organic_fullnames = organic_fullnames[:10]
            b = IDBuilder(organic_fullnames,
                          wrap = self.builder_wrapper,
                          keep_fn = organic.keep_fresh_links,
                          skip = True)
            organic_links = b.get_items()[0]

            has_subscribed = c.user.has_subscribed
            interestbar_prob = g.live_config['spotlight_interest_sub_p'
                                             if has_subscribed else
                                             'spotlight_interest_nosub_p']
            interestbar = InterestBar(has_subscribed)
            promotion_prob = 0.5 if c.user_is_loggedin else 1.

            s = SpotlightListing(organic_links=organic_links,
                                 promoted_links=promoted_links,
                                 interestbar=interestbar,
                                 interestbar_prob=interestbar_prob,
                                 promotion_prob=promotion_prob,
                                 max_num = self.listing_obj.max_num,
                                 max_score = self.listing_obj.max_score).listing()
            return s
示例#44
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
示例#45
0
 def _get_selfserve_links(self, count):
     links = Subreddit._by_name(g.advertising_links_sr).get_links('new', 'all')
     items = Link._by_fullname(links, data=True, return_dict=False)
     id36s = map(lambda x: self.advertising_link_id36_re.match(x.url).group(1), items)
     ad_links = Link._byID36(id36s, return_dict=False, data=True)
     return wrap_links(ad_links, num=count)
示例#46
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
示例#47
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
示例#48
0
def rendered_comment(comments, gilded=False):
    wrapped = wrap_links(comments, num=1)
    if gilded:
        for w in wrapped:
            w.gilded_message = "this comment was fake-gilded"
    return wrapped.render(style="html")
示例#49
0
def rendered_comment(comments):
    return wrap_links(comments, num = 1).render(style = "html")
示例#50
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