Example #1
0
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
Example #2
0
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)
Example #3
0
 def delete(self, *args, **kwargs):
     super(ApiKey, self).delete(*args, **kwargs)
     r.delete('apikey:%s' % self.key)