Merge lp:~benji/launchpad/bug-590014 into lp:launchpad

Proposed by Benji York
Status: Merged
Approved by: Benji York
Approved revision: no longer in the source branch.
Merged at revision: 13624
Proposed branch: lp:~benji/launchpad/bug-590014
Merge into: lp:launchpad
Diff against target: 172 lines (+47/-37)
3 files modified
lib/lp/translations/browser/configure.zcml (+0/-14)
lib/lp/translations/browser/person.py (+45/-21)
lib/lp/translations/templates/person-translations.pt (+2/-2)
To merge this branch: bzr merge lp:~benji/launchpad/bug-590014
Reviewer Review Type Date Requested Status
Brad Crittenden (community) code Approve
Review via email: mp+70614@code.launchpad.net

Commit message

[r=bac][bug=590014] remove some duplicate queries

Description of the change

This branch reduces the number of queries needed to render the
translations page for a person by refactoring the code such that already
run queries are retained by cachedproperty decorators.

Tests for the view can be run with this command:

    bin/test -c -m lp.translations.browser.tests -t test_persontranslationview

Lint: the lint report is clean

QA: Compare translations.launchpad.net/~ and
translations.qastaging.launchpad.net/~ and make sure they look vaguely
similar (not that some lists are randomized so variances there are to be
expected).

To post a comment you must log in.
Revision history for this message
Brad Crittenden (bac) wrote :

