def merge_upload(self, request, fileobj, overwrite, author_name=None, author_email=None, method='translate', fuzzy=''): """Top level handler for file uploads.""" filecopy = fileobj.read() fileobj.close() # Strip possible UTF-8 BOM if filecopy[:3] == codecs.BOM_UTF8: filecopy = filecopy[3:] # Load backend file store = try_load( fileobj.name, filecopy, self.component.file_format_cls, self.component.template_store ) # Check valid plural forms if hasattr(store.store, 'parseheader'): header = store.store.parseheader() try: number, equation = Plural.parse_formula(header['Plural-Forms']) if not self.plural.same_plural(number, equation): raise Exception('Plural forms do not match the language.') except (ValueError, KeyError): # Formula wrong or missing pass # Optionally set authorship orig_user = None if author_email: from weblate.auth.models import User orig_user = request.user request.user = User.objects.get_or_create( email=author_email, defaults={ 'username': author_email, 'is_active': False, 'full_name': author_name or author_email, } )[0] try: if method in ('translate', 'fuzzy', 'approve'): # Merge on units level with self.component.repository.lock: return self.merge_translations( request, store, overwrite, method, fuzzy, ) # Add as sugestions return self.merge_suggestions(request, store, fuzzy) finally: if orig_user: request.user = orig_user
def merge_upload(self, request, fileobj, overwrite, author_name=None, author_email=None, method='translate', fuzzy=''): """Top level handler for file uploads.""" filecopy = fileobj.read() fileobj.close() # Strip possible UTF-8 BOM if filecopy[:3] == codecs.BOM_UTF8: filecopy = filecopy[3:] # Load backend file store = try_load( fileobj.name, filecopy, self.component.file_format_cls, self.component.template_store ) # Check valid plural forms if hasattr(store.store, 'parseheader'): header = store.store.parseheader() try: number, equation = Plural.parse_formula(header['Plural-Forms']) if not self.plural.same_plural(number, equation): raise Exception('Plural forms do not match the language.') except (ValueError, KeyError): # Formula wrong or missing pass # Optionally set authorship orig_user = None if author_email: from weblate.auth.models import User orig_user = request.user request.user = User.objects.get_or_create( email=author_email, defaults={ 'username': author_email, 'is_active': False, 'full_name': author_name or author_email, } )[0] try: if method in ('translate', 'fuzzy', 'approve'): # Merge on units level with self.component.repository.lock: return self.merge_translations( request, store, overwrite, method, fuzzy, ) # Add as sugestions return self.merge_suggestions(request, store, fuzzy) finally: if orig_user: request.user = orig_user
def merge_upload(self, request, fileobj, overwrite, author=None, merge_header=True, method='translate', fuzzy=''): """Top level handler for file uploads.""" filecopy = fileobj.read() fileobj.close() # Commit pending changes so far self.commit_pending(request) # Strip possible UTF-8 BOM if filecopy[:3] == codecs.BOM_UTF8: filecopy = filecopy[3:] # Load backend file store = try_load(fileobj.name, filecopy, self.component.file_format_cls, self.component.template_store) # Optionally set authorship if author is None: author = get_author_name(request.user) # Check valid plural forms if hasattr(store.store, 'parseheader'): header = store.store.parseheader() try: number, equation = Plural.parse_formula(header['Plural-Forms']) if not self.plural.same_plural(number, equation): raise Exception('Plural forms do not match the language.') except (ValueError, KeyError): # Formula wrong or missing pass if method in ('translate', 'fuzzy'): # Merge on units level with self.component.repository.lock: return self.merge_translations( request, store, overwrite, (method == 'fuzzy'), fuzzy, merge_header, ) # Add as sugestions return self.merge_suggestions(request, store, fuzzy)
def merge_upload(self, request, fileobj, overwrite, author=None, merge_header=True, method='translate', fuzzy=''): """Top level handler for file uploads.""" filecopy = fileobj.read() fileobj.close() # Commit pending changes so far self.commit_pending(request) # Strip possible UTF-8 BOM if filecopy[:3] == codecs.BOM_UTF8: filecopy = filecopy[3:] # Load backend file store = try_load( fileobj.name, filecopy, self.component.file_format_cls, self.component.template_store ) # Optionally set authorship if author is None: author = request.user.get_author_name() # Check valid plural forms if hasattr(store.store, 'parseheader'): header = store.store.parseheader() try: number, equation = Plural.parse_formula(header['Plural-Forms']) if not self.plural.same_plural(number, equation): raise Exception('Plural forms do not match the language.') except (ValueError, KeyError): # Formula wrong or missing pass if method in ('translate', 'fuzzy'): # Merge on units level with self.component.repository.lock: return self.merge_translations( request, store, overwrite, (method == 'fuzzy'), fuzzy, merge_header, ) # Add as sugestions return self.merge_suggestions(request, store, fuzzy)
def get_plural(self, language): """Return matching plural object.""" header = self.store.parseheader() try: number, equation = Plural.parse_formula(header['Plural-Forms']) except (ValueError, KeyError): return super(PoFormat, self).get_plural(language) # Find matching one for plural in language.plural_set.all(): if plural.same_plural(number, equation): return plural # Create new one return Plural.objects.create( language=language, source=Plural.SOURCE_GETTEXT, number=number, equation=equation, )
def get_plural(self, language): """Return matching plural object.""" from weblate.lang.models import Plural header = self.store.parseheader() try: number, equation = Plural.parse_formula(header['Plural-Forms']) except (ValueError, KeyError): return super(PoFormat, self).get_plural(language) # Find matching one for plural in language.plural_set.all(): if plural.same_plural(number, equation): return plural # Create new one return Plural.objects.create( language=language, source=Plural.SOURCE_GETTEXT, number=number, equation=equation, )
def get_plural(self, language): """Return matching plural object.""" from weblate.lang.models import Plural header = self.store.parseheader() try: number, equation = Plural.parse_formula(header["Plural-Forms"]) except (ValueError, KeyError): return super().get_plural(language) # Find matching one for plural in language.plural_set.iterator(): if plural.same_plural(number, equation): return plural # Create new one return Plural.objects.create( language=language, source=Plural.SOURCE_GETTEXT, number=number, equation=equation, )
def merge_upload( self, request, fileobj, overwrite, author_name=None, author_email=None, method="translate", fuzzy="", ): """Top level handler for file uploads.""" # Optionally set authorship orig_user = None if author_email: from weblate.auth.models import User orig_user = request.user request.user = User.objects.get_or_create( email=author_email, defaults={ "username": author_email, "is_active": False, "full_name": author_name or author_email, }, )[0] if method == "replace": return self.handle_replace(request, fileobj) filecopy = fileobj.read() fileobj.close() # Strip possible UTF-8 BOM if filecopy[:3] == codecs.BOM_UTF8: filecopy = filecopy[3:] # Load backend file store = try_load( fileobj.name, filecopy, self.component.file_format_cls, self.component.template_store, ) # Check valid plural forms if hasattr(store.store, "parseheader"): header = store.store.parseheader() try: number, equation = Plural.parse_formula(header["Plural-Forms"]) if not self.plural.same_plural(number, equation): raise PluralFormsMismatch() except (ValueError, KeyError): # Formula wrong or missing pass try: if method in ("translate", "fuzzy", "approve"): # Merge on units level with self.component.repository.lock: return self.merge_translations(request, store, overwrite, method, fuzzy) # Add as sugestions return self.merge_suggestions(request, store, fuzzy) finally: if orig_user: request.user = orig_user