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
1=== modified file 'pybb/feeds.py'
2--- pybb/feeds.py 2018-12-11 13:44:01 +0000
3+++ pybb/feeds.py 2019-03-24 08:28:39 +0000
4@@ -56,15 +56,11 @@
5 title_template = 'pybb/feeds/posts_title.html'
6 description_template = 'pybb/feeds/posts_description.html'
7
8- all_objects = Post.objects.exclude(
9- topic__forum__category__internal=True).exclude(
10- topic__in=Post.hidden_topics.all()).filter(hidden=False)
11+ all_objects = Post.objects.public()
12
13 def items_for_object(self, obj):
14- # Latest posts for forum 'xy'
15- return Post.objects.exclude(
16- topic__in=Post.hidden_topics.all()).filter(
17- hidden=False, topic__forum=obj).order_by('-created')[:15]
18+ # Latest posts for forum 'xy'
19+ return Post.objects.public(limit=15)
20
21 # Validated through http://validator.w3.org/feed/
22
23
24=== modified file 'pybb/models.py'
25--- pybb/models.py 2018-12-21 09:43:02 +0000
26+++ pybb/models.py 2019-03-24 08:28:39 +0000
27@@ -255,6 +255,31 @@
28 except:
29 return []
30
31+class PublicPostsManager(models.Manager):
32+
33+ def public(self, limit=None, date_from=None):
34+ """Get public posts.
35+
36+ Filters out all posts which shouldn't be visible to
37+ normal visitors. The result is always orderd by the
38+ posts creation time, Descending. Optional arguments:
39+
40+ limit: Slice the QuerySet [:limit].
41+ date_from: Gathers all posts from this day until today.
42+ """
43+
44+ qs = self.get_queryset().filter(
45+ topic__forum__category__internal=False, hidden=False).exclude(
46+ topic__in=Post.hidden_topics.all()).order_by(
47+ '-created')
48+
49+ if date_from:
50+ qs = qs.filter(created__gte=date_from)
51+ if limit:
52+ qs = qs[:limit]
53+
54+ return qs
55+
56
57 class Post(RenderableItem):
58 topic = models.ForeignKey(
59@@ -270,8 +295,8 @@
60 body_text = models.TextField(_('Text version'))
61 hidden = models.BooleanField(_('Hidden'), blank=True, default=False)
62
63- objects = models.Manager() # Normal manager
64- hidden_topics = HiddenTopicsManager() # Custom manager
65+ objects = PublicPostsManager() # Normal manager, extended
66+ hidden_topics = HiddenTopicsManager() # Custom manager
67
68 class Meta:
69 ordering = ['created']
70
71=== modified file 'pybb/templatetags/pybb_extras.py'
72--- pybb/templatetags/pybb_extras.py 2019-03-20 21:32:32 +0000
73+++ pybb/templatetags/pybb_extras.py 2019-03-24 08:28:39 +0000
74@@ -21,25 +21,21 @@
75 @register.inclusion_tag('pybb/last_posts.html', takes_context=True)
76 def pybb_last_posts(context, number=8):
77
78- # Create a Queryset
79- last_posts = Post.objects.all().order_by(
80- '-created')
81+ BASE_COUNT = 100
82
83- # Permission dependent Queryset filtering
84+ # Create permission dependent Querysets
85 if pybb.views.allowed_for(context.request.user):
86- last_posts = last_posts.filter(
87- hidden=False)[:100]
88+ last_posts = Post.objects.filter(
89+ hidden=False).order_by('-created')[:BASE_COUNT]
90 else:
91- last_posts = last_posts.filter(
92- hidden=False, topic__forum__category__internal=False)[:100]
93+ last_posts = Post.objects.public(limit=BASE_COUNT)
94
95 check = []
96 answer = []
97 for post in last_posts:
98- if not post.topic.is_hidden:
99- if (post.topic_id not in check) and len(check) < number:
100- check = check + [post.topic_id]
101- answer = answer + [post]
102+ if (post.topic_id not in check) and len(check) < number:
103+ check = check + [post.topic_id]
104+ answer = answer + [post]
105 return {
106 'posts': answer,
107 }
108
109=== modified file 'pybb/views.py'
110--- pybb/views.py 2019-03-20 21:19:12 +0000
111+++ pybb/views.py 2019-03-24 08:28:39 +0000
112@@ -436,16 +436,8 @@
113 # Executed on every request (POST and GET)
114 search_date = date.today() - timedelta(int(days))
115
116- # Create a QuerySet ordered by date
117- last_posts = Post.objects.filter(
118- created__gte=search_date,
119- hidden=False,
120- topic__forum__category__internal=False
121- ).order_by('-created')
122-
123- # Exclude hidden topics. After this operation last_posts isn't a
124- # type of QuerySet anymore and django queries will not work
125- last_posts = [p for p in last_posts if not p.topic.is_hidden]
126+ # Create a QuerySet with only public posts
127+ last_posts = Post.objects.public(date_from=search_date)
128
129 posts_count = len(last_posts)
130

Subscribers

People subscribed via source and target branches