def friend_rels(self, _update=False): rel_ids = self.friend_rels_cache(_update=_update) try: rels = Friend._byID_rel(rel_ids, return_dict=False, eager_load=True, data=True, thing_data=True) rels = list(rels) except NotFound: if _update: raise else: log_text("friend-rels-bandaid 1", "Had to recalc friend_rels (1) for %s" % self.name, "warning") return self.friend_rels(_update=True) if not _update: sorted_1 = sorted([r._thing2_id for r in rels]) sorted_2 = sorted(list(self.friends)) if sorted_1 != sorted_2: g.log.error("FR1: %r" % sorted_1) g.log.error("FR2: %r" % sorted_2) log_text("friend-rels-bandaid 2", "Had to recalc friend_rels (2) for %s" % self.name, "warning") self.friend_ids(_update=True) return self.friend_rels(_update=True) return dict((r._thing2_id, r) for r in rels)
def indict(defendant): from r2.models import Trial tk = trial_key(defendant) rv = False if defendant._deleted: result = "already deleted" elif getattr(defendant, "promoted", None) is not None: result = "it's promoted" elif hasattr(defendant, "verdict") and defendant.verdict is not None: result = "it already has a verdict" elif g.hardcache.get(tk): result = "it's already on trial" else: # The spams/koshers dict is just a infrequently-updated cache; the # official source of the data is the Jury relation. g.hardcache.set(tk, dict(spams=0, koshers=0), TRIAL_TIME) Trial.all_defendants(_update=True) result = "it's now indicted: %s" % tk rv = True log_text("indict_result", "%s: %s" % (defendant._id36, result), level="info") return rv
def check_txn_type(txn_type, psl): if txn_type == 'subscr_signup': return ("Ok", None) elif txn_type == 'subscr_cancel': return ("Ok", "cancel") elif txn_type == 'subscr_eot': return ("Ok", None) elif txn_type == 'subscr_failed': log_text("failed_subscription", "Just got notice of a failed PayPal resub.", "info") return ("Ok", None) elif txn_type == 'subscr_modify': log_text("modified_subscription", "Just got notice of a modified PayPal sub.", "info") return ("Ok", None) elif txn_type == 'send_money': return ("Ok", None) elif txn_type in ('new_case', 'recurring_payment_suspended_due_to_max_failed_payment'): return ("Ok", None) elif txn_type == 'subscr_payment' and psl == 'completed': return (None, "new") elif txn_type == 'web_accept' and psl == 'completed': return (None, None) else: raise ValueError("Unknown IPN txn_type / psl %r" % ((txn_type, psl),))
def populate_spotlight(): raise Exception("this function is broken (re: ip_and_slash16) and pending demolition") from r2.models import Jury from r2.lib.db.thing import NotFound if not (c.user_is_loggedin and c.user.jury_betatester()): g.log.debug("not eligible") return None try: juries_already_on = Jury.by_account(c.user) except NotFound: # This can happen if Jury.delete_old() just so happens to be cleaning # up this user's old Jury rels while they're visiting the front page. # In this unlucky case, just skip the 20% nagging below. juries_already_on = [] # If they're already on a jury, and haven't yet voted, re-show # it every five or so times. if rand.random() < 0.2: unvoted = filter(lambda j: j._name == '0', juries_already_on) defs = [u._thing2 for u in unvoted] active_trials = trial_info(defs) for d in defs: if active_trials.get(d._fullname, False): return d if not g.cache.add("global-jury-key", True, 5): g.log.debug("not yet time to add another juror") return None ip, slash16 = ip_and_slash16(request) jcd = jury_cache_dict(c.user, ip, slash16) if jcd is None: g.cache.delete("global-jury-key") return None if g.cache.get_multi(jcd.keys()) and not g.debug: g.log.debug("recent juror") g.cache.delete("global-jury-key") return None trial = assign_trial(c.user, juries_already_on, ip, slash16) if trial is None: g.log.debug("nothing available") g.cache.delete("global-jury-key") return None for k, v in jcd.iteritems(): g.cache.set(k, True, v) log_text("juryassignment", "%s was just assigned to the jury for %s" % (c.user.name, trial._id36), level="info") return trial
def run(self, dest): if not dest: dest = request.referer or self.default or "/" ld = dest.lower() if (ld.startswith("/") or ld.startswith("http://") or ld.startswith("https://")): u = UrlParser(dest) if u.is_reddit_url(): return dest ip = getattr(request, "ip", "[unknown]") fp = getattr(request, "fullpath", "[unknown]") dm = c.domain or "[unknown]" cn = c.cname or "[unknown]" log_text("invalid redirect", "%s attempted to redirect from %s to %s with domain %s and cname %s" % (ip, fp, dest, dm, cn), "info") return "/"
def friend_rels(self, _update = False): rel_ids = self.friend_rels_cache(_update=_update) try: rels = Friend._byID_rel(rel_ids, return_dict=False, eager_load = True, data = True, thing_data = True) rels = list(rels) except NotFound: if _update: raise else: log_text("friend-rels-bandaid 1", "Had to recalc friend_rels (1) for %s" % self.name, "warning") return self.friend_rels(_update=True) if not _update: sorted_1 = sorted([r._thing2_id for r in rels]) sorted_2 = sorted(list(self.friends)) if sorted_1 != sorted_2: g.log.error("FR1: %r" % sorted_1) g.log.error("FR2: %r" % sorted_2) log_text("friend-rels-bandaid 2", "Had to recalc friend_rels (2) for %s" % self.name, "warning") self.friend_ids(_update=True) return self.friend_rels(_update=True) return dict((r._thing2_id, r) for r in rels)
def update_voting(defendant, koshers, spams): tk = trial_key(defendant) d = g.hardcache.get(tk) if d is None: log_text("update_voting() fail", "%s not on trial" % defendant._id36, level="error") else: d["koshers"] = koshers d["spams"] = spams g.hardcache.set(tk, d, TRIAL_TIME)
def check_payment_status(payment_status): if payment_status is None: payment_status = '' psl = payment_status.lower() if psl == 'completed': return (None, psl) elif psl == 'refunded': log_text("refund", "Just got notice of a refund.", "info") # TODO: something useful when this happens -- and don't # forget to verify first return ("Ok", psl) elif psl == 'pending': log_text("pending", "Just got notice of a Pending, whatever that is.", "info") # TODO: something useful when this happens -- and don't # forget to verify first return ("Ok", psl) elif psl == 'reversed': log_text("reversal", "Just got notice of a PayPal reversal.", "info") # TODO: something useful when this happens -- and don't # forget to verify first return ("Ok", psl) elif psl == 'canceled_reversal': log_text("canceled_reversal", "Just got notice of a PayPal 'canceled reversal'.", "info") return ("Ok", psl) elif psl == '': return (None, psl) else: raise ValueError("Unknown IPN status: %r" % payment_status)
def myfunc(msgs, chan): if verbose: print "processing a batch" incrs = {} for msg in msgs: try: d = check_dict(msg.body) except TypeError: log_text("usage_q error", "wtf is %r" % msg.body, "error") continue hund_sec = hund_from_start_and_end(d["start_time"], d["end_time"]) action = d["action"].replace("-", "_") fudged_count = int(1 / d["sampling_rate"]) fudged_elapsed = int(hund_sec / d["sampling_rate"]) for exp_time, bucket in buckets(d["end_time"]): k = "%s-%s" % (bucket, action) incrs.setdefault(k, [0, 0, exp_time]) incrs[k][0] += fudged_count incrs[k][1] += fudged_elapsed for k, (count, elapsed, exp_time) in incrs.iteritems(): c_key = "profile_count-" + k e_key = "profile_elapsed-" + k if verbose: c_old = g.hardcache.get(c_key) e_old = g.hardcache.get(e_key) g.hardcache.accrue(c_key, delta=count, time=exp_time) g.hardcache.accrue(e_key, delta=elapsed, time=exp_time) if verbose: c_new = g.hardcache.get(c_key) e_new = g.hardcache.get(e_key) print "%s: %s -> %s" % (c_key, c_old, c_new) print "%s: %s -> %s" % (e_key, e_old, e_new) if len(msgs) < limit / 2: if verbose: print "Sleeping..." sleep(10)
def myfunc(msgs, chan): if verbose: print "processing a batch" incrs = {} for msg in msgs: try: d = check_dict(msg.body) except TypeError: log_text("usage_q error", "wtf is %r" % msg.body, "error") continue hund_sec = hund_from_start_and_end(d["start_time"], d["end_time"]) action = d["action"].replace("-", "_") fudged_count = int( 1 / d["sampling_rate"]) fudged_elapsed = int(hund_sec / d["sampling_rate"]) for exp_time, bucket in buckets(d["end_time"]): k = "%s-%s" % (bucket, action) incrs.setdefault(k, [0, 0, exp_time]) incrs[k][0] += fudged_count incrs[k][1] += fudged_elapsed for k, (count, elapsed, exp_time) in incrs.iteritems(): c_key = "profile_count-" + k e_key = "profile_elapsed-" + k if verbose: c_old = g.hardcache.get(c_key) e_old = g.hardcache.get(e_key) g.hardcache.accrue(c_key, delta=count, time=exp_time) g.hardcache.accrue(e_key, delta=elapsed, time=exp_time) if verbose: c_new = g.hardcache.get(c_key) e_new = g.hardcache.get(e_key) print "%s: %s -> %s" % (c_key, c_old, c_new) print "%s: %s -> %s" % (e_key, e_old, e_new) if len(msgs) < limit / 2: if verbose: print "Sleeping..." sleep (10)
def _notfound(self, style): from pylons import g, request from pylons.controllers.util import abort from r2.lib.log import log_text if g.debug: raise NotImplementedError (repr(self), style) else: if style == 'png': level = "debug" else: level = "warning" log_text("missing template", "Couldn't find %s template for %r %s" % (style, self, request.path), level) abort(404)
def populate_spotlight(): if not (c.user_is_loggedin and c.user.jury_betatester()): g.log.debug("not eligible") return None if not g.cache.add("global-jury-key", True, 5): g.log.debug("not yet time to add another juror") return None ip, slash16 = ip_and_slash16(request) jcd = jury_cache_dict(c.user, ip, slash16) if jcd is None: g.cache.delete("global-jury-key") return None if g.cache.get_multi(jcd.keys()) and not g.debug: g.log.debug("recent juror") g.cache.delete("global-jury-key") return None trial = assign_trial(c.user, ip, slash16) if trial is None: g.log.debug("nothing available") g.cache.delete("global-jury-key") return None for k, v in jcd.iteritems(): g.cache.set(k, True, v) log_text("juryassignment", "%s was just assigned to the jury for %s" % (c.user.name, trial._id36), level="info") return trial
def POST_ipn(self, paypal_secret, payment_status, txn_id, paying_id, payer_email, mc_currency, mc_gross, custom): parameters = request.POST.copy() # Make sure it's really PayPal if paypal_secret != g.PAYPAL_SECRET: log_text("invalid IPN secret", "%s guessed the wrong IPN secret" % request.ip, "warning") raise ValueError # Return early if it's an IPN class we don't care about response, psl = check_payment_status(payment_status) if response: return response # Return early if it's a txn_type we don't care about response, subscription = check_txn_type(parameters['txn_type'], psl) if subscription is None: subscr_id = None elif subscription == "new": subscr_id = parameters['subscr_id'] elif subscription == "cancel": cancel_subscription(parameters['subscr_id']) else: raise ValueError("Weird subscription: %r" % subscription) if response: return response # Check for the debug flag, and if so, dump the IPN dict if g.cache.get("ipn-debug"): g.cache.delete("ipn-debug") dump_parameters(parameters) if mc_currency != 'USD': raise ValueError("Somehow got non-USD IPN %r" % mc_currency) if not (txn_id and paying_id and payer_email and mc_gross): dump_parameters(parameters) raise ValueError("Got incomplete IPN") pennies = int(mc_gross * 100) months, days = months_and_days_from_pennies(pennies) # Special case: autorenewal payment existing = existing_subscription(subscr_id, paying_id, custom) if existing: if existing != "deleted account": create_claimed_gold ("P" + txn_id, payer_email, paying_id, pennies, days, None, existing._id, c.start_time, subscr_id) admintools.engolden(existing, days) g.log.info("Just applied IPN renewal for %s, %d days" % (existing.name, days)) return "Ok" # More sanity checks that all non-autorenewals should pass: if not custom: dump_parameters(parameters) raise ValueError("Got IPN with txn_id=%s and no custom" % txn_id) self.finish(parameters, "P" + txn_id, payer_email, paying_id, subscr_id, custom, pennies, months, days)
def _cache_myself(self): ck = self._cache_key() if self.__class__.__name__ in ("Link", "Comment", "Subreddit") and not self._t: log_text ("{} cache", "About to cache {} for %r" % ck, "warning") cache.set(ck, self)
def __getattr__(self, attr): #makes pickling work for some reason if attr.startswith('__'): raise AttributeError, attr if not (attr.startswith('_') or self._asked_for_data or getattr(self, "_nodb", False)): msg = ("getattr(%r) called on %r, " + "but you didn't say data=True") % (attr, self) raise ValueError(msg) try: if hasattr(self, '_t'): rv = self._t[attr] return rv else: raise AttributeError, attr except KeyError: try: return getattr(self, '_defaults')[attr] except KeyError: try: _id = object.__getattribute__(self, "_id") except AttributeError: _id = "???" try: cl = object.__getattribute__(self, "__class__").__name__ except AttributeError: cl = "???" if self._loaded: nl = "it IS loaded" else: nl = "it is NOT loaded" # The %d format is nicer since it has no "L" at the # end, but if we can't do that, fall back on %r. try: id_str = "%d" % _id except TypeError: id_str = "%r" % _id descr = '%s(%s).%s' % (cl, id_str, attr) try: essentials = object.__getattribute__(self, "_essentials") except AttributeError: print "%s has no _essentials" % descr essentials = () if isinstance(essentials, str): print "Some dumbass forgot a comma." essentials = essentials, deleted = object.__getattribute__(self, "_deleted") if deleted: nl += " and IS deleted." else: nl += " and is NOT deleted." if attr in essentials and not deleted: log_text ("essentials-bandaid-reload", "%s not found; %s Forcing reload." % (descr, nl), "warning") self._load() try: return self._t[attr] except KeyError: log_text ("essentials-bandaid-failed", "Reload of %s didn't help. I recommend deletion." % descr, "error") raise AttributeError, '%s not found; %s' % (descr, nl)
def __getattr__(self, attr): #makes pickling work for some reason if attr.startswith('__'): raise AttributeError, attr try: if hasattr(self, '_t'): return self._t[attr] else: raise AttributeError, attr except KeyError: try: return getattr(self, '_defaults')[attr] except KeyError: try: _id = object.__getattribute__(self, "_id") except AttributeError: _id = "???" try: cl = object.__getattribute__(self, "__class__").__name__ except AttributeError: cl = "???" if self._loaded: nl = "it IS loaded." else: nl = "it is NOT loaded." # The %d format is nicer since it has no "L" at the # end, but if we can't do that, fall back on %r. try: id_str = "%d" % _id except TypeError: id_str = "%r" % _id descr = '%s(%s).%s' % (cl, id_str, attr) try: essentials = object.__getattribute__(self, "_essentials") except AttributeError: print "%s has no _essentials" % descr essentials = () if isinstance(essentials, str): print "Some dumbass forgot a comma." essentials = essentials, if attr in essentials: log_text ("essentials-bandaid-reload", "%s not found; %s Forcing reload." % (descr, nl), "warning") self._load() try: return self._t[attr] except KeyError: log_text ("essentials-bandaid-failed", "Reload of %s didn't help. I recommend deletion." % descr, "error") raise AttributeError, '%s not found; %s' % (descr, nl)