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

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

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

        ret = False

        if method in ('', 'fuzzy'):
            # Do actual merge
            if self.subproject.has_template():
                # Merge on units level
                self.merge_translations(request, author, store, overwrite,
                                        (method == 'fuzzy'))
            else:
                # Merge on file level
                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()