Merge lp:~jtv/launchpad/bug-662552-get-tm-or-dummy into lp:launchpad

Proposed by Jeroen T. Vermeulen on 2010-10-27
Status: Merged
Approved by: Jeroen T. Vermeulen on 2010-10-27
Approved revision: no longer in the source branch.
Merged at revision: 11811
Proposed branch: lp:~jtv/launchpad/bug-662552-get-tm-or-dummy
Merge into: lp:launchpad
Diff against target: 674 lines (+102/-182)
10 files modified
lib/lp/translations/browser/pofile.py (+3/-22)
lib/lp/translations/browser/tests/translationmessage-views.txt (+27/-15)
lib/lp/translations/browser/translationmessage.py (+17/-44)
lib/lp/translations/doc/canonical_url_examples.txt (+4/-4)
lib/lp/translations/doc/potmsgset.txt (+7/-29)
lib/lp/translations/doc/translationmessage.txt (+13/-12)
lib/lp/translations/interfaces/potmsgset.py (+8/-8)
lib/lp/translations/model/potmsgset.py (+7/-11)
lib/lp/translations/model/translationmessage.py (+0/-7)
lib/lp/translations/tests/test_potmsgset.py (+16/-30)
To merge this branch: bzr merge lp:~jtv/launchpad/bug-662552-get-tm-or-dummy
Reviewer Review Type Date Requested Status
Edwin Grubbs (community) code 2010-10-27 Approve on 2010-10-27
Review via email: mp+39467@code.launchpad.net

Commit message

Streamline IPOTMsgSet.getCurrentDummyTranslationMessage.

Description of the change

= Bug 662552: Streamline getCurrentDummyTranslationMessage =

We're getting timeouts on the POFile:+translate page, so I'm fixing up a bunch of relatively non-invasive things that it wastes time on.

In this case, it's IPOTMsgSet.getCurrentDummyTranslationMessage. This method returns a DummyTranslationMessage. With a few easily fixed exceptions in one doctest, every use of this method follows the same basic pattern:

    message = potmsgset.getCurrentTranslationMessage(
        pofile.potemplate, pofile.language)
    if message is None:
        message = potmsgset.getCurrentDummyTranslationMessage(
            pofile.potemplate, pofile.language)
    else:
        message.setPOFile(pofile)

