Пример #1
0
def random_species_with_multiple_photos(user = None):
    if user is not None:
        # Use set difference to avoid returning a species user has seen in 
        # the past 100 goes
        species_set = USER_NOTSEEN_TEMP_SET % user.username
        r.sdiffstore(species_set, SPECIES_SET, USER_SEEN_SET % user.username)
    else:
        species_set = SPECIES_SET
    while True:
        pk = r.srandmember(species_set)
        if pk is None:
            update_redis_set()
            pk = r.srandmember(species_set)
            if pk is None:
                raise ValueError('No species has more than 1 visible photo')
        try:
            return Species.objects.get(pk = pk)
        except Species.DoesNotExist:
            r.srem(species_set, pk)
Пример #2
0
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