本文整理汇总了Python中r2.lib.hooks.get_hook函数的典型用法代码示例。如果您正苦于以下问题:Python get_hook函数的具体用法?Python get_hook怎么用?Python get_hook使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_hook函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cast_vote
def cast_vote(user, thing, direction, **data):
"""Register a vote and queue it for processing."""
update_vote_lookups(user, thing, direction)
vote_data = {
"user_id": user._id,
"thing_fullname": thing._fullname,
"direction": direction,
"date": int(epoch_timestamp(datetime.now(g.tz))),
}
data['ip'] = getattr(request, "ip", None)
if data['ip'] is not None:
data['org'] = organization_by_ips(data['ip'])
vote_data['data'] = data
hooks.get_hook("vote.get_vote_data").call(
data=vote_data["data"],
user=user,
thing=thing,
request=request,
context=c,
)
# The vote event will actually be sent from an async queue processor, so
# we need to pull out the context data at this point
if not g.running_as_script:
vote_data["event_data"] = {
"context": Event.get_context_data(request, c),
"sensitive": Event.get_sensitive_context_data(request, c),
}
amqp.add_item(thing.vote_queue_name, json.dumps(vote_data))
示例2: use
def use(self):
hooks.get_hook("js_preload.use").call(js_preload=self)
if self.data:
return js.DataSource.use(self)
else:
return ''
示例3: void_campaign
def void_campaign(link, campaign):
transactions = get_transactions(link, [campaign])
bid_record = transactions.get(campaign._id)
if bid_record:
a = Account._byID(link.author_id)
authorize.void_transaction(a, bid_record.transaction, campaign._id)
hooks.get_hook('campaign.void').call(link=link, campaign=campaign)
示例4: accept_promotion
def accept_promotion(link):
"""
Accepting is campaign agnostic. Accepting the ad just means that
it is allowed to run if payment has been processed.
If a campagn is able to run, this also requeues it.
"""
PromotionLog.add(link, 'status update: accepted')
# update the query queue
set_promote_status(link, PROMOTE_STATUS.accepted)
# campaigns that should be live now must be updated
now = promo_datetime_now(0)
if link._fullname in set(l.thing_name for l in
PromotionWeights.get_campaigns(now)):
PromotionLog.add(link, 'Marked promotion for acceptance')
charge_pending(0) # campaign must be charged before it will go live
queue_changed_promo(link, "accepted")
# campaigns that were charged and will go live in the future must be updated
future_campaigns = [camp for camp in PromoCampaign._by_link(link._id)
if camp.start_date > now]
transactions = get_transactions(link, future_campaigns)
charged_campaigns = [camp for camp in future_campaigns
if (transactions.get(camp._id) and
transactions.get(camp._id).is_charged())]
for campaign in charged_campaigns:
hooks.get_hook('campaign.edit').call(link=link, campaign=campaign)
if link._spam:
link._spam = False
link._commit()
emailer.accept_promo(link)
示例5: charge_pending
def charge_pending(offset=1):
for l, camp, weight in accepted_campaigns(offset=offset):
user = Account._byID(l.author_id)
try:
if charged_or_not_needed(camp):
continue
charge_succeeded = authorize.charge_transaction(user, camp.trans_id,
camp._id)
if not charge_succeeded:
continue
hooks.get_hook('promote.new_charge').call(link=l, campaign=camp)
if is_promoted(l):
emailer.queue_promo(l, camp.bid, camp.trans_id)
else:
set_promote_status(l, PROMOTE_STATUS.pending)
emailer.queue_promo(l, camp.bid, camp.trans_id)
text = ('auth charge for campaign %s, trans_id: %d' %
(camp._id, camp.trans_id))
PromotionLog.add(l, text)
except:
print "Error on %s, campaign %s" % (l, camp._id)
示例6: charge_campaign
def charge_campaign(link, campaign):
if charged_or_not_needed(campaign):
return
user = Account._byID(link.author_id)
success, reason = authorize.charge_transaction(user, campaign.trans_id,
campaign._id)
if not success:
if reason == authorize.TRANSACTION_NOT_FOUND:
# authorization hold has expired
original_trans_id = campaign.trans_id
campaign.trans_id = NO_TRANSACTION
campaign._commit()
text = ('voided expired transaction for %s: (trans_id: %d)'
% (campaign, original_trans_id))
PromotionLog.add(link, text)
return
hooks.get_hook('promote.edit_campaign').call(link=link, campaign=campaign)
if not is_promoted(link):
update_promote_status(link, PROMOTE_STATUS.pending)
emailer.queue_promo(link, campaign.bid, campaign.trans_id)
text = ('auth charge for campaign %s, trans_id: %d' %
(campaign._id, campaign.trans_id))
PromotionLog.add(link, text)
示例7: POST_update
def POST_update(self, form, jquery, text):
if form.has_errors("body", errors.NO_TEXT,
errors.TOO_LONG):
return
# create and store the new update
update = LiveUpdate(data={
"author_id": c.user._id,
"body": text,
"_spam": c.user._spam,
})
hooks.get_hook("liveupdate.update").call(update=update)
LiveUpdateStream.add_update(c.liveupdate_event, update)
# tell the world about our new update
builder = LiveUpdateBuilder(None)
wrapped = builder.wrap_items([update])[0]
rendered = wrapped.render(style="api")
_broadcast(type="update", payload=rendered)
# Queue up parsing any embeds
queue_parse_embeds(c.liveupdate_event, update)
# reset the submission form
t = form.find("textarea")
t.attr('rows', 3).html("").val("")
示例8: apply_effects
def apply_effects(self):
"""Apply the effects of the vote to the thing that was voted on."""
# remove the old vote
if self.previous_vote and self.previous_vote.affected_thing_attr:
self.thing._incr(self.previous_vote.affected_thing_attr, -1)
if (self.previous_vote and not self.previous_vote.effects.affects_score and
self.previous_vote.is_self_vote):
self.thing._incr('_ups', -1)
# add the new vote
if self.affected_thing_attr:
self.thing._incr(self.affected_thing_attr, 1)
if self.effects.affects_karma:
change = self.effects.karma_change
if self.previous_vote:
change -= self.previous_vote.effects.karma_change
if change:
self.thing.author_slow.incr_karma(
kind=self.thing.affects_karma_type,
sr=self.thing.subreddit_slow,
amt=change,
)
hooks.get_hook("vote.apply_effects").call(vote=self)
示例9: accept_promotion
def accept_promotion(link):
"""
Accepting is campaign agnostic. Accepting the ad just means that
it is allowed to run if payment has been processed.
If a campagn is able to run, this also requeues it.
"""
# update the query queue
set_promote_status(link, PROMOTE_STATUS.accepted)
# campaigns that should be live now must be updated
now = promo_datetime_now(0)
promotion_weights = PromotionWeights.get_campaigns(now)
live_campaigns = {pw.promo_idx for pw in promotion_weights if pw.thing_name == link._fullname}
if live_campaigns:
campaigns = PromoCampaign._byID(live_campaigns, data=True, return_dict=False)
PromotionLog.add(link, "has live campaigns, forcing live")
charge_pending(0) # campaign must be charged before it will go live
for campaign in campaigns:
hooks.get_hook("campaign.edit").call(link=link, campaign=campaign)
queue_changed_promo(link, "accepted")
# campaigns that were charged and will go live in the future must be updated
future_campaigns = [camp for camp in PromoCampaign._by_link(link._id) if camp.start_date > now]
transactions = get_transactions(link, future_campaigns)
charged_campaigns = [
camp for camp in future_campaigns if (transactions.get(camp._id) and transactions.get(camp._id).is_charged())
]
for campaign in charged_campaigns:
hooks.get_hook("campaign.edit").call(link=link, campaign=campaign)
if link._spam:
link._spam = False
link._commit()
emailer.accept_promo(link)
示例10: edit_campaign
def edit_campaign(link, campaign, dates, bid, cpm, sr, priority):
sr_name = sr.name if sr else "" # empty string means target to all
try:
# if the bid amount changed, cancel any pending transactions
if campaign.bid != bid:
void_campaign(link, campaign)
# update the schedule
PromotionWeights.reschedule(link, campaign._id, sr_name, dates[0], dates[1], bid)
# update values in the db
campaign.update(dates[0], dates[1], bid, cpm, sr_name, campaign.trans_id, priority, commit=True)
if campaign.priority.cpm:
# record the transaction
text = "updated campaign %s. (bid: %0.2f)" % (campaign._id, bid)
PromotionLog.add(link, text)
# make it a freebie, if applicable
author = Account._byID(link.author_id, True)
if getattr(author, "complimentary_promos", False):
free_campaign(link, campaign, c.user)
hooks.get_hook("campaign.edit").call(link=link, campaign=campaign)
except Exception, e: # record error and rethrow
g.log.error("Failed to update PromoCampaign %s on link %d. Error was: %r" % (campaign._id, link._id, e))
try: # wrapped in try/except so orig error won't be lost if commit fails
text = "update FAILED. (campaign: %s, bid: %.2f)" % (campaign._id, bid)
PromotionLog.add(link, text)
except:
pass
raise e
示例11: accept
def accept(cls, things, correct=True):
from r2.lib.db import queries
things = tup(things)
things_by_cls = {}
for thing in things:
things_by_cls.setdefault(thing.__class__, []).append(thing)
for thing_cls, cls_things in things_by_cls.iteritems():
to_clear = []
# look up all of the reports for each thing
rel_cls = cls.rel(Account, thing_cls)
thing_ids = [t._id for t in cls_things]
rels = list(rel_cls._query(rel_cls.c._thing2_id == thing_ids))
for r in rels:
if r._name == '0':
r._name = '1' if correct else '-1'
r._commit()
for thing in cls_things:
if thing.reported > 0:
thing.reported = 0
thing._commit()
to_clear.append(thing)
queries.clear_reports(to_clear, rels)
if correct:
hooks.get_hook("report.accept").call(reports=rels)
else:
hooks.get_hook("report.reject").call(reports=rels)
示例12: edit_campaign
def edit_campaign(link, campaign, dates, bid, cpm, sr, priority):
sr_name = sr.name if sr else '' # empty string means target to all
# if the bid amount changed, cancel any pending transactions
if campaign.bid != bid:
void_campaign(link, campaign)
# update the schedule
PromotionWeights.reschedule(link, campaign._id, sr_name,
dates[0], dates[1], bid)
# update values in the db
campaign.update(dates[0], dates[1], bid, cpm, sr_name,
campaign.trans_id, priority, commit=True)
if campaign.priority.cpm:
# record the transaction
text = 'updated campaign %s. (bid: %0.2f)' % (campaign._id, bid)
PromotionLog.add(link, text)
# make it a freebie, if applicable
author = Account._byID(link.author_id, True)
if getattr(author, "complimentary_promos", False):
free_campaign(link, campaign, c.user)
hooks.get_hook('promote.edit_campaign').call(link=link, campaign=campaign)
示例13: toggle_pause_campaign
def toggle_pause_campaign(link, campaign, should_pause):
campaign.paused = should_pause
campaign._commit()
action = "paused" if should_pause else "resumed"
PromotionLog.add(link, "%s campaign %s" % (action, campaign._id))
hooks.get_hook("promote.edit_campaign").call(link=link, campaign=campaign)
示例14: _on_create
def _on_create(self):
hooks.get_hook("oauth2.create_token").call(token=self)
# update the by-user view
if self.user_id:
self._by_user_view()._set_values(str(self.user_id), {self._id: ''})
return super(OAuth2AccessToken, self)._on_create()
示例15: js_config
def js_config(extra_config=None):
logged = c.user_is_loggedin and c.user.name
gold = bool(logged and c.user.gold)
config = {
# is the user logged in?
"logged": logged,
# the subreddit's name (for posts)
"post_site": c.site.name if not c.default_sr else "",
# the user's voting hash
"modhash": c.modhash or False,
# the current rendering style
"renderstyle": c.render_style,
# they're welcome to try to override this in the DOM because we just
# disable the features server-side if applicable
'store_visits': gold and c.user.pref_store_visits,
# current domain
"cur_domain": get_domain(cname=c.frameless_cname, subreddit=False, no_www=True),
# where do ajax requests go?
"ajax_domain": get_domain(cname=c.authorized_cname, subreddit=False),
"extension": c.extension,
"https_endpoint": is_subdomain(request.host, g.domain) and g.https_endpoint,
# does the client only want to communicate over HTTPS?
"https_forced": c.user.https_forced,
# debugging?
"debug": g.debug,
"send_logs": g.live_config["frontend_logging"],
"server_time": math.floor(time.time()),
"status_msg": {
"fetching": _("fetching title..."),
"submitting": _("submitting..."),
"loading": _("loading...")
},
"is_fake": isinstance(c.site, FakeSubreddit),
"fetch_trackers_url": g.fetch_trackers_url,
"adtracker_url": g.adtracker_url,
"clicktracker_url": g.clicktracker_url,
"uitracker_url": g.uitracker_url,
"static_root": static(''),
"over_18": bool(c.over18),
"new_window": bool(c.user.pref_newwindow),
"vote_hash": c.vote_hash,
"gold": gold,
"has_subscribed": logged and c.user.has_subscribed,
}
if g.uncompressedJS:
config["uncompressedJS"] = True
if extra_config:
config.update(extra_config)
hooks.get_hook("js_config").call(config=config)
return config