~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/translations/browser/translationimportqueue.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-01-14 19:43:35 UTC
  • mfrom: (12177.10.4 bug-611217)
  • Revision ID: launchpad@pqm.canonical.com-20110114194335-52d5sjw6x0q0s653
[r=mars][ui=none][bug=611217] Ignore POFiles for obsolete POTemplates
        during upload approval.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
    ]
15
15
 
16
16
import os
17
 
from os.path import (
18
 
    basename,
19
 
    splitext,
20
 
    )
21
17
 
22
18
from zope.app.form.interfaces import ConversionError
23
19
from zope.component import getUtility
52
48
from lp.translations.enums import RosettaImportStatus
53
49
from lp.translations.interfaces.pofile import IPOFileSet
54
50
from lp.translations.interfaces.potemplate import IPOTemplateSet
 
51
from lp.translations.interfaces.translationimporter import (
 
52
    ITranslationImporter,
 
53
    )
55
54
from lp.translations.interfaces.translationimportqueue import (
56
55
    IEditTranslationImportQueueEntry,
57
56
    ITranslationImportQueue,
102
101
            return field_values
103
102
        # Fill the know values.
104
103
        field_values['path'] = self.context.path
105
 
        (fname, fext) = splitext(self.context.path)
106
 
        if fext.lower() == '.po':
 
104
 
 
105
        importer = getUtility(ITranslationImporter)
 
106
        if importer.isTemplateName(self.context.path):
 
107
            file_type = TranslationFileType.POT
 
108
        elif importer.isTranslationName(self.context.path):
107
109
            file_type = TranslationFileType.PO
108
 
        elif fext.lower() == '.pot':
109
 
            file_type = TranslationFileType.POT
110
110
        else:
111
111
            file_type = TranslationFileType.UNSPEC
112
112
        field_values['file_type'] = file_type
323
323
                productseries=self.context.productseries)
324
324
        return potemplate_subset
325
325
 
 
326
    def _findObjectionToFilePath(self, file_type, path):
 
327
        """Return textual objection, if any, to setting this file path."""
 
328
        importer = getUtility(ITranslationImporter)
 
329
        if file_type == TranslationFileType.POT:
 
330
            if not importer.isTemplateName(path):
 
331
                return "This filename is not appropriate for a template."
 
332
        else:
 
333
            if not importer.isTranslationName(path):
 
334
                return "This filename is not appropriate for a translation."
 
335
 
 
336
        if path == self.context.path:
 
337
            # No change, so no objections.
 
338
            return None
 
339
 
 
340
        # The Rosetta Expert decided to change the path of the file.
 
341
        # Before accepting such change, we should check first whether
 
342
        # there is already another entry with that path in the same
 
343
        # context (sourcepackagename/distroseries or productseries).
 
344
        # A duplicate name will confuse the auto-approval
 
345
        # process.
 
346
        if file_type == TranslationFileType.POT:
 
347
            potemplate_set = getUtility(IPOTemplateSet)
 
348
            existing_file = potemplate_set.getPOTemplateByPathAndOrigin(
 
349
                path, self.context.productseries, self.context.distroseries,
 
350
                self.context.sourcepackagename)
 
351
            already_exists = existing_file is not None
 
352
        else:
 
353
            pofile_set = getUtility(IPOFileSet)
 
354
            existing_files = pofile_set.getPOFilesByPathAndOrigin(
 
355
                path, self.context.productseries,
 
356
                self.context.distroseries,
 
357
                self.context.sourcepackagename)
 
358
            already_exists = not existing_files.is_empty()
 
359
 
 
360
        if already_exists:
 
361
            # We already have an IPOFile in this path, let's notify
 
362
            # the user about that so they choose another path.
 
363
            return "There is already a file in the given path."
 
364
 
 
365
        return None
 
366
 
326
367
    def _validatePath(self, file_type, path):
327
 
        path_changed = False # Flag for change_action
328
 
        if path == None or path.strip() == "":
329
 
            self.setFieldError('path', 'The file name is missing.')
 
368
        """Should the entry's path be updated?"""
 
369
        if path is None or path.strip() == "":
 
370
            self.setFieldError('path', "The file name is missing.")
 
371
            return False
 
372
 
 
373
        objection = self._findObjectionToFilePath(file_type, path)
 
374
        if objection is None:
 
375
            return True
330
376
        else:
331
 
            (fname, fext) = splitext(basename(path))
332
 
            if len(fname) == 0:
333
 
                self.setFieldError('path',
334
 
                                   'The file name is incomplete.')
335
 
            if (file_type == TranslationFileType.POT and
336
 
                    fext.lower() != '.pot' and fext.lower() != '.xpi'):
337
 
                self.setFieldError('path',
338
 
                                   'The file name must end with ".pot".')
339
 
            if (file_type == TranslationFileType.PO and
340
 
                    fext.lower() != '.po' and fext.lower() != '.xpi'):
341
 
                self.setFieldError('path',
342
 
                                   'The file name must end with ".po".')
343
 
 
344
 
            if self.context.path != path:
345
 
                # The Rosetta Expert decided to change the path of the file.
346
 
                # Before accepting such change, we should check first whether
347
 
                # there is already another entry with that path in the same
348
 
                # context (sourcepackagename/distroseries or productseries).
349
 
                if file_type == TranslationFileType.POT:
350
 
                    potemplate_set = getUtility(IPOTemplateSet)
351
 
                    existing_file = (
352
 
                        potemplate_set.getPOTemplateByPathAndOrigin(
353
 
                            path, self.context.productseries,
354
 
                            self.context.distroseries,
355
 
                            self.context.sourcepackagename))
356
 
                    already_exists = existing_file is not None
357
 
                else:
358
 
                    pofile_set = getUtility(IPOFileSet)
359
 
                    existing_files = pofile_set.getPOFilesByPathAndOrigin(
360
 
                        path, self.context.productseries,
361
 
                        self.context.distroseries,
362
 
                        self.context.sourcepackagename)
363
 
                    already_exists = not existing_files.is_empty()
364
 
 
365
 
                if already_exists:
366
 
                    # We already have an IPOFile in this path, let's notify
367
 
                    # the user about that so they choose another path.
368
 
                    self.setFieldError('path',
369
 
                        'There is already a file in the given path.')
370
 
                else:
371
 
                    # There is no other pofile in the given path for this
372
 
                    # context, let's change it as requested by admins.
373
 
                    path_changed = True
374
 
 
375
 
        return path_changed
 
377
            self.setFieldError('path', objection)
 
378
            return False
376
379
 
377
380
    def _validatePOT(self, data):
378
381
        name = data.get('name')