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
=== modified file 'lib/lp/translations/browser/configure.zcml'
--- lib/lp/translations/browser/configure.zcml 2011-05-27 21:03:22 +0000
+++ lib/lp/translations/browser/configure.zcml 2011-08-05 18:45:01 +0000
@@ -772,20 +772,6 @@
772 permission="zope.Public"772 permission="zope.Public"
773 template="../templates/person-translations-to-review.pt"773 template="../templates/person-translations-to-review.pt"
774 layer="lp.translations.publisher.TranslationsLayer"/>774 layer="lp.translations.publisher.TranslationsLayer"/>
775 <browser:page
776 for="lp.registry.interfaces.person.IPerson"
777 name="+translations-to-review-table"
778 class="lp.translations.browser.person.PersonTranslationView"
779 permission="zope.Public"
780 template="../templates/person-translations-to-review-table.pt"
781 layer="lp.translations.publisher.TranslationsLayer"/>
782 <browser:page
783 for="lp.registry.interfaces.person.IPerson"
784 name="+translations-to-complete-table"
785 class="lp.translations.browser.person.PersonTranslationView"
786 permission="zope.Public"
787 template="../templates/person-translations-to-complete-table.pt"
788 layer="lp.translations.publisher.TranslationsLayer"/>
789775
790776
791<!-- Product views -->777<!-- Product views -->
792778
=== modified file 'lib/lp/translations/browser/person.py'
--- lib/lp/translations/browser/person.py 2011-05-19 13:26:10 +0000
+++ lib/lp/translations/browser/person.py 2011-08-05 18:45:01 +0000
@@ -19,6 +19,7 @@
19import urllib19import urllib
2020
21import pytz21import pytz
22from z3c.ptcompat import ViewPageTemplateFile
22from zope.app.form.browser import TextWidget23from zope.app.form.browser import TextWidget
23from zope.component import getUtility24from zope.component import getUtility
24from zope.interface import (25from zope.interface import (
@@ -134,12 +135,24 @@
134 "while listing activity for %s." % (135 "while listing activity for %s." % (
135 person.name, pofiletranslator.person.name))136 person.name, pofiletranslator.person.name))
136137
137 self.date = pofiletranslator.date_last_touched138 self._person = person
138139 self._pofiletranslator = pofiletranslator
139 pofile = pofiletranslator.pofile140
140141 @cachedproperty
141 self.title = pofile.potemplate.translationtarget.title142 def date(self):
142 self.url = compose_pofile_filter_url(pofile, person)143 return self._pofiletranslator.date_last_touched
144
145 @cachedproperty
146 def _pofile(self):
147 return self._pofiletranslator.pofile
148
149 @cachedproperty
150 def title(self):
151 return self._pofile.potemplate.translationtarget.title
152
153 @cachedproperty
154 def url(self):
155 return compose_pofile_filter_url(self._pofile, self._person)
143156
144157
145def person_is_reviewer(person):158def person_is_reviewer(person):
@@ -193,7 +206,10 @@
193 def __init__(self, *args, **kwargs):206 def __init__(self, *args, **kwargs):
194 super(PersonTranslationView, self).__init__(*args, **kwargs)207 super(PersonTranslationView, self).__init__(*args, **kwargs)
195 now = datetime.now(pytz.timezone('UTC'))208 now = datetime.now(pytz.timezone('UTC'))
196 self.history_horizon = now - timedelta(90, 0, 0)209 # Down-to-the-second detail isn't important so the hope is that this
210 # will result in faster queries (cache effects).
211 today = now.replace(minute=0, second=0, microsecond=0)
212 self.history_horizon = today - timedelta(90, 0, 0)
197213
198 @property214 @property
199 def page_title(self):215 def page_title(self):
@@ -282,22 +298,17 @@
282 return not (298 return not (
283 translationmessage.potmsgset.hide_translations_from_anonymous)299 translationmessage.potmsgset.hide_translations_from_anonymous)
284300
285 def _getTargetsForReview(self, max_fetch=None):301 @cachedproperty
302 def _review_targets(self):
286 """Query and aggregate the top targets for review.303 """Query and aggregate the top targets for review.
287304
288 :param max_fetch: Maximum number of `POFile`s to fetch while305 :return: a list of translation targets. Multiple `POFile`s may be
289 looking for these.306 aggregated together into a single target.
290 :return: a list of at most `max_fetch` translation targets.
291 Multiple `POFile`s may be aggregated together into a single
292 target.
293 """307 """
294 person = ITranslationsPerson(self.context)308 person = ITranslationsPerson(self.context)
295 pofiles = person.getReviewableTranslationFiles(309 pofiles = person.getReviewableTranslationFiles(
296 no_older_than=self.history_horizon)310 no_older_than=self.history_horizon)
297311
298 if max_fetch is not None:
299 pofiles = pofiles[:max_fetch]
300
301 return ReviewLinksAggregator().aggregate(pofiles)312 return ReviewLinksAggregator().aggregate(pofiles)
302313
303 def _suggestTargetsForReview(self, max_fetch):314 def _suggestTargetsForReview(self, max_fetch):
@@ -344,7 +355,7 @@
344 @cachedproperty355 @cachedproperty
345 def all_projects_and_packages_to_review(self):356 def all_projects_and_packages_to_review(self):
346 """Top projects and packages for this person to review."""357 """Top projects and packages for this person to review."""
347 return self._getTargetsForReview()358 return self._review_targets
348359
349 def _addToTargetsList(self, existing_targets, new_targets, max_items,360 def _addToTargetsList(self, existing_targets, new_targets, max_items,
350 max_overall):361 max_overall):
@@ -390,10 +401,8 @@
390 list_length = 10401 list_length = 10
391402
392 # Start out with the translations that the person has recently403 # Start out with the translations that the person has recently
393 # worked on. Aggregation may reduce the number we get, so ask404 # worked on.
394 # the database for a few extra.405 recent = self._review_targets
395 fetch = 5 * max_known_targets
396 recent = self._getTargetsForReview(fetch)
397 overall = self._addToTargetsList(406 overall = self._addToTargetsList(
398 [], recent, max_known_targets, list_length)407 [], recent, max_known_targets, list_length)
399408
@@ -442,6 +451,21 @@
442 return overall451 return overall
443452
444453
454 to_complete_template = ViewPageTemplateFile(
455 '../templates/person-translations-to-complete-table.pt')
456
457 def translations_to_complete_table(self):
458 return self.to_complete_template(dict(view=self))
459
460
461 to_review_template = ViewPageTemplateFile(
462 '../templates/person-translations-to-review-table.pt')
463
464 def translations_to_review_table(self):
465 return self.to_review_template(dict(view=self))
466
467
468
445class PersonTranslationReviewView(PersonTranslationView):469class PersonTranslationReviewView(PersonTranslationView):
446 """View for translation-related Person pages."""470 """View for translation-related Person pages."""
447471
448472
=== modified file 'lib/lp/translations/templates/person-translations.pt'
--- lib/lp/translations/templates/person-translations.pt 2011-07-01 15:24:41 +0000
+++ lib/lp/translations/templates/person-translations.pt 2011-08-05 18:45:01 +0000
@@ -55,7 +55,7 @@
55 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>)55 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>)
56 </p>56 </p>
57 <metal:translations-to-review57 <metal:translations-to-review
58 tal:replace="structure context/@@+translations-to-review-table" />58 tal:replace="structure view/translations_to_review_table" />
59 <div class="see-all"59 <div class="see-all"
60 tal:condition="python: view.num_projects_and_packages_to_review > 1">60 tal:condition="python: view.num_projects_and_packages_to_review > 1">
61 <a href="+translations-to-review" id="translations-to-review-link">61 <a href="+translations-to-review" id="translations-to-review-link">
@@ -74,7 +74,7 @@
74 <div id="translations-to-complete-section" class="portlet">74 <div id="translations-to-complete-section" class="portlet">
75 <h2>Translations you can help complete</h2>75 <h2>Translations you can help complete</h2>
76 <metal:translations-to-complete76 <metal:translations-to-complete
77 tal:replace="structure context/@@+translations-to-complete-table" />77 tal:replace="structure view/translations_to_complete_table" />
78 </div>78 </div>
79 </tal:translator>79 </tal:translator>
80 </tal:me>80 </tal:me>