views:

49

answers:

1

I need to create a cross-site template object which will check the current time and return either one string if it's within a time range specified in a model or another or blank in all other cases.

Seems simple, but I wonder what is the best approach here? Also, there are a few other considerations:

  • the string should be editable
  • the display time should not have an end time before the start time
  • we need to allow for multiple strings to be stored, but only one (or none) to be selected as 'live'

As a starting point, I'd use a model to define the string as well as the start and end time, like this:

from datetime import datetime
class dynamicString(models.Model):
start = models.TimeField()
end = models.TimeField()
name = models.CharField(max_length=50, help_text = 'Just for reference, won\'t be displayed on site.')
number = models.CharField(max_length=18, help_text = 'This is the string to be displayed within the above timeframe.')
active = models.BooleanField(help_text = 'Uncheck this to stop string from displaying entierly.')
def __unicode__(self):
 return self.name

But where next to incorporate the logic rules?

A: 

Redesigned this using a simpler rule system: check for one number time range, if within range and if not blank, return the number, otherwise return a backup number (which may be blank).

Works a treat passed as a context process.

Chris