I like these changes Benji. A good idea to not instantiate the view multiple times.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/translations/browser/configure.zcml'
2--- lib/lp/translations/browser/configure.zcml 2011-05-27 21:03:22 +0000
3+++ lib/lp/translations/browser/configure.zcml 2011-08-05 18:45:01 +0000
4@@ -772,20 +772,6 @@
5 permission="zope.Public"
6 template="../templates/person-translations-to-review.pt"
7 layer="lp.translations.publisher.TranslationsLayer"/>
8- <browser:page
9- for="lp.registry.interfaces.person.IPerson"
10- name="+translations-to-review-table"
11- class="lp.translations.browser.person.PersonTranslationView"
12- permission="zope.Public"
13- template="../templates/person-translations-to-review-table.pt"
14- layer="lp.translations.publisher.TranslationsLayer"/>
15- <browser:page
16- for="lp.registry.interfaces.person.IPerson"
17- name="+translations-to-complete-table"
18- class="lp.translations.browser.person.PersonTranslationView"
19- permission="zope.Public"
20- template="../templates/person-translations-to-complete-table.pt"
21- layer="lp.translations.publisher.TranslationsLayer"/>
22
23
24 <!-- Product views -->
25
26=== modified file 'lib/lp/translations/browser/person.py'
27--- lib/lp/translations/browser/person.py 2011-05-19 13:26:10 +0000
28+++ lib/lp/translations/browser/person.py 2011-08-05 18:45:01 +0000
29@@ -19,6 +19,7 @@
30 import urllib
31
32 import pytz
33+from z3c.ptcompat import ViewPageTemplateFile
34 from zope.app.form.browser import TextWidget
35 from zope.component import getUtility
36 from zope.interface import (
37@@ -134,12 +135,24 @@
38 "while listing activity for %s." % (
39 person.name, pofiletranslator.person.name))
40
41- self.date = pofiletranslator.date_last_touched
42-
43- pofile = pofiletranslator.pofile
44-
45- self.title = pofile.potemplate.translationtarget.title
46- self.url = compose_pofile_filter_url(pofile, person)
47+ self._person = person
48+ self._pofiletranslator = pofiletranslator
49+
50+ @cachedproperty
51+ def date(self):
52+ return self._pofiletranslator.date_last_touched
53+
54+ @cachedproperty
55+ def _pofile(self):
56+ return self._pofiletranslator.pofile
57+
58+ @cachedproperty
59+ def title(self):
60+ return self._pofile.potemplate.translationtarget.title
61+
62+ @cachedproperty
63+ def url(self):
64+ return compose_pofile_filter_url(self._pofile, self._person)
65
66
67 def person_is_reviewer(person):
68@@ -193,7 +206,10 @@
69 def __init__(self, *args, **kwargs):
70 super(PersonTranslationView, self).__init__(*args, **kwargs)
71 now = datetime.now(pytz.timezone('UTC'))
72- self.history_horizon = now - timedelta(90, 0, 0)
73+ # Down-to-the-second detail isn't important so the hope is that this
74+ # will result in faster queries (cache effects).
75+ today = now.replace(minute=0, second=0, microsecond=0)
76+ self.history_horizon = today - timedelta(90, 0, 0)
77
78 @property
79 def page_title(self):
80@@ -282,22 +298,17 @@
81 return not (
82 translationmessage.potmsgset.hide_translations_from_anonymous)
83
84- def _getTargetsForReview(self, max_fetch=None):
85+ @cachedproperty
86+ def _review_targets(self):
87 """Query and aggregate the top targets for review.
88
89- :param max_fetch: Maximum number of `POFile`s to fetch while
90- looking for these.
91- :return: a list of at most `max_fetch` translation targets.
92- Multiple `POFile`s may be aggregated together into a single
93- target.
94+ :return: a list of translation targets. Multiple `POFile`s may be
95+ aggregated together into a single target.
96 """
97 person = ITranslationsPerson(self.context)
98 pofiles = person.getReviewableTranslationFiles(
99 no_older_than=self.history_horizon)
100
101- if max_fetch is not None:
102- pofiles = pofiles[:max_fetch]
103-
104 return ReviewLinksAggregator().aggregate(pofiles)
105
106 def _suggestTargetsForReview(self, max_fetch):
107@@ -344,7 +355,7 @@
108 @cachedproperty
109 def all_projects_and_packages_to_review(self):
110 """Top projects and packages for this person to review."""
111- return self._getTargetsForReview()
112+ return self._review_targets
113
114 def _addToTargetsList(self, existing_targets, new_targets, max_items,
115 max_overall):
116@@ -390,10 +401,8 @@
117 list_length = 10
118
119 # Start out with the translations that the person has recently
120- # worked on. Aggregation may reduce the number we get, so ask
121- # the database for a few extra.
122- fetch = 5 * max_known_targets
123- recent = self._getTargetsForReview(fetch)
124+ # worked on.
125+ recent = self._review_targets
126 overall = self._addToTargetsList(
127 [], recent, max_known_targets, list_length)
128
129@@ -442,6 +451,21 @@
130 return overall
131
132
133+ to_complete_template = ViewPageTemplateFile(
134+ '../templates/person-translations-to-complete-table.pt')
135+
136+ def translations_to_complete_table(self):
137+ return self.to_complete_template(dict(view=self))
138+
139+
140+ to_review_template = ViewPageTemplateFile(
141+ '../templates/person-translations-to-review-table.pt')
142+
143+ def translations_to_review_table(self):
144+ return self.to_review_template(dict(view=self))
145+
146+
147+
148 class PersonTranslationReviewView(PersonTranslationView):
149 """View for translation-related Person pages."""
150
151
152=== modified file 'lib/lp/translations/templates/person-translations.pt'
153--- lib/lp/translations/templates/person-translations.pt 2011-07-01 15:24:41 +0000
154+++ lib/lp/translations/templates/person-translations.pt 2011-08-05 18:45:01 +0000
155@@ -55,7 +55,7 @@
156 By reviewing the following translations, you can help ensure other translators' work is published to software users. (<a href="/+help/reviewing.html" target="help">More about reviewing</a>)
157 </p>
158 <metal:translations-to-review
159- tal:replace="structure context/@@+translations-to-review-table" />
160+ tal:replace="structure view/translations_to_review_table" />
161 <div class="see-all"
162 tal:condition="python: view.num_projects_and_packages_to_review > 1">
163 <a href="+translations-to-review" id="translations-to-review-link">
164@@ -74,7 +74,7 @@
165 <div id="translations-to-complete-section" class="portlet">
166 <h2>Translations you can help complete</h2>
167 <metal:translations-to-complete
168- tal:replace="structure context/@@+translations-to-complete-table" />
169+ tal:replace="structure view/translations_to_complete_table" />
170 </div>
171 </tal:translator>
172 </tal:me>