def set_day(self): # make the day from day.models import Day if not self.taken: log.error("tried to set day but no taken date set. %s" % (self.id and str(self.id) or "no id")) return False date = self.taken.date() qq = date try: day = Day.objects.filter(date=date).get() except Day.DoesNotExist: day = Day(date=date) day.save() # first time through myphoto is null. if self.id: # if self.myphoto==False: # do nothing, don't re-add the day # pass # elif self.myphoto: self.day = day return True else: # when you are first saved, treat as myphoto. self.day = day self.myphoto = True return True
def first_day(request): # TODO optimize this first_day = Day.first_day() if not first_day: first_day = datetime.today() return {"FIRST_DAY": first_day }
def include_statistics(): # TODO cache these or otherwise optimize them start_date = Day.first_day() num_total_stories = Story.all_fiction.count() mod_ten = num_total_stories % 10 num_total_stories -= mod_ten featured_stories = Story.verified_fiction.exclude(featured_days=None).filter(featured_days__day__lte=datetime.today()) num_featured_stories = featured_stories.count() num_total_journals = Journal.objects.count() num_featured_journals = len(set([s.journal for s in featured_stories])) return { "start_date": start_date, "num_total_journals": num_total_journals, "num_total_stories": num_total_stories, "num_featured_stories": num_featured_stories, "num_featured_journals": num_featured_journals, } # Since Feb 1, 2012, 7 featured stories from 12 unique journals, drawn from 345 stories from 12 unique journals.
def random_day_view(request): return redirect(Day.random().get_absolute_url())
def select_story_for_next_day(): """ If tomorrow (Day after today) exists and has a Story, does nothing. If it doesn't exist, creates it. (Note: for now, Day.save() automatically assigns a Story, so the next step shouldn't happen.) If it exists and doesn't have a Story assigned, assigns it a Story. :return: """ logger = select_story_for_next_day.get_logger(logfile="logs/user/tasks.log") logger.info("select_story_for_next_day") try: logger.info("in try") today = date.today() tomorrow = date.fromordinal(today.toordinal() + 1) day_after_tomorrow = date.fromordinal(tomorrow.toordinal() + 1) today_days = Day.objects.filter(day__gte=today).filter(day__lte=tomorrow) logger.info("today_days.count() is %d" % today_days.count()) if today_days.count() < 1: logger.warning("No Day for today. Creating one.") today_day = Day.objects.create(day=today) else: today_day = today_days[0] if today_days.count() > 1: logger.warning( "Multiple Days for today! This violates the db constraint on Day so should never happen. today_days.count() is %d." % today_days.count() ) logger.info("today_day is %s" % today_day) if not today_day.story: logger.warning("No story set for today! Setting one.") today_day.story = Day.find_random_unfeatured_story(num_days_recent=settings.NUM_DAYS_RECENT) logger.info("today_day.story is %s" % today_day.story) tomorrow_days = Day.objects.filter(day__gte=tomorrow).filter(day__lte=day_after_tomorrow) logger.debug("Selecting story for %s. tomorrow_days.count() is %d." % (tomorrow, tomorrow_days.count())) if tomorrow_days.count() > 1: # error--multiple Days for tomorrow! logger.warning( "Multiple Days for tomorrow! This violates the db constraint on Day so should never happen. tomorrow_days.count() is %d." % tomorrow_days.count() ) if tomorrow_days: tomorrow_day = tomorrow_days[0] else: tomorrow_day = Day.objects.create(day=tomorrow) logger.info("tomorrow_day is %s" % tomorrow_day) if not tomorrow_day.story: # As long as Day.save() is also calling find_random_unfeatured_story, this should not be called tomorrow_day.story = Day.find_random_unfeatured_story(num_days_recent=settings.NUM_DAYS_RECENT) logger.info("tomorrow_day.story is %s" % tomorrow_day.story) except Exception, exc: logger.warning("Exception in select_story_for_tomorrow. Retrying. Exception was '%s'." % exc) select_story_for_next_day.retry(exc=exc) # retry in default (3 * 60 seconds)