def send_error(self, code, error, fields=None, explanation=None): abort( reddit_http_error( code=code or error.code, error_name=error, explanation=explanation, fields=tup(fields), ))
def handle_login( controller, form, responder, user, rem=None, signature=None, **kwargs ): # check captcha before login (if any) since its answer might # change once c.user is set. captcha_shown = not signature and need_provider_captcha("login") def _event(error, captcha_shown=captcha_shown): g.events.login_event( 'login_attempt', error_msg=error, user_name=request.urlvars.get('url_user'), remember_me=rem, signature=signature, captcha_shown=captcha_shown, request=request, context=c) if signature and not signature.is_valid(): _event(error="SIGNATURE") abort(403) hook_error = hooks.get_hook("account.login").call_until_return( responder=responder, request=request, context=c, ) # if any of the hooks returned an error, abort the login. The # set_error in this case also needs to exist in the hook. if hook_error: _event(error=hook_error) return exempt_ua = (request.user_agent and any(ua in request.user_agent for ua in g.config.get('exempt_login_user_agents', ()))) if (errors.LOGGED_IN, None) in c.errors: if user == c.user or exempt_ua: # Allow funky clients to re-login as the current user. c.errors.remove((errors.LOGGED_IN, None)) else: _event(error='LOGGED_IN') abort(reddit_http_error(409, errors.LOGGED_IN)) if responder.has_errors("ratelimit", errors.RATELIMIT): _event(error='RATELIMIT') elif responder.has_errors("passwd", errors.WRONG_PASSWORD): _event(error='WRONG_PASSWORD') # last but not least, we have to check the captcha elif (not signature and not g.disable_captcha and not valid_provider_captcha(responder, "login")): _event(error='BAD_CAPTCHA') else: controller._login(responder, user, rem) _event(error=None)
def abort_with_error(error, code=None): if not code and not error.code: raise ValueError("Error %r missing status code" % error) abort( reddit_http_error( code=code or error.code, error_name=error.name, explanation=error.message, fields=error.fields ) )
def on_validation_error(self, error): if not error.code: raise ValueError('Error %r missing status code' % error) abort(reddit_http_error( code=error.code, error_name=error.name, explanation=error.message, fields=error.fields, ))
def handle_login(controller, form, responder, user, rem=None, signature=None, **kwargs): def _event(error): g.events.login_event('login_attempt', error_msg=error, user_name=request.urlvars.get('url_user'), remember_me=rem, signature=signature, request=request, context=c) if signature and not signature.is_valid(): _event(error="SIGNATURE") abort(403) if feature.is_enabled('login_disabled'): _event(error="DISABLED") abort(403) hook_error = hooks.get_hook("account.login").call_until_return( responder=responder, request=request, context=c, ) # if any of the hooks returned an error, abort the login. The # set_error in this case also needs to exist in the hook. if hook_error: _event(error=hook_error) return exempt_ua = (request.user_agent and any( ua in request.user_agent for ua in g.config.get('exempt_login_user_agents', ()))) if (errors.LOGGED_IN, None) in c.errors: if user == c.user or exempt_ua: # Allow funky clients to re-login as the current user. c.errors.remove((errors.LOGGED_IN, None)) else: _event(error='LOGGED_IN') abort(reddit_http_error(409, errors.LOGGED_IN)) if responder.has_errors("ratelimit", errors.RATELIMIT): _event(error='RATELIMIT') elif responder.has_errors("passwd", errors.WRONG_PASSWORD): _event(error='WRONG_PASSWORD') else: controller._login(responder, user, rem) _event(error=None)
def on_validation_error(self, error): if not error.code: raise ValueError('Error %r missing status code' % error) abort( reddit_http_error( code=error.code, error_name=error.name, explanation=error.message, fields=error.fields, ))
def handle_login( controller, form, responder, user, rem=None, signature=None, **kwargs ): def _event(error): g.events.login_event( 'login_attempt', error_msg=error, user_name=request.urlvars.get('url_user'), remember_me=rem, signature=signature, request=request, context=c) if signature and not signature.is_valid(): _event(error="SIGNATURE") abort(403) hook_error = hooks.get_hook("account.login").call_until_return( responder=responder, request=request, context=c, ) # if any of the hooks returned an error, abort the login. The # set_error in this case also needs to exist in the hook. if hook_error: _event(error=hook_error) return exempt_ua = (request.user_agent and any(ua in request.user_agent for ua in g.config.get('exempt_login_user_agents', ()))) if (errors.LOGGED_IN, None) in c.errors: if user == c.user or exempt_ua: # Allow funky clients to re-login as the current user. c.errors.remove((errors.LOGGED_IN, None)) else: _event(error='LOGGED_IN') abort(reddit_http_error(409, errors.LOGGED_IN)) if responder.has_errors("ratelimit", errors.RATELIMIT): _event(error='RATELIMIT') elif responder.has_errors("passwd", errors.WRONG_PASSWORD): _event(error='WRONG_PASSWORD') else: controller._login(responder, user, rem) _event(error=None)
def handle_error(self, code, reason=None, **data): abort(reddit_http_error(code, reason, **data))
def POST_request_promo( self, site, srnames, is_mobile_web, platform, loid, is_refresh, displayed_things, referrer, ): self.OPTIONS_request_promo() if (errors.INVALID_SITE_PATH, "site") in c.errors: return abort(reddit_http_error(400, errors.INVALID_SITE_PATH)) displayed_list = displayed_things.split(',') if displayed_things else [] if site: keywords = promote.keywords_from_context( c.user, site, displayed_things=displayed_list, ) elif srnames: keywords = srnames.split('+') else: return properties = properties_from_context(c, site) # backwards compat if platform is None: platform = "mobile_web" if is_mobile_web else "desktop" # request multiple ads in case some are hidden by the builder due # to the user's hides/preferences response = adzerk_request( keywords=keywords, properties=properties, user_id=self.get_uid(loid), platform=platform, is_refresh=is_refresh, referrer=referrer, ) if not response: g.stats.simple_event('adzerk.request.no_promo') return # for adservers, adzerk returns markup so we pass it to the client if isinstance(response, AdserverResponse): g.stats.simple_event('adzerk.request.adserver') return responsive(response.body) res_by_campaign = {r.campaign: r for r in response} adserver_click_urls = {r.campaign: r.click_url for r in response} tuples = [promote.PromoTuple(r.link, 1., r.campaign) for r in response] builder = CampaignBuilder(tuples, wrap=default_thing_wrapper(), keep_fn=promote.promo_keep_fn, num=1, skip=True) listing = LinkListing(builder, nextprev=False).listing() promote.add_trackers(listing.things, c.site, adserver_click_urls=adserver_click_urls) promote.update_served(listing.things) if listing.things: g.stats.simple_event('adzerk.request.valid_promo') if is_refresh: g.stats.simple_event('adzerk.request.auto_refresh') w = listing.things[0] r = res_by_campaign[w.campaign] up = UrlParser(r.imp_pixel) up.hostname = "pixel.redditmedia.com" w.adserver_imp_pixel = up.unparse() w.adserver_upvote_pixel = r.upvote_pixel w.adserver_downvote_pixel = r.downvote_pixel w.adserver_click_url = r.click_url w.ecpm = r.ecpm w.moat_query = r.moat_query w.num = "" return responsive(w.render(), space_compress=True) else: g.stats.simple_event('adzerk.request.skip_promo')
def POST_request_promo( self, site, srnames, is_mobile_web, platform, loid, is_refresh, placements, displayed_things, referrer, user_day, user_hour, adblock, ): self.OPTIONS_request_promo() if (errors.INVALID_SITE_PATH, "site") in c.errors: return abort(reddit_http_error(400, errors.INVALID_SITE_PATH)) displayed_list = displayed_things.split(',') if displayed_things else [] if site: keywords = promote.keywords_from_context( c.user, site, displayed_things=displayed_list, ) elif srnames: keywords = srnames.split('+') else: return properties = promote.properties_from_context( context=c, keywords=keywords, placement="sponsored_headline", displayed_things=displayed_list, exclude=(None if c.user_is_loggedin else ["age_hours"]), ) properties['user_day'] = user_day properties['user_hour'] = user_hour if adblock is not None: properties["adblock"] = adblock # backwards compat if platform is None: platform = "mobile_web" if is_mobile_web else "desktop" if not placements: placements = ["div0"] # request multiple ads in case some are hidden by the builder due # to the user's hides/preferences response = adzerk_request( keywords=keywords, properties=properties, user_id=self.get_uid(loid), placement_names=placements, platform=platform, is_refresh=is_refresh, referrer=referrer, timeout=int(g.live_config.get("ads_loading_timeout_ms", 1000) / 1000.), ) if not response: g.stats.simple_event('adzerk.request.no_promo') return # for blanks, adzerk returns markup so we pass it to the client if isinstance(response, BlankCreativeResponse): g.stats.simple_event('adzerk.request.blank') return responsive(response.body) res_by_campaign = {r.campaign: r for r in response} adserver_click_urls = {r.campaign: r.click_url for r in response} priorities = {r.campaign: r.priority for r in response} tuples = [promote.PromoTuple(r.link, 1., r.campaign) for r in response] builder = CampaignBuilder(tuples, wrap=default_thing_wrapper(), keep_fn=promote.promo_keep_fn, num=1, skip=True) listing = LinkListing(builder, nextprev=False).listing() promote.add_trackers( listing.things, c.site, adserver_click_urls=adserver_click_urls, priorities=priorities) promote.update_served(listing.things) if listing.things: g.stats.simple_event('adzerk.request.valid_promo') if is_refresh: g.stats.simple_event('adzerk.request.auto_refresh') w = listing.things[0] r = res_by_campaign[w.campaign] up = UrlParser(r.imp_pixel) up.hostname = "pixel.redditmedia.com" w.adserver_imp_pixel = up.unparse() w.adserver_upvote_pixel = r.upvote_pixel w.adserver_downvote_pixel = r.downvote_pixel w.adserver_click_url = r.click_url w.ecpm = r.ecpm w.moat_query = r.moat_query w.num = "" return responsive(w.render(), space_compress=True) else: g.stats.simple_event('adzerk.request.skip_promo')