def random_photos_for_species(species): key = 'species-photo-ids:%s' % species.pk photo_ids = r.smembers(key) if not photo_ids or len(photo_ids) < 2: photo_ids = list(species.visible_photos().values_list('pk',flat=True)) r.delete(key) for id in photo_ids: r.sadd(key, id) r.expire(key, 10 * 60) counts = [ c or 0 for c in r.mget( *['bestpics-photo-times-seen:%s' % i for i in photo_ids] ) ] # Pick first photo biased based on number of times they have been seen inverted_scores = [max(counts) - c for c in counts] first_photo = photo_ids[random_index_with_bias(inverted_scores)] remainder = list(id for id in photo_ids if id != first_photo) second_photo = random.choice(remainder) photos = list( Photo.objects.select_related('created_by').filter( pk__in = [first_photo, second_photo] ) ) random.shuffle(photos) return photos
def update_redis_set(): r.delete(SPECIES_SET + '-temp') species_with_multiple_photos = Species.objects.filter( sightings__photos__is_visible = True ).annotate( num_photos = models.Count('sightings__photos') ).filter(num_photos__gt = 1) for species in species_with_multiple_photos: r.sadd(SPECIES_SET + '-temp', species.pk) r.rename(SPECIES_SET + '-temp', SPECIES_SET)
def process_submission(request): # Process the previous submission try: options = signed.loads(request.POST.get('options', '')) except ValueError: return {} if (int(time.time()) - options['time']) > (5 * 60): return {} # Form is too old if not utils.check_token(options['token']): return {} # Token invalid species_pk = options['species'] contestants = options['contestants'] winner = int(request.POST.get('winner', '')) if not winner: return {} loser = (set(contestants) - set([winner])).pop() # Record a win! context = utils.record_win(species_pk, winner, loser) if not request.user.is_anonymous(): utils.record_contribution_from(request.user.username) photos = Photo.objects.select_related( 'created_by' ).in_bulk([winner, loser]) last_species = Species.objects.get(pk = species_pk) description = ''' %s: <a href="%s">%s</a>: <a href="%s"><img src="%s"></a> beat <a href="%s"><img src="%s"></a> ''' % ( str(datetime.datetime.now()), last_species.get_absolute_url(), last_species.common_name, photos[winner].get_absolute_url(), photos[winner].thumb_75_url(), photos[loser].get_absolute_url(), photos[loser].thumb_75_url(), ) if not request.user.is_anonymous(): description += ' (rated by <a href="%s">%s</a>)' % ( request.user.username, request.user.username ) # And record the species so we don't show it to them multiple times set_key = utils.USER_SEEN_SET % request.user.username list_key = utils.USER_SEEN_LIST % request.user.username r.push(list_key, species_pk, head=True) r.sadd(set_key, species_pk) if r.scard(set_key) >= SEEN_SPECIES_COUNT: r.srem(set_key, r.pop(list_key, tail=True)) r.push('bestpic-activity', description, head=True) r.ltrim('bestpic-activity', 0, 200) context.update({ 'last_species': last_species, 'last_winner': photos[winner], 'last_loser': photos[loser], 'show_link_to_best': utils.species_has_top_10(last_species), }) return context