diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -43,9 +43,21 @@ REGEX_REPLY = re.compile('>>(\d+)') class PostManager(models.Manager): + def clear_ppd_cache_if_old(self): + """ + If new post in the other day then current post, a day has changed + and we need to remove the PPD cache and recalculate PPD including the + previous day + """ + + today = datetime.now().date() + posts = self.filter(pub_time__gte=today) + if posts.exists(): + cache.delete(CACHE_KEY_PPD) + def create_post(self, title, text, image=None, thread=None, ip=NO_IP, tags=None, user=None): - cache.delete(CACHE_KEY_PPD) + self.clear_ppd_cache_if_old() posting_time = timezone.now() if not thread: @@ -62,7 +74,7 @@ class PostManager(models.Manager): thread_new=thread, image=image, poster_ip=ip, - poster_user_agent=UNKNOWN_UA, + poster_user_agent=UNKNOWN_UA, # TODO Get UA at last! last_edit_time=posting_time, user=user) @@ -162,7 +174,7 @@ class PostManager(models.Manager): return int(math.ceil(thread_count / float(settings.THREADS_PER_PAGE))) def get_posts_per_day(self): - """Get count of posts for the current day""" + """Get average count of posts per day for the last 7 days""" ppd = cache.get(CACHE_KEY_PPD) if ppd: