bgneal@515: """ bgneal@515: Tools for the Photo of the Day (potd) application. bgneal@515: bgneal@515: """ bgneal@515: import random bgneal@515: import logging bgneal@515: bgneal@515: from potd.models import Current, Sequence, Photo bgneal@515: bgneal@515: bgneal@515: logger = logging.getLogger(__name__) bgneal@515: bgneal@515: bgneal@515: def get_sequence(): bgneal@515: """ bgneal@515: Reads the photo sequence object from the database and converts it into a bgneal@515: list of photo IDs. If the sequence object is not found, and empty list is bgneal@515: returned. bgneal@515: bgneal@515: """ bgneal@515: try: bgneal@515: s = Sequence.objects.get(pk=1) bgneal@515: except Sequence.DoesNotExist: bgneal@515: return [] bgneal@515: bgneal@515: return [int(x) for x in s.seq.split(',')] bgneal@515: bgneal@515: bgneal@515: def new_sequence(): bgneal@515: """ bgneal@515: Generates a new random sequence of photos and saves it to the database. bgneal@515: The sequence is returned as a list of photo IDs. bgneal@515: bgneal@515: """ bgneal@515: ids = list(Photo.objects.values_list('id', flat=True)) bgneal@515: random.shuffle(ids) bgneal@515: bgneal@515: try: bgneal@515: s = Sequence.objects.get(pk=1) bgneal@515: except Sequence.DoesNotExist: bgneal@515: s = Sequence() bgneal@515: bgneal@515: s.seq = ','.join([str(n) for n in ids]) bgneal@515: s.save() bgneal@515: return ids bgneal@515: bgneal@515: bgneal@515: def pick_potd(): bgneal@515: """ bgneal@515: Chooses the next POTD. Run this command at midnight to update the POTD. bgneal@515: bgneal@515: """ bgneal@515: # Get the "current" record for the now old POTD: bgneal@515: try: bgneal@515: c = Current.objects.get(pk=1) bgneal@515: current = c.potd.pk bgneal@515: except Current.DoesNotExist: bgneal@515: c = Current() bgneal@515: current = None bgneal@515: bgneal@515: # Get the sequence of photo ID's: bgneal@515: seq = get_sequence() bgneal@515: bgneal@515: # If there is no current object, sequence, or if this was the last POTD in bgneal@515: # the sequence, generate a new random sequence: bgneal@515: bgneal@515: if current is None or not seq or current == seq[-1]: bgneal@515: # time to generate a new random sequence bgneal@515: seq = new_sequence() bgneal@515: # set current to the first one in the sequence bgneal@515: if seq: bgneal@515: try: bgneal@515: c.potd = Photo.objects.get(pk=seq[0]) bgneal@515: except Photo.DoesNotExist: bgneal@515: logger.error("POTD: missing photo %d", seq[0]) bgneal@515: else: bgneal@515: c.potd.potd_count += 1 bgneal@515: c.potd.save() bgneal@515: c.save() bgneal@515: else: bgneal@515: # find current in the sequence, pick the next one bgneal@515: try: bgneal@515: i = seq.index(current) bgneal@515: except ValueError: bgneal@515: logger.error("POTD: current photo (%d) not in sequence", current) bgneal@515: return bgneal@515: bgneal@515: n = i + 1 bgneal@515: try: bgneal@515: c.potd = Photo.objects.get(pk=seq[n]) bgneal@515: except Photo.DoesNotExist: bgneal@515: logger.error("POTD: missing next photo %d", n) bgneal@515: else: bgneal@515: c.potd.potd_count += 1 bgneal@515: c.potd.save() bgneal@515: c.save()