Merge lp:~widelands-dev/widelands-website/official_posts into lp:widelands-website

Proposed by kaputtnik
Status: Merged
Merged at revision: 529
Proposed branch: lp:~widelands-dev/widelands-website/official_posts
Merge into: lp:widelands-website
Diff against target: 129 lines (+40/-31)
4 files modified
pybb/feeds.py (+3/-7)
pybb/models.py (+27/-2)
pybb/templatetags/pybb_extras.py (+8/-12)
pybb/views.py (+2/-10)
To merge this branch: bzr merge lp:~widelands-dev/widelands-website/official_posts
Reviewer Review Type Date Requested Status
GunChleoc Approve
kaputtnik (community) Needs Resubmitting
Review via email: mp+364989@code.launchpad.net

Commit message

Unify getting of official posts; performance tweaks

Description of the change

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.

To post a comment you must log in.
Revision history for this message
GunChleoc (gunchleoc) wrote :

I think the word "official" is confusing here - I associate public announcements and sticky topics with that. I think we should call them "public".

Code LGTM otherwise :)

533. By kaputtnik

refactored name of manager; fixed ordering

Revision history for this message
kaputtnik (franku) wrote :

Public is much better :-)

review: Needs Resubmitting
Revision history for this message
GunChleoc (gunchleoc) wrote :

Just 2 more string nits. Deploy any time :)

review: Approve
534. By kaputtnik

wording, spelling

Revision history for this message
kaputtnik (franku) wrote :

Thanks :-), merged and deployed

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'pybb/feeds.py'
--- pybb/feeds.py 2018-12-11 13:44:01 +0000
+++ pybb/feeds.py 2019-03-24 08:28:39 +0000
@@ -56,15 +56,11 @@
56 title_template = 'pybb/feeds/posts_title.html'56 title_template = 'pybb/feeds/posts_title.html'
57 description_template = 'pybb/feeds/posts_description.html'57 description_template = 'pybb/feeds/posts_description.html'
5858
59 all_objects = Post.objects.exclude(59 all_objects = Post.objects.public()
60 topic__forum__category__internal=True).exclude(
61 topic__in=Post.hidden_topics.all()).filter(hidden=False)
6260
63 def items_for_object(self, obj):61 def items_for_object(self, obj):
64 # Latest posts for forum 'xy' 62 # Latest posts for forum 'xy'
65 return Post.objects.exclude(63 return Post.objects.public(limit=15)
66 topic__in=Post.hidden_topics.all()).filter(
67 hidden=False, topic__forum=obj).order_by('-created')[:15]
6864
69# Validated through http://validator.w3.org/feed/65# Validated through http://validator.w3.org/feed/
7066
7167
=== modified file 'pybb/models.py'
--- pybb/models.py 2018-12-21 09:43:02 +0000
+++ pybb/models.py 2019-03-24 08:28:39 +0000
@@ -255,6 +255,31 @@
255 except:255 except:
256 return []256 return []
257257
258class PublicPostsManager(models.Manager):
259
260 def public(self, limit=None, date_from=None):
261 """Get public posts.
262
263 Filters out all posts which shouldn't be visible to
264 normal visitors. The result is always orderd by the
265 posts creation time, Descending. Optional arguments:
266
267 limit: Slice the QuerySet [:limit].
268 date_from: Gathers all posts from this day until today.
269 """
270
271 qs = self.get_queryset().filter(
272 topic__forum__category__internal=False, hidden=False).exclude(
273 topic__in=Post.hidden_topics.all()).order_by(
274 '-created')
275
276 if date_from:
277 qs = qs.filter(created__gte=date_from)
278 if limit:
279 qs = qs[:limit]
280
281 return qs
282
258283
259class Post(RenderableItem):284class Post(RenderableItem):
260 topic = models.ForeignKey(285 topic = models.ForeignKey(
@@ -270,8 +295,8 @@
270 body_text = models.TextField(_('Text version'))295 body_text = models.TextField(_('Text version'))
271 hidden = models.BooleanField(_('Hidden'), blank=True, default=False)296 hidden = models.BooleanField(_('Hidden'), blank=True, default=False)
272297
273 objects = models.Manager() # Normal manager 298 objects = PublicPostsManager() # Normal manager, extended
274 hidden_topics = HiddenTopicsManager() # Custom manager299 hidden_topics = HiddenTopicsManager() # Custom manager
275300
276 class Meta:301 class Meta:
277 ordering = ['created']302 ordering = ['created']
278303
=== 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-24 08:28:39 +0000
@@ -21,25 +21,21 @@
21@register.inclusion_tag('pybb/last_posts.html', takes_context=True)21@register.inclusion_tag('pybb/last_posts.html', takes_context=True)
22def pybb_last_posts(context, number=8):22def pybb_last_posts(context, number=8):
2323
24 # Create a Queryset24 BASE_COUNT = 100
25 last_posts = Post.objects.all().order_by(
26 '-created')
2725
28 # Permission dependent Queryset filtering26 # Create permission dependent Querysets
29 if pybb.views.allowed_for(context.request.user):27 if pybb.views.allowed_for(context.request.user):
30 last_posts = last_posts.filter(28 last_posts = Post.objects.filter(
31 hidden=False)[:100]29 hidden=False).order_by('-created')[:BASE_COUNT]
32 else:30 else:
33 last_posts = last_posts.filter(31 last_posts = Post.objects.public(limit=BASE_COUNT)
34 hidden=False, topic__forum__category__internal=False)[:100]
3532
36 check = []33 check = []
37 answer = []34 answer = []
38 for post in last_posts:35 for post in last_posts:
39 if not post.topic.is_hidden:36 if (post.topic_id not in check) and len(check) < number:
40 if (post.topic_id not in check) and len(check) < number:37 check = check + [post.topic_id]
41 check = check + [post.topic_id]38 answer = answer + [post]
42 answer = answer + [post]
43 return {39 return {
44 'posts': answer,40 'posts': answer,
45 }41 }
4642
=== modified file 'pybb/views.py'
--- pybb/views.py 2019-03-20 21:19:12 +0000
+++ pybb/views.py 2019-03-24 08:28:39 +0000
@@ -436,16 +436,8 @@
436 # Executed on every request (POST and GET)436 # Executed on every request (POST and GET)
437 search_date = date.today() - timedelta(int(days))437 search_date = date.today() - timedelta(int(days))
438438
439 # Create a QuerySet ordered by date439 # Create a QuerySet with only public posts
440 last_posts = Post.objects.filter(440 last_posts = Post.objects.public(date_from=search_date)
441 created__gte=search_date,
442 hidden=False,
443 topic__forum__category__internal=False
444 ).order_by('-created')
445
446 # Exclude hidden topics. After this operation last_posts isn't a
447 # type of QuerySet anymore and django queries will not work
448 last_posts = [p for p in last_posts if not p.topic.is_hidden]
449441
450 posts_count = len(last_posts)442 posts_count = len(last_posts)
451443

Subscribers

People subscribed via source and target branches