(The message.setPOFile() part is a hack to get around the fact that some code still relies on TranslationMessage.pofile even though it's been removed from the schema; we now use an alternate which is meant to be valid only within the request).

In this branch I replace the entire usage pattern with a single new method:

    message = potmsgset.getCurrentTranslationMessageOrDummy(
        pofile)

I also eliminated a few assertions that guarded the integrity of this pattern along the way, doing several redundant database lookups to ensure that there is no real current translation message before creating a dummy. That pattern is now explicit, and enshrined in a single place in the code.

About half of the branch is actually lint cleanup. There's a bit of lint left, but that's all related to blank lines around free-standing comment blocks. I'm not yet convinced that "make lint" has a creditable complaint.

Jeroen

To post a comment you must log in.
Edwin Grubbs (edwin-grubbs) wrote :

Hi Jeroen,

This branch looks good. I just have one comment on an preexisting typo.

-Edwin

>=== modified file 'lib/lp/translations/browser/tests/translationmessage-views.txt'
>--- lib/lp/translations/browser/tests/translationmessage-views.txt 2010-10-18 22:24:59 +0000
>+++ lib/lp/translations/browser/tests/translationmessage-views.txt 2010-10-27 20:18:48 +0000
>@@ -1,4 +1,5 @@
>-= TranslationMessage View =
>+TranslationMessage View
>+=======================
>
> On this section, we are going to test the view class for an
> ITranslationMessage object.
>@@ -11,13 +12,14 @@
> >>> from lp.services.worlddata.interfaces.language import ILanguageSet
> >>> from lp.translations.publisher import TranslationsLayer
>
>-All the tests will be submitted as comming from Kurem, an editor for the POFile
>-that we are going to edit.
>+All the tests will be submitted as comming from Kurem, an editor for the

s/comming/coming/

>+POFile that we are going to edit.
>
> >>> login('<email address hidden>')
>
>
>-== No plural forms ==
>+No plural forms
>+---------------
>
> We are going to see what happens if we get an entry for a language
> without the plural form information.

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/pofile.py'
2--- lib/lp/translations/browser/pofile.py 2010-08-31 11:11:09 +0000
3+++ lib/lp/translations/browser/pofile.py 2010-10-28 03:59:47 +0000
4@@ -87,20 +87,7 @@
5 raise NotFoundError(
6 "%r is not a valid sequence number." % name)
7
8- # Need to check in our database whether we have already the requested
9- # TranslationMessage.
10- translationmessage = potmsgset.getCurrentTranslationMessage(
11- self.context.potemplate, self.context.language)
12-
13- if translationmessage is not None:
14- # Already have a valid POMsgSet entry, just return it.
15- translationmessage.setPOFile(self.context)
16- return translationmessage
17- else:
18- # Get a fake one so we don't create new TranslationMessage just
19- # because someone is browsing the web.
20- return potmsgset.getCurrentDummyTranslationMessage(
21- self.context.potemplate, self.context.language)
22+ return potmsgset.getCurrentTranslationMessageOrDummy(self.context)
23
24
25 class POFileFacets(POTemplateFacets):
26@@ -846,14 +833,8 @@
27 "POTMsgSets on page not in ascending sequence order")
28 last = potmsgset
29
30- translationmessage = potmsgset.getCurrentTranslationMessage(
31- self.context.potemplate, self.context.language)
32- if translationmessage is None:
33- translationmessage = (
34- potmsgset.getCurrentDummyTranslationMessage(
35- self.context.potemplate, self.context.language))
36- else:
37- translationmessage.setPOFile(self.context)
38+ translationmessage = (
39+ potmsgset.getCurrentTranslationMessageOrDummy(self.context))
40 view = self._prepareView(
41 CurrentTranslationMessageView, translationmessage,
42 self.errors.get(potmsgset))
43
44=== modified file 'lib/lp/translations/browser/tests/translationmessage-views.txt'
45--- lib/lp/translations/browser/tests/translationmessage-views.txt 2010-10-18 22:24:59 +0000
46+++ lib/lp/translations/browser/tests/translationmessage-views.txt 2010-10-28 03:59:47 +0000
47@@ -1,4 +1,5 @@
48-= TranslationMessage View =
49+TranslationMessage View
50+=======================
51
52 On this section, we are going to test the view class for an
53 ITranslationMessage object.
54@@ -11,13 +12,14 @@
55 >>> from lp.services.worlddata.interfaces.language import ILanguageSet
56 >>> from lp.translations.publisher import TranslationsLayer
57
58-All the tests will be submitted as comming from Kurem, an editor for the POFile
59-that we are going to edit.
60+All the tests will be submitted as coming from Kurem, an editor for the
61+POFile that we are going to edit.
62
63 >>> login('kurem@debian.cz')
64
65
66-== No plural forms ==
67+No plural forms
68+---------------
69
70 We are going to see what happens if we get an entry for a language
71 without the plural form information.
72@@ -29,8 +31,7 @@
73 >>> potmsgset = pofile_tlh.potemplate.getPOTMsgSetByMsgIDText(
74 ... u'evolution addressbook')
75 >>> current_translationmessage = (
76- ... potmsgset.getCurrentDummyTranslationMessage(
77- ... pofile_tlh.potemplate, pofile_tlh.language))
78+ ... potmsgset.getCurrentTranslationMessageOrDummy(pofile_tlh))
79 >>> translationmessage_page_view = create_view(
80 ... current_translationmessage, "+translate", layer=TranslationsLayer)
81 >>> translationmessage_page_view.initialize()
82@@ -46,7 +47,8 @@
83 False
84
85
86-== Basic checks ==
87+Basic checks
88+------------
89
90 Now, we will use objects that we have in our database, instead of
91 dummy ones.
92@@ -79,7 +81,8 @@
93 False
94
95
96-== The subview: TranslationMessageView ==
97+The subview: TranslationMessageView
98+-----------------------------------
99
100 For the next tests, we grab the subview which is what holds information
101 that pertains to the POMsgSet rendering itself:
102@@ -145,7 +148,8 @@
103 AssertionError: There is no plural form #1 for Spanish (es) language
104
105
106-== Web presentation ==
107+Web presentation
108+----------------
109
110 Some characters are presented specially in the Web interface, and there are
111 functions to determine whether to advise translators about their presence.
112@@ -226,7 +230,8 @@
113 >>> transaction.commit()
114
115
116-== Submitting translations ==
117+Submitting translations
118+-----------------------
119
120 A new translation is submitted through the view.
121
122@@ -330,7 +335,8 @@
123 [u'Foo']
124
125
126-== Bogus translation submission ==
127+Bogus translation submission
128+----------------------------
129
130 What would happen if we get a submit for another msgset that isn't being
131 considered?
132@@ -362,7 +368,8 @@
133 True
134
135
136-== TranslationMessageSuggestions ==
137+TranslationMessageSuggestions
138+-----------------------------
139
140 This class keeps all suggestions available for a concrete
141 ITranslationMessage.
142@@ -529,7 +536,8 @@
143 0
144
145
146-== Sequence number of new shared POTMsgSets ==
147+Sequence number of new shared POTMsgSets
148+----------------------------------------
149
150 Newly added shared POTMsgSets don't have their sequence field set, but
151 they do have sequence number when being displayed with translation
152@@ -556,7 +564,9 @@
153 >>> subview.sequence
154 1
155
156-== Ordering with unset potemplate values ==
157+
158+Ordering with unset potemplate values
159+-------------------------------------
160
161 Fix for bug #371560: can be removed after message sharing cleanup is done.
162 Code still uses potmsgset.potemplate when getting a sequence number, and
163@@ -596,7 +606,9 @@
164 ... server_url=server_url)
165 >>> pofile_view.initialize()
166
167-== Sharing and diverging messages ==
168+
169+Sharing and diverging messages
170+------------------------------
171
172 When there is an existing shared translation, one gets an option
173 to diverge it when on a zoomed-in view (when looking that particular
174
175=== modified file 'lib/lp/translations/browser/translationmessage.py'
176--- lib/lp/translations/browser/translationmessage.py 2010-09-03 16:01:01 +0000
177+++ lib/lp/translations/browser/translationmessage.py 2010-10-28 03:59:47 +0000
178@@ -67,10 +67,6 @@
179 )
180 from lp.translations.interfaces.translationsperson import ITranslationsPerson
181
182-#
183-# Exceptions and helper classes
184-#
185-
186
187 class POTMsgSetBatchNavigator(BatchNavigator):
188
189@@ -143,9 +139,6 @@
190 return contents
191
192
193-#
194-# Standard UI classes
195-#
196 class CurrentTranslationMessageFacets(POTemplateFacets):
197 usedfor = ITranslationMessage
198
199@@ -176,9 +169,6 @@
200 return Link('../+export', text, icon='download')
201
202
203-#
204-# Views
205-#
206 class CurrentTranslationMessageIndexView:
207 """A view to forward to the translation form."""
208
209@@ -298,16 +288,16 @@
210
211 if self.request.method == 'POST':
212 if self.user is None:
213- raise UnexpectedFormData, (
214- 'Anonymous users or users who are not accepting our '
215- 'licensing terms cannot do POST submissions.')
216+ raise UnexpectedFormData(
217+ "Anonymous users or users who are not accepting our "
218+ "licensing terms cannot do POST submissions.")
219 translations_person = ITranslationsPerson(self.user)
220 if (translations_person.translations_relicensing_agreement
221 is not None and
222 not translations_person.translations_relicensing_agreement):
223- raise UnexpectedFormData, (
224- 'Users who do not agree to licensing terms '
225- 'cannot do POST submissions.')
226+ raise UnexpectedFormData(
227+ "Users who do not agree to licensing terms "
228+ "cannot do POST submissions.")
229 try:
230 # Try to get the timestamp when the submitted form was
231 # created. We use it to detect whether someone else updated
232@@ -318,9 +308,9 @@
233 except zope_datetime.DateTimeError:
234 # invalid format. Either we don't have the timestamp in the
235 # submitted form or it has the wrong format.
236- raise UnexpectedFormData, (
237- 'We didn\'t find the timestamp that tells us when was'
238- ' generated the submitted form.')
239+ raise UnexpectedFormData(
240+ "We didn't find the timestamp that tells us when was"
241+ " generated the submitted form.")
242
243 # Check if this is really the form we are listening for..
244 if self.request.form.get("submit_translations"):
245@@ -543,7 +533,7 @@
246 elif fallback_language is not None:
247 # If there's a standard alternative language and no
248 # user-specified language was provided, preselect it.
249- alternative_language = fallback_language
250+ alternative_language = fallback_language
251 second_lang_code = fallback_language.code
252 else:
253 # The second_lang_code is None and there is no fallback_language.
254@@ -685,12 +675,8 @@
255 # current translation, suggestion or the new translation
256 # field.
257 current_translation_message = (
258- potmsgset.getCurrentTranslationMessage(
259- self.pofile.potemplate, self.pofile.language))
260- if current_translation_message is None:
261- current_translation_message = (
262- potmsgset.getCurrentDummyTranslationMessage(
263- self.pofile.potemplate, self.pofile.language))
264+ potmsgset.getCurrentTranslationMessageOrDummy(
265+ self.pofile))
266 if (selected_translation_key !=
267 msgset_ID_LANGCODE_translation_PLURALFORM_new):
268 # It's either current translation or an existing
269@@ -890,14 +876,6 @@
270 template = ViewPageTemplateFile(
271 '../templates/currenttranslationmessage-translate-one.pt')
272
273- # Relevant instance variables:
274- # self.translations
275- # self.error
276- # self.sec_lang
277- # self.second_lang_potmsgset
278- # self.suggestion_blocks
279- # self.pluralform_indices
280-
281 def __init__(self, current_translation_message, request,
282 plural_indices_to_store, translations, force_suggestion,
283 force_diverge, error, second_lang_code, form_is_writeable):
284@@ -1053,7 +1031,7 @@
285
286 diverged_and_have_shared = (
287 self.context.potemplate is not None and
288- self.shared_translationmessage is not None)
289+ self.shared_translationmessage is not None)
290 if diverged_and_have_shared:
291 pofile = self.shared_translationmessage.ensureBrowserPOFile()
292 if pofile is None:
293@@ -1152,10 +1130,10 @@
294
295 def _setOnePOFile(self, messages):
296 """Return a list of messages that all have a browser_pofile set.
297-
298+
299 If a pofile cannot be found for a message, it is not included in
300 the resulting list.
301- """
302+ """
303 result = []
304 for message in messages:
305 if message.browser_pofile is None:
306@@ -1167,7 +1145,7 @@
307 message.setPOFile(pofile)
308 result.append(message)
309 return result
310-
311+
312 def _buildAllSuggestions(self):
313 """Builds all suggestions and puts them into suggestions_block.
314
315@@ -1503,7 +1481,6 @@
316 return "%s_dismissable_button" % self.html_id
317
318
319-
320 class CurrentTranslationMessageZoomedView(CurrentTranslationMessageView):
321 """A view that displays a `TranslationMessage`, but zoomed in.
322
323@@ -1533,11 +1510,6 @@
324 return None
325
326
327-#
328-# Pseudo-content class
329-#
330-
331-
332 class TranslationMessageSuggestions:
333 """See `ITranslationMessageSuggestions`."""
334
335@@ -1591,6 +1563,7 @@
336 class Submission:
337 """A submission generated from a TranslationMessage"""
338
339+
340 def convert_translationmessage_to_submission(
341 message, current_message, plural_form, pofile, legal_warning_needed,
342 is_empty=False, packaged=False, local_to_pofile=False):
343
344=== modified file 'lib/lp/translations/doc/canonical_url_examples.txt'
345--- lib/lp/translations/doc/canonical_url_examples.txt 2010-07-30 12:56:27 +0000
346+++ lib/lp/translations/doc/canonical_url_examples.txt 2010-10-28 03:59:47 +0000
347@@ -71,8 +71,8 @@
348 Even for a dummy one.
349
350 >>> potmsgset = potemplate.getPOTMsgSetBySequence(20)
351- >>> translationmessage = potmsgset.getCurrentDummyTranslationMessage(
352- ... pofile.potemplate, pofile.language)
353+ >>> translationmessage = potmsgset.getCurrentTranslationMessageOrDummy(
354+ ... pofile)
355 >>> print canonical_url(translationmessage)
356 http://transl.../hoary/+source/evolution/+pots/evolution-2.2/es/20
357
358@@ -109,8 +109,8 @@
359 Even for a dummy PO msgset
360
361 >>> potmsgset = potemplate.getPOTMsgSetBySequence(20)
362- >>> translationmessage = potmsgset.getCurrentDummyTranslationMessage(
363- ... pofile.potemplate, pofile.language)
364+ >>> translationmessage = potmsgset.getCurrentTranslationMessageOrDummy(
365+ ... pofile)
366 >>> print canonical_url(translationmessage)
367 http://translations.../evolution/trunk/+pots/evolution-2.2/es/20
368
369
370=== modified file 'lib/lp/translations/doc/potmsgset.txt'
371--- lib/lp/translations/doc/potmsgset.txt 2010-10-26 10:31:37 +0000
372+++ lib/lp/translations/doc/potmsgset.txt 2010-10-28 03:59:47 +0000
373@@ -478,8 +478,8 @@
374 ... evolution_potemplate, pt_BR_dummypofile.language)
375 >>> print current
376 None
377- >>> pt_BR_dummy_current = potmsgset.getCurrentDummyTranslationMessage(
378- ... evolution_potemplate, pt_BR_dummypofile.language)
379+ >>> pt_BR_dummy_current = potmsgset.getCurrentTranslationMessageOrDummy(
380+ ... pt_BR_dummypofile)
381 >>> pt_BR_dummy_current.plural_forms
382 1
383 >>> pt_BR_dummy_current.translations
384@@ -508,8 +508,8 @@
385 >>> print apa_dummypofile.language.pluralforms
386 None
387 >>> apa_dummy_current = (
388- ... plural_potmsgset.getCurrentDummyTranslationMessage(
389- ... evolution_potemplate, apa_dummypofile.language))
390+ ... plural_potmsgset.getCurrentTranslationMessageOrDummy(
391+ ... apa_dummypofile))
392 >>> apa_dummy_current.plural_forms
393 2
394 >>> apa_dummy_current.translations
395@@ -519,8 +519,9 @@
396
397 >>> language_ru = getUtility(ILanguageSet).getLanguageByCode('ru')
398 >>> ru_dummypofile = evolution_potemplate.getDummyPOFile(language_ru)
399- >>> ru_dummy_current = plural_potmsgset.getCurrentDummyTranslationMessage(
400- ... evolution_potemplate, ru_dummypofile.language)
401+ >>> ru_dummy_current = (
402+ ... plural_potmsgset.getCurrentTranslationMessageOrDummy(
403+ ... ru_dummypofile))
404
405 >>> print ru_dummypofile.language.pluralforms
406 3
407@@ -925,29 +926,6 @@
408 0
409
410
411-POTMsgSet.getCurrentDummyTranslationMessage
412--------------------------------------------
413-
414-Sometimes, there are POTMsgSet objects with no translations to a language,
415-and we need to get dummy objects which emulate them to do read operations.
416-This method give us such dummy objects.
417-
418- >>> spanish_in_mexico = getUtility(ILanguageSet).getLanguageByCode(
419- ... 'es_MX')
420- >>> potmsgset.getCurrentDummyTranslationMessage(
421- ... evolution_potemplate, spanish_in_mexico) is None
422- False
423-
424-But, if we already have a TranslationMessage for a POTMsgSet in our database,
425-and we request a dummy one, that's broken and we detect it.
426-
427- >>> potmsgset.getCurrentDummyTranslationMessage(
428- ... evolution_potemplate, spanish)
429- Traceback (most recent call last):
430- ...
431- AssertionError: There is already a translation message ...
432-
433-
434 Suggestions for translator credits
435 ----------------------------------
436
437
438=== modified file 'lib/lp/translations/doc/translationmessage.txt'
439--- lib/lp/translations/doc/translationmessage.txt 2010-10-18 22:24:59 +0000
440+++ lib/lp/translations/doc/translationmessage.txt 2010-10-28 03:59:47 +0000
441@@ -10,6 +10,7 @@
442 >>> from lp.translations.interfaces.translationmessage import (
443 ... ITranslationMessage)
444 >>> from lp.translations.interfaces.translator import ITranslatorSet
445+ >>> from lp.translations.model.pofile import DummyPOFile
446
447 >>> login('carlos@canonical.com')
448 >>> pofile_es = factory.makePOFile(language_code='es')
449@@ -18,16 +19,15 @@
450
451 This class links the translations submitted by a translator with the
452 associated POFile and POTMsgSet. TranslationMessage and
453-DummyTranslationMessage both implement ITranslationMessage interface:
454+DummyTranslationMessage both implement ITranslationMessage:
455
456 >>> translationmessage = factory.makeTranslationMessage(
457 ... potmsgset=potmsgset, pofile=pofile_es)
458 >>> verifyObject(ITranslationMessage, translationmessage)
459 True
460
461- >>> serbian = getUtility(ILanguageSet)['sr']
462- >>> dummy_message = potmsgset.getCurrentDummyTranslationMessage(
463- ... potemplate, serbian)
464+ >>> dummy_message = potmsgset.getCurrentTranslationMessageOrDummy(
465+ ... factory.makePOFile('xh'))
466 >>> verifyObject(ITranslationMessage, dummy_message)
467 True
468
469@@ -48,18 +48,19 @@
470 the translation, no matter the number of plural forms defined for the
471 language:
472
473+ >>> serbian = getUtility(ILanguageSet)['sr']
474 >>> serbian.pluralforms
475 3
476- >>> current_sr = potmsgset.getCurrentDummyTranslationMessage(
477- ... potemplate, serbian)
478+ >>> current_sr = potmsgset.getCurrentTranslationMessageOrDummy(
479+ ... DummyPOFile(potemplate, serbian))
480 >>> current_sr.plural_forms
481 1
482
483 >>> divehi = getUtility(ILanguageSet)['dv']
484 >>> print divehi.pluralforms
485 None
486- >>> current_dv = potmsgset.getCurrentDummyTranslationMessage(
487- ... potemplate, divehi)
488+ >>> current_dv = potmsgset.getCurrentTranslationMessageOrDummy(
489+ ... DummyPOFile(potemplate, divehi))
490 >>> current_dv.plural_forms
491 1
492
493@@ -73,8 +74,8 @@
494 u'plural'
495 >>> serbian.pluralforms
496 3
497- >>> current_sr = potmsgset_plural.getCurrentDummyTranslationMessage(
498- ... potemplate, serbian)
499+ >>> current_sr = potmsgset_plural.getCurrentTranslationMessageOrDummy(
500+ ... DummyPOFile(potemplate, serbian))
501 >>> current_sr.plural_forms
502 3
503
504@@ -83,8 +84,8 @@
505
506 >>> print divehi.pluralforms
507 None
508- >>> current_dv = potmsgset_plural.getCurrentDummyTranslationMessage(
509- ... potemplate, divehi)
510+ >>> current_dv = potmsgset_plural.getCurrentTranslationMessageOrDummy(
511+ ... DummyPOFile(potemplate, divehi))
512 >>> current_dv.plural_forms
513 2
514
515
516=== modified file 'lib/lp/translations/interfaces/potmsgset.py'
517--- lib/lp/translations/interfaces/potmsgset.py 2010-08-20 20:31:18 +0000
518+++ lib/lp/translations/interfaces/potmsgset.py 2010-10-28 03:59:47 +0000
519@@ -65,6 +65,7 @@
520 class BrokenTextError(ValueError):
521 """Exception raised when we detect values on a text that aren't valid."""
522
523+
524 class POTMsgSetInIncompatibleTemplatesError(Exception):
525 """Raised when a POTMsgSet appears in multiple incompatible templates.
526
527@@ -134,14 +135,13 @@
528 queries that search for credits messages.
529 """))
530
531- def getCurrentDummyTranslationMessage(potemplate, language):
532- """Return a DummyTranslationMessage for this message language.
533-
534- :param potemplate: PO template you want a translation message for.
535- :param language: language we want a dummy translations for.
536-
537- If a TranslationMessage for this language already exists,
538- an exception is raised.
539+ def getCurrentTranslationMessageOrDummy(pofile):
540+ """Return the current `TranslationMessage`, or a dummy.
541+
542+ :param pofile: PO template you want a translation message for.
543+ :return: The current translation for `self` in `pofile`, if
544+ there is one. Otherwise, a `DummyTranslationMessage` for
545+ `self` in `pofile`.
546 """
547
548 def getCurrentTranslationMessage(potemplate, language):
549
550=== modified file 'lib/lp/translations/model/potmsgset.py'
551--- lib/lp/translations/model/potmsgset.py 2010-10-27 07:56:41 +0000
552+++ lib/lp/translations/model/potmsgset.py 2010-10-28 03:59:47 +0000
553@@ -42,7 +42,6 @@
554 from canonical.launchpad.interfaces.lpstorm import ISlaveStore
555 from canonical.launchpad.readonly import is_read_only
556 from lp.app.errors import UnexpectedFormData
557-from lp.translations.interfaces.pofile import IPOFileSet
558 from lp.translations.interfaces.potmsgset import (
559 BrokenTextError,
560 IPOTMsgSet,
561@@ -237,18 +236,15 @@
562 else:
563 return self.msgid_plural.msgid
564
565- def getCurrentDummyTranslationMessage(self, potemplate, language):
566+ def getCurrentTranslationMessageOrDummy(self, pofile):
567 """See `IPOTMsgSet`."""
568-
569- pofile = potemplate.getPOFileByLang(language.code)
570- if pofile is None:
571- pofileset = getUtility(IPOFileSet)
572- pofile = pofileset.getDummy(potemplate, language)
573+ current = self.getCurrentTranslationMessage(
574+ pofile.potemplate, pofile.language)
575+ if current is None:
576+ return DummyTranslationMessage(pofile, self)
577 else:
578- assert self.getCurrentTranslationMessage(potemplate,
579- language) is None, (
580- 'There is already a translation message in our database.')
581- return DummyTranslationMessage(pofile, self)
582+ current.setPOFile(pofile)
583+ return current
584
585 def _getUsedTranslationMessage(self, potemplate, language, current=True):
586 """Get a translation message which is either used in
587
588=== modified file 'lib/lp/translations/model/translationmessage.py'
589--- lib/lp/translations/model/translationmessage.py 2010-10-04 22:56:09 +0000
590+++ lib/lp/translations/model/translationmessage.py 2010-10-28 03:59:47 +0000
591@@ -115,13 +115,6 @@
592 implements(ITranslationMessage)
593
594 def __init__(self, pofile, potmsgset):
595- # Check whether we already have a suitable TranslationMessage, in
596- # which case, the dummy one must not be used.
597- assert potmsgset.getCurrentTranslationMessage(
598- pofile.potemplate,
599- pofile.language) is None, (
600- 'This translation message already exists in the database.')
601-
602 self.id = None
603 self.browser_pofile = pofile
604 self.potemplate = pofile.potemplate
605
606=== modified file 'lib/lp/translations/tests/test_potmsgset.py'
607--- lib/lp/translations/tests/test_potmsgset.py 2010-10-26 10:31:37 +0000
608+++ lib/lp/translations/tests/test_potmsgset.py 2010-10-28 03:59:47 +0000
609@@ -13,17 +13,13 @@
610 import pytz
611 import transaction
612 from zope.component import getUtility
613-from zope.security.proxy import (
614- isinstance as zope_isinstance,
615- removeSecurityProxy,
616- )
617+from zope.security.proxy import removeSecurityProxy
618
619 from canonical.launchpad.interfaces.launchpad import ILaunchpadCelebrities
620 from canonical.testing.layers import ZopelessDatabaseLayer
621 from lp.app.enums import ServiceUsage
622 from lp.registry.interfaces.person import IPersonSet
623 from lp.registry.interfaces.product import IProductSet
624-from lp.services.worlddata.interfaces.language import ILanguageSet
625 from lp.testing import TestCaseWithFactory
626 from lp.translations.interfaces.potemplate import IPOTemplateSet
627 from lp.translations.interfaces.potmsgset import (
628@@ -165,31 +161,21 @@
629 translation.potemplate = self.devel_potemplate
630 self.assertEquals(potmsgset.singular_text, ENGLISH_STRING)
631
632- def test_getCurrentDummyTranslationMessage(self):
633- """Test that a DummyTranslationMessage is correctly returned."""
634-
635- # When there is no POFile, we get a DummyTranslationMessage inside
636- # a DummyPOFile.
637- serbian = getUtility(ILanguageSet).getLanguageByCode('sr')
638- dummy = self.potmsgset.getCurrentDummyTranslationMessage(
639- self.devel_potemplate, serbian)
640- self.assertTrue(zope_isinstance(dummy, DummyTranslationMessage))
641-
642- # If a POFile exists, but there is no current translation message,
643- # a dummy translation message is returned.
644- sr_pofile = self.factory.makePOFile('sr', self.devel_potemplate)
645- dummy = self.potmsgset.getCurrentDummyTranslationMessage(
646- self.devel_potemplate, serbian)
647- self.assertTrue(zope_isinstance(dummy, DummyTranslationMessage))
648-
649- # When there is a current translation message, an exception
650- # is raised.
651- translation = self.factory.makeTranslationMessage(
652- pofile=sr_pofile, potmsgset=self.potmsgset)
653- self.assertTrue(translation.is_current)
654- self.assertRaises(AssertionError,
655- self.potmsgset.getCurrentDummyTranslationMessage,
656- self.devel_potemplate, serbian)
657+ def test_getCurrentTranslationMessageOrDummy_returns_real_tm(self):
658+ pofile = self.factory.makePOFile('nl')
659+ message = self.factory.makeTranslationMessage(
660+ pofile=pofile, suggestion=False, is_imported=True)
661+
662+ self.assertEqual(
663+ message,
664+ message.potmsgset.getCurrentTranslationMessageOrDummy(pofile))
665+
666+ def test_getCurrentTranslationMessageOrDummy_returns_dummy_tm(self):
667+ pofile = self.factory.makePOFile('nl')
668+ potmsgset = self.factory.makePOTMsgSet(pofile.potemplate)
669+
670+ message = potmsgset.getCurrentTranslationMessageOrDummy(pofile)
671+ self.assertIsInstance(message, DummyTranslationMessage)
672
673 def test_getCurrentTranslationMessage(self):
674 """Test how shared and diverged current translation messages