diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -5,6 +5,7 @@ from random import random import time import math import re +from django.core.cache import cache from django.db import models from django.http import Http404 @@ -14,6 +15,10 @@ from markupfield.fields import MarkupFie from neboard import settings from boards import thumbs +APP_LABEL_BOARDS = 'boards' + +CACHE_KEY_PPD = 'ppd' + POSTS_PER_DAY_RANGE = range(7) BAN_REASON_AUTO = 'Auto' @@ -40,6 +45,8 @@ class PostManager(models.Manager): def create_post(self, title, text, image=None, thread=None, ip=NO_IP, tags=None, user=None): + cache.delete(CACHE_KEY_PPD) + posting_time = timezone.now() if not thread: thread = Thread.objects.create(bump_time=posting_time, @@ -157,6 +164,10 @@ class PostManager(models.Manager): def get_posts_per_day(self): """Get count of posts for the current day""" + ppd = cache.get(CACHE_KEY_PPD) + if ppd: + return ppd + today = datetime.now().date() posts_per_days = [] @@ -166,11 +177,14 @@ class PostManager(models.Manager): day_time_start = datetime.combine(day_start, dtime()) day_time_end = datetime.combine(day_end, dtime()) - posts_per_days.append(float(self.filter(pub_time__lte=day_time_end, - pub_time__gte=day_time_start).count())) + posts_per_days.append(float(self.filter( + pub_time__lte=day_time_end, + pub_time__gte=day_time_start).count())) - return sum(posts_per_day for posts_per_day in posts_per_days) / \ - len(posts_per_days) + ppd = (sum(posts_per_day for posts_per_day in posts_per_days) / + len(posts_per_days)) + cache.set(CACHE_KEY_PPD, ppd) + return ppd class Post(models.Model): @@ -179,7 +193,7 @@ class Post(models.Model): objects = PostManager() class Meta: - app_label = 'boards' + app_label = APP_LABEL_BOARDS def _update_image_filename(self, filename): """Get unique image filename""" @@ -241,7 +255,7 @@ class Post(models.Model): class Thread(models.Model): class Meta: - app_label = 'boards' + app_label = APP_LABEL_BOARDS tags = models.ManyToManyField('Tag') bump_time = models.DateTimeField()