本文整理汇总了Python中r2.lib.template_helpers.add_sr函数的典型用法代码示例。如果您正苦于以下问题:Python add_sr函数的具体用法?Python add_sr怎么用?Python add_sr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了add_sr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GET_s
def GET_s(self, urloid):
"""/s/http://..., show a given URL with the toolbar. if it's
submitted, redirect to /tb/$id36"""
force_html()
path = demangle_url(request.fullpath)
if not path:
# it was malformed
self.abort404()
# if the domain is shame-banned, bail out.
if is_shamed_domain(path)[0]:
self.abort404()
listing = hot_links_by_url_listing(path, sr=c.site, num=1)
link = listing.things[0] if listing.things else None
if link:
# we were able to find it, let's send them to the
# toolbar (if enabled) or comments (if not)
return self.redirect(add_sr("/tb/" + link._id36))
else:
# It hasn't been submitted yet. Give them a chance to
qs = utils.query_string({"url": path})
return self.redirect(add_sr("/submit" + qs))
示例2: __init__
def __init__(self, *args, **kwargs):
self.base_url = add_sr(
"/live/" + c.liveupdate_event._id,
force_hostname=True,
force_https=c.secure,
)
super(LiveUpdateEventEmbed, self).__init__(*args, **kwargs)
示例3: GET_wiki_discussions
def GET_wiki_discussions(self, page, num, after, reverse, count):
page_url = add_sr("%s/%s" % (c.wiki_base_url, page.name))
builder = url_links_builder(page_url, num=num, after=after,
reverse=reverse, count=count)
listing = LinkListing(builder).listing()
return WikiDiscussions(listing, page=page.name,
may_revise=this_may_revise(page)).render()
示例4: url_links_builder
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 = 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
示例5: GET_s
def GET_s(self, urloid):
"""/s/http://..., show a given URL with the toolbar. if it's
submitted, redirect to /tb/$id36"""
force_html()
path = demangle_url(request.fullpath)
if not path:
# it was malformed
self.abort404()
# if the domain is shame-banned, bail out.
if is_shamed_domain(path)[0]:
self.abort404()
listing = hot_links_by_url_listing(path, sr=c.site, num=1)
link = listing.things[0] if listing.things else None
if c.cname and not c.authorized_cname:
# In this case, we make some bad guesses caused by the
# cname frame on unauthorised cnames.
# 1. User types http://foo.com/http://myurl?cheese=brie
# (where foo.com is an unauthorised cname)
# 2. We generate a frame that points to
# http://www.reddit.com/r/foo/http://myurl?cnameframe=0.12345&cheese=brie
# 3. Because we accept everything after the /r/foo/, and
# we've now parsed, modified, and reconstituted that
# URL to add cnameframe, we really can't make any good
# assumptions about what we've done to a potentially
# already broken URL, and we can't assume that we've
# rebuilt it in the way that it was originally
# submitted (if it was)
# We could try to work around this with more guesses (by
# having demangle_url try to remove that param, hoping
# that it's not already a malformed URL, and that we
# haven't re-ordered the GET params, removed
# double-slashes, etc), but for now, we'll just refuse to
# do this operation
return self.abort404()
if link:
# we were able to find it, let's send them to the
# toolbar (if enabled) or comments (if not)
return self.redirect(add_sr("/tb/" + link._id36))
else:
# It hasn't been submitted yet. Give them a chance to
qs = utils.query_string({"url": path})
return self.redirect(add_sr("/submit?" + qs))
示例6: update_creative
def update_creative(link, az_advertiser):
"""Add/update a reddit link as an Adzerk Creative"""
if getattr(link, 'external_creative_id', None) is not None:
az_creative = adzerk_api.Creative.get(link.external_creative_id)
else:
az_creative = None
title = link._fullname
url = add_sr(link.url, sr_path=False) if link.is_self else link.url
# protocols are case sensitive (lower) in adzerk.
# can cause double protocols:
# http://Http://www.example.com
url = re.sub(r"^(https?)", lambda m: m.group(0).lower(), url, flags=re.I)
# as long as there are no 3rd party trackers for the link
# it's DNT compliant.
DNT_compliant = (not (hasattr(link, 'third_party_tracking_url') or
hasattr(link, 'third_party_tracking_url_2')))
d = {
'Body': title,
'ScriptBody': render_link(link),
'AdTypeId': LEADERBOARD_AD_TYPE,
'Alt': '',
'Url': url,
'IsHTMLJS': True,
'IsSync': False,
'IsDeleted': False,
'IsActive': not link._deleted,
'IsNoTrack': DNT_compliant,
}
if az_creative:
changed = update_changed(az_creative, **d)
change_strs = make_change_strings(changed)
if change_strs:
log_text = 'updated %s: ' % az_creative + ', '.join(change_strs)
else:
log_text = None
else:
d.update({
'AdvertiserId': az_advertiser.Id,
'Title': title,
})
try:
az_creative = adzerk_api.Creative.create(**d)
except:
raise ValueError(d)
link.external_creative_id = az_creative.Id
link._commit()
log_text = 'created %s' % az_creative
if log_text:
PromotionLog.add(link, log_text)
g.log.info(log_text)
return az_creative
示例7: GET_wiki_discussions
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()
示例8: _get_related_link_ids
def _get_related_link_ids(cls, event_id):
url = add_sr("/live/%s" % event_id, sr_path=False, force_hostname=True)
try:
links = tup(Link._by_url(url, sr=None))
except NotFound:
links = []
return [link._id for link in links]
示例9: __init__
def __init__(self):
links = self.get_links(c.liveupdate_event._id)
self.more_links = len(links) > self.max_links
self.links = links[:self.max_links]
self.submit_url = "/submit?" + urllib.urlencode({
"url": add_sr("/live/" + c.liveupdate_event._id,
sr_path=False, force_hostname=True),
"title": c.liveupdate_event.title,
})
Templated.__init__(self)
示例10: GET_s
def GET_s(self, rest):
"""/s/http://..., show a given URL with the toolbar. if it's
submitted, redirect to /tb/$id36"""
force_html()
path = demangle_url(request.fullpath)
if not path:
# it was malformed
self.abort404()
# if the domain is shame-banned, bail out.
if is_shamed_domain(path)[0]:
self.abort404()
listing = hot_links_by_url_listing(path, sr=c.site, num=1)
link = listing.things[0] if listing.things else None
if c.cname and not c.authorized_cname:
# In this case, we make some bad guesses caused by the
# cname frame on unauthorised cnames.
# 1. User types http://foo.com/http://myurl?cheese=brie
# (where foo.com is an unauthorised cname)
# 2. We generate a frame that points to
# http://www.reddit.com/r/foo/http://myurl?cnameframe=0.12345&cheese=brie
# 3. Because we accept everything after the /r/foo/, and
# we've now parsed, modified, and reconstituted that
# URL to add cnameframe, we really can't make any good
# assumptions about what we've done to a potentially
# already broken URL, and we can't assume that we've
# rebuilt it in the way that it was originally
# submitted (if it was)
# We could try to work around this with more guesses (by
# having demangle_url try to remove that param, hoping
# that it's not already a malformed URL, and that we
# haven't re-ordered the GET params, removed
# double-slashes, etc), but for now, we'll just refuse to
# do this operation
return self.abort404()
if link:
# we were able to find it, let's send them to the
# link-id-based URL so that their URL is reusable
return self.redirect(add_sr("/tb/" + link._id36))
title = utils.domain(path)
res = Frame(
title=title,
url=match_current_reddit_subdomain(path),
)
# we don't want clients to think that this URL is actually a
# valid URL for search-indexing or the like
request.environ['usable_error_content'] = spaceCompress(res.render())
abort(404)
示例11: valid_feed
def valid_feed(name, feedhash, path):
if name and feedhash and path:
from r2.lib.template_helpers import add_sr
path = add_sr(path)
try:
user = Account._by_name(name)
if (user.pref_private_feeds and
constant_time_compare(feedhash, make_feedhash(user, path))):
return user
except NotFound:
pass
示例12: intermediate_redirect
def intermediate_redirect(cls, form_path):
"""
Generates a /login or /over18 redirect from the current
fullpath, after having properly reformated the path via
format_output_url. The reformatted original url is encoded
and added as the "dest" parameter of the new url.
"""
from r2.lib.template_helpers import add_sr
dest = cls.format_output_url(request.fullpath)
path = add_sr(form_path + query_string({"dest": dest}))
return cls.redirect(path)
示例13: intermediate_redirect
def intermediate_redirect(cls, form_path, sr_path=True, fullpath=None):
"""
Generates a /login or /over18 redirect from the specified or current
fullpath, after having properly reformated the path via
format_output_url. The reformatted original url is encoded
and added as the "dest" parameter of the new url.
"""
from r2.lib.template_helpers import add_sr
params = dict(dest=cls.format_output_url(fullpath or request.fullurl))
if c.extension == "widget" and request.GET.get("callback"):
params['callback'] = request.GET.get("callback")
path = add_sr(cls.format_output_url(form_path) +
query_string(params), sr_path=sr_path)
abort(302, location=path)
示例14: intermediate_redirect
def intermediate_redirect(cls, form_path):
"""
Generates a /login or /over18 redirect from the current
fullpath, after having properly reformated the path via
format_output_url. The reformatted original url is encoded
and added as the "dest" parameter of the new url.
"""
from r2.lib.template_helpers import add_sr
params = dict(dest=cls.format_output_url(request.fullpath))
if c.extension == "widget" and request.GET.get("callback"):
params["callback"] = request.GET.get("callback")
path = add_sr(cls.format_output_url(form_path) + query_string(params))
return cls.redirect(path)
示例15: GET_urloid
def GET_urloid(self, urloid):
# they got here from "/http://..."
path = demangle_url(request.fullpath)
if not path:
# malformed URL
self.abort404()
redir_path = add_sr("/s/" + path)
force_html()
# Redirect to http://reddit.com/s/http://google.com
# rather than http://reddit.com/s/http:/google.com
redir_path = self.slash_fixer.sub(r'\1///', redir_path, 1)
# ^^^
# 3=2 when it comes to self.redirect()
return self.redirect(redir_path)