Exemple #1
0
    def merge_upload(self, request, fileobj, overwrite, author=None,
                     merge_header=True, method=''):
        '''
        Top level handler for file uploads.
        '''
        # Load backend file
        try:
            # First try using own loader
            store = self.subproject.file_format_cls(
                fileobj,
                self.subproject.template_store
            )
        except:
            # Fallback to automatic detection
            fileobj.seek(0)
            store = AutoFormat(fileobj)

        # Optionally set authorship
        if author is None:
            author = self.get_author_name(request.user)

        # List translations we should process
        translations = Translation.objects.filter(
            language=self.language,
            subproject__project=self.subproject.project
        )
        # Filter out those who don't want automatic update, but keep ourselves
        translations = translations.filter(
            Q(pk=self.pk) | Q(subproject__allow_translation_propagation=True)
        )

        ret = False

        if method in ('', 'fuzzy'):
            # Do actual merge
            for translation in translations:
                ret |= translation.merge_store(
                    request,
                    author,
                    store,
                    overwrite,
                    merge_header,
                    (method == 'fuzzy')
                )
        else:
            # Add as sugestions
            ret = self.merge_suggestions(request, store)

        return ret, store.count_units()
    def upload(self, project, language, fileobj, method):
        '''
        Handles dictionary update.
        '''
        ret = 0

        # Load file using translate-toolkit
        store = AutoFormat.load(fileobj)

        # process all units
        for unit in store.units:
            # We care only about translated things
            if not unit.istranslatable() or not unit.istranslated():
                continue

            # Ignore too long words
            if len(unit.source) > 200 or len(unit.target) > 200:
                continue

            # Get object
            word, created = self.get_or_create(
                project=project,
                language=language,
                source=unit.source
            )

            # Already existing entry found
            if not created:
                # Same as current -> ignore
                if unit.target == word.target:
                    continue
                if method == 'add':
                    # Add word
                    word = self.create(
                        project=project,
                        language=language,
                        source=unit.source
                    )
                elif method != 'overwrite':
                    # No overwriting or adding
                    continue

            # Store word
            word.target = unit.target
            word.save()

            ret += 1

        return ret
    def upload(self, project, language, fileobj, method):
        '''
        Handles dictionary update.
        '''
        # Load file using translate-toolkit
        store = AutoFormat.load(fileobj)

        ret, skipped = self.import_store(project, language, store, method)

        if ret == 0 and skipped > 0 and isinstance(store, csvfile):
            # Retry with different CSV scheme
            fileobj.seek(0)
            store = csvfile(fileobj, ('source', 'target'))
            ret, skipped = self.import_store(project, language, store, method)

        return ret
Exemple #4
0
    def merge_upload(self,
                     request,
                     fileobj,
                     overwrite,
                     author=None,
                     merge_header=True,
                     method=''):
        '''
        Top level handler for file uploads.
        '''
        # Load backend file
        try:
            # First try using own loader
            store = self.subproject.file_format_cls(
                fileobj, self.subproject.template_store)
        except:
            # Fallback to automatic detection
            store = AutoFormat(fileobj)

        # Optionally set authorship
        if author is None:
            author = self.get_author_name(request.user)

        # List translations we should process
        translations = Translation.objects.filter(
            language=self.language,
            subproject__project=self.subproject.project)
        # Filter out those who don't want automatic update, but keep ourselves
        translations = translations.filter(
            Q(pk=self.pk) | Q(subproject__allow_translation_propagation=True))

        ret = False

        if method in ('', 'fuzzy'):
            # Do actual merge
            for translation in translations:
                ret |= translation.merge_store(request, author, store,
                                               overwrite, merge_header,
                                               (method == 'fuzzy'))
        else:
            # Add as sugestions
            ret = self.merge_suggestions(request, store)

        return ret