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 photo(self): photo = cache.get('photo-of-species:%s' % self.pk) if photo is None: best = r.zrange('bestpics-species:%s' % self.pk, 0, 0, desc=True) if best: from zoo.photos.models import Photo try: photo = Photo.objects.get(pk = best[0]) except Photo.DoesNotExist: photo = 'no-photo' else: try: photo = self.visible_photos().annotate( num_faves = Count('favourited') ).select_related('created_by').order_by('-num_faves')[0] except IndexError: photo = 'no-photo' cache.set('photo-of-species:%s' % self.pk, photo, 60 * 60 * 5) if photo == 'no-photo': return None return photo
def top_3_photo_ids(self): return r.zrange('bestpics-species:%s' % self.pk, 0, 2, desc=True)