def check(self, key): group_id = r.get('apikey:%s' % key) if not group_id: raise BadKeyError, 'Key does not exist' group_info = r.get('apikeygroup:%s' % group_id) if not group_info: raise BadKeyError, 'Key does not belong to a valid group' self.max_per_day, self.max_per_hour, self.max_per_minute, \ self.max_per_5_second_burst = map(int, group_info.split(':')) return super(KeyRateLimiter, self).check(key)
def record_win(species, winner, loser): # We record the win-percentage for everything - what percentage of # competitions it has been in has it won? We do this on a per-species # basis to account for photos with multiple species that are great for # one and bad for the other. winner_times_seen = r.incr(SEEN_KEY % winner) loser_times_seen = r.incr(SEEN_KEY % loser) winner_times_won = r.incr(WON_KEY % winner) loser_times_won = r.get(WON_KEY % loser) or 0 winner_score = ( float(winner_times_won) / float(winner_times_seen) ) loser_score = ( float(loser_times_won) / float(loser_times_seen) ) # Update scores in the ordered - but ONLY for photos meeting minimum # viewing requirements if winner_times_seen >= MIN_VIEWING_REQUIREMENT: r.zadd(BESTPICS_KEY % species, winner, winner_score) if loser_times_seen >= MIN_VIEWING_REQUIREMENT: r.zadd(BESTPICS_KEY % species, loser, loser_score) return { 'winner_times_seen': winner_times_seen, 'winner_times_won': winner_times_won, 'winner_score': winner_score * 100, 'loser_times_seen': loser_times_seen, 'loser_times_won': loser_times_won, 'loser_score': loser_score * 100, }
def top_10_for_species(species): species_key = BESTPICS_KEY % species.pk pks = r.zrange(species_key, 0, 10, desc=True) metrics = [ (r.zscore(species_key, pk) or 0, r.get(SEEN_KEY % pk) or 0, pk) for pk in pks ] # We award win percentage above all - if that's a tie, the one which has # been in the most matches wins. If that's a tie, the one with the highest # pk (i.e. the one that was most recently added) wins, to keep the photos # on the site 'fresh'. metrics.sort(reverse = True) photos = Photo.objects.in_bulk(pks) results = [] for score, matches, pk in metrics: photo = photos[pk] photo.bestpic_score = score * 100 photo.bestpic_matches = matches results.append(photo) return results
def check_token(token): return r.get(token) is not None