annotate potd/tools.py @ 1125:4f81c9fe8705

Convert edit elsewhere links page to V3 design.
author Brian Neal <bgneal@gmail.com>
date Thu, 18 Aug 2016 20:36:41 -0500
parents ee87ea74d46b
children
rev   line source
bgneal@515 1 """
bgneal@515 2 Tools for the Photo of the Day (potd) application.
bgneal@515 3
bgneal@515 4 """
bgneal@515 5 import random
bgneal@515 6 import logging
bgneal@515 7
bgneal@515 8 from potd.models import Current, Sequence, Photo
bgneal@515 9
bgneal@515 10
bgneal@515 11 logger = logging.getLogger(__name__)
bgneal@515 12
bgneal@515 13
bgneal@515 14 def get_sequence():
bgneal@515 15 """
bgneal@515 16 Reads the photo sequence object from the database and converts it into a
bgneal@515 17 list of photo IDs. If the sequence object is not found, and empty list is
bgneal@515 18 returned.
bgneal@515 19
bgneal@515 20 """
bgneal@515 21 try:
bgneal@515 22 s = Sequence.objects.get(pk=1)
bgneal@515 23 except Sequence.DoesNotExist:
bgneal@515 24 return []
bgneal@515 25
bgneal@515 26 return [int(x) for x in s.seq.split(',')]
bgneal@515 27
bgneal@515 28
bgneal@515 29 def new_sequence():
bgneal@515 30 """
bgneal@515 31 Generates a new random sequence of photos and saves it to the database.
bgneal@515 32 The sequence is returned as a list of photo IDs.
bgneal@515 33
bgneal@515 34 """
bgneal@515 35 ids = list(Photo.objects.values_list('id', flat=True))
bgneal@515 36 random.shuffle(ids)
bgneal@515 37
bgneal@515 38 try:
bgneal@515 39 s = Sequence.objects.get(pk=1)
bgneal@515 40 except Sequence.DoesNotExist:
bgneal@515 41 s = Sequence()
bgneal@515 42
bgneal@515 43 s.seq = ','.join([str(n) for n in ids])
bgneal@515 44 s.save()
bgneal@515 45 return ids
bgneal@515 46
bgneal@515 47
bgneal@515 48 def pick_potd():
bgneal@515 49 """
bgneal@515 50 Chooses the next POTD. Run this command at midnight to update the POTD.
bgneal@515 51
bgneal@515 52 """
bgneal@515 53 # Get the "current" record for the now old POTD:
bgneal@515 54 try:
bgneal@515 55 c = Current.objects.get(pk=1)
bgneal@515 56 current = c.potd.pk
bgneal@515 57 except Current.DoesNotExist:
bgneal@515 58 c = Current()
bgneal@515 59 current = None
bgneal@515 60
bgneal@515 61 # Get the sequence of photo ID's:
bgneal@515 62 seq = get_sequence()
bgneal@515 63
bgneal@515 64 # If there is no current object, sequence, or if this was the last POTD in
bgneal@515 65 # the sequence, generate a new random sequence:
bgneal@515 66
bgneal@515 67 if current is None or not seq or current == seq[-1]:
bgneal@515 68 # time to generate a new random sequence
bgneal@515 69 seq = new_sequence()
bgneal@515 70 # set current to the first one in the sequence
bgneal@515 71 if seq:
bgneal@515 72 try:
bgneal@515 73 c.potd = Photo.objects.get(pk=seq[0])
bgneal@515 74 except Photo.DoesNotExist:
bgneal@515 75 logger.error("POTD: missing photo %d", seq[0])
bgneal@515 76 else:
bgneal@515 77 c.potd.potd_count += 1
bgneal@515 78 c.potd.save()
bgneal@515 79 c.save()
bgneal@515 80 else:
bgneal@515 81 # find current in the sequence, pick the next one
bgneal@515 82 try:
bgneal@515 83 i = seq.index(current)
bgneal@515 84 except ValueError:
bgneal@515 85 logger.error("POTD: current photo (%d) not in sequence", current)
bgneal@515 86 return
bgneal@515 87
bgneal@515 88 n = i + 1
bgneal@515 89 try:
bgneal@515 90 c.potd = Photo.objects.get(pk=seq[n])
bgneal@515 91 except Photo.DoesNotExist:
bgneal@515 92 logger.error("POTD: missing next photo %d", n)
bgneal@515 93 else:
bgneal@515 94 c.potd.potd_count += 1
bgneal@515 95 c.potd.save()
bgneal@515 96 c.save()