kaputtnik has proposed merging lp:~widelands-dev/widelands-website/official_posts into lp:widelands-website.
Commit message: Unify getting of official posts; performance tweaks Requested reviews: Widelands Developers (widelands-dev) For more details, see: https://code.launchpad.net/~widelands-dev/widelands-website/official_posts/+merge/364989 This branch makes gathering of official posts (= posts for normal users) easier, because it is defined in one place. This applies for the places were ALL posts needed filtering: feeds, the new latest posts page and the Last Posts box. This branch boosts also performance: E.g. in my test environment gathering the last 1000 posts in the new latest posts view lasts: - with trunk: ~0.4 sec - with this branch: ~0.04 sec This scales good also for all posts (9107 on my testsystem): - trunk: 18 sec. - this branch: 0.09 sec. -- Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands-website/official_posts into lp:widelands-website.
=== modified file 'pybb/feeds.py' --- pybb/feeds.py 2018-12-11 13:44:01 +0000 +++ pybb/feeds.py 2019-03-22 21:00:47 +0000 @@ -56,15 +56,11 @@ title_template = 'pybb/feeds/posts_title.html' description_template = 'pybb/feeds/posts_description.html' - all_objects = Post.objects.exclude( - topic__forum__category__internal=True).exclude( - topic__in=Post.hidden_topics.all()).filter(hidden=False) + all_objects = Post.objects.official() def items_for_object(self, obj): - # Latest posts for forum 'xy' - return Post.objects.exclude( - topic__in=Post.hidden_topics.all()).filter( - hidden=False, topic__forum=obj).order_by('-created')[:15] + # Latest posts for forum 'xy' + return Post.objects.official(limit=15) # Validated through http://validator.w3.org/feed/ === modified file 'pybb/models.py' --- pybb/models.py 2018-12-21 09:43:02 +0000 +++ pybb/models.py 2019-03-22 21:00:47 +0000 @@ -255,6 +255,31 @@ except: return [] +class OfficialPosts(models.Manager): + + def official(self, limit=None, date_from=None): + """Get official posts. + + That are all posts which shouldn't be visible to normal + visitors. The result is always orderd by the posts + creation time, Descending. Optional arguments: + + limit: Slice the QuerySet [:limit]. + date_from: Gathers all posts from this day until today. + """ + + qs = self.get_queryset().filter( + topic__forum__category__internal=False, hidden=False).exclude( + topic__in=Post.hidden_topics.all()).order_by( + '-created') + + if date_from: + qs = qs.filter(created__gte=date_from) + if limit: + qs = qs[:limit] + + return qs + class Post(RenderableItem): topic = models.ForeignKey( @@ -270,7 +295,7 @@ body_text = models.TextField(_('Text version')) hidden = models.BooleanField(_('Hidden'), blank=True, default=False) - objects = models.Manager() # Normal manager + objects = OfficialPosts() # Normal manager hidden_topics = HiddenTopicsManager() # Custom manager class Meta: === modified file 'pybb/templatetags/pybb_extras.py' --- pybb/templatetags/pybb_extras.py 2019-03-20 21:32:32 +0000 +++ pybb/templatetags/pybb_extras.py 2019-03-22 21:00:47 +0000 @@ -21,25 +21,21 @@ @register.inclusion_tag('pybb/last_posts.html', takes_context=True) def pybb_last_posts(context, number=8): - # Create a Queryset - last_posts = Post.objects.all().order_by( - '-created') + BASE_COUNT = 100 - # Permission dependent Queryset filtering + # Create permission dependent Querysets if pybb.views.allowed_for(context.request.user): - last_posts = last_posts.filter( - hidden=False)[:100] + last_posts = Post.objects.filter( + hidden=False)[:BASE_COUNT] else: - last_posts = last_posts.filter( - hidden=False, topic__forum__category__internal=False)[:100] + last_posts = Post.objects.official(limit=BASE_COUNT) check = [] answer = [] for post in last_posts: - if not post.topic.is_hidden: - if (post.topic_id not in check) and len(check) < number: - check = check + [post.topic_id] - answer = answer + [post] + if (post.topic_id not in check) and len(check) < number: + check = check + [post.topic_id] + answer = answer + [post] return { 'posts': answer, } === modified file 'pybb/views.py' --- pybb/views.py 2019-03-20 21:19:12 +0000 +++ pybb/views.py 2019-03-22 21:00:47 +0000 @@ -436,16 +436,8 @@ # Executed on every request (POST and GET) search_date = date.today() - timedelta(int(days)) - # Create a QuerySet ordered by date - last_posts = Post.objects.filter( - created__gte=search_date, - hidden=False, - topic__forum__category__internal=False - ).order_by('-created') - - # Exclude hidden topics. After this operation last_posts isn't a - # type of QuerySet anymore and django queries will not work - last_posts = [p for p in last_posts if not p.topic.is_hidden] + # Create a QuerySet with only official posts + last_posts = Post.objects.official(date_from=search_date) posts_count = len(last_posts)
_______________________________________________ Mailing list: https://launchpad.net/~widelands-dev Post to : widelands-dev@lists.launchpad.net Unsubscribe : https://launchpad.net/~widelands-dev More help : https://help.launchpad.net/ListHelp