def get_avatar_image(user, size): """ Returns avatar image from cache (if available) or downloads it. """ cache_key = u'avatar-img-{0}-{1}'.format( user.username, size ) # Try using avatar specific cache if available try: cache = caches['avatar'] except InvalidCacheBackendError: cache = caches['default'] image = cache.get(cache_key) if image is None: try: image = download_avatar_image(user, size) cache.set(cache_key, image) except IOError as error: report_error( error, sys.exc_info(), extra_data={'avatar': user.username} ) weblate.logger.error( 'Failed to fetch avatar for %s: %s', user.username, str(error) ) return get_fallback_avatar(size) return image
def upload_dictionary(request, project, lang): prj = get_project(request, project) lang = get_object_or_404(Language, code=lang) form = DictUploadForm(request.POST, request.FILES) if form.is_valid(): try: count = Dictionary.objects.upload( request, prj, lang, request.FILES['file'], form.cleaned_data['method'] ) import_message( request, count, _('No words to import found in file.'), ungettext( 'Imported %d word from the uploaded file.', 'Imported %d words from the uploaded file.', count ) ) except Exception as error: report_error(error, sys.exc_info(), request) messages.error( request, _('File upload has failed: %s') % force_text(error) ) else: messages.error(request, _('Failed to process form!')) return redirect( 'show_dictionary', project=prj.slug, lang=lang.code )
def get_avatar_image(user, size): """ Returns avatar image from cache (if available) or downloads it. """ cache_key = 'avatar-img-{0}-{1}'.format( user.username, size ) # Try using avatar specific cache if available try: cache = caches['avatar'] except InvalidCacheBackendError: cache = caches['default'] image = cache.get(cache_key) if image is None: try: image = download_avatar_image(user, size) cache.set(cache_key, image) except IOError as error: report_error( error, sys.exc_info(), extra_data={'avatar': user.username} ) LOGGER.error( 'Failed to fetch avatar for %s: %s', user.username, str(error) ) return get_fallback_avatar(size) return image
def upload_dictionary(request, project, lang): prj = get_project(request, project) lang = get_object_or_404(Language, code=lang) form = DictUploadForm(request.POST, request.FILES) if form.is_valid(): try: count = Dictionary.objects.upload(request, prj, lang, request.FILES['file'], form.cleaned_data['method']) if count == 0: messages.warning(request, _('No words to import found in file.')) else: messages.success( request, ungettext('Imported %d word from the uploaded file.', 'Imported %d words from the uploaded file.', count) % count) except Exception as error: report_error(error, sys.exc_info(), request) messages.error(request, _('File upload has failed: %s') % unicode(error)) else: messages.error(request, _('Failed to process form!')) return redirect('show_dictionary', project=prj.slug, lang=lang.code)
def get_avatar_image(request, user, size): """ Returns avatar image from cache (if available) or downloads it. """ cache_key = '-'.join(('avatar-img', user.username, str(size))) # Try using avatar specific cache if available try: cache = caches['avatar'] except InvalidCacheBackendError: cache = caches['default'] image = cache.get(cache_key) if image is None: try: image = download_avatar_image(user, size) cache.set(cache_key, image) except IOError as error: report_error( error, sys.exc_info(), request, extra_data={'avatar': user.username}, level='debug', ) LOGGER.error('Failed to fetch avatar for %s: %s', user.username, str(error)) return get_fallback_avatar(size) return image
def upload_translation(request, project, subproject, lang): ''' Handling of translation uploads. ''' obj = get_translation(request, project, subproject, lang) # Check method and lock if obj.is_locked(request.user): messages.error(request, _('Access denied.')) return redirect(obj) # Get correct form handler based on permissions form = get_upload_form(request.user, obj.subproject.project)(request.POST, request.FILES) # Check form validity if not form.is_valid(): messages.error(request, _('Please fix errors in the form.')) return redirect(obj) # Create author name author = None if (can_author_translation(request.user, obj.subproject.project) and form.cleaned_data['author_name'] != '' and form.cleaned_data['author_email'] != ''): author = '%s <%s>' % (form.cleaned_data['author_name'], form.cleaned_data['author_email']) # Check for overwriting overwrite = False if can_overwrite_translation(request.user, obj.subproject.project): overwrite = form.cleaned_data['overwrite'] # Do actual import try: ret, count = obj.merge_upload( request, request.FILES['file'], overwrite, author, merge_header=form.cleaned_data['merge_header'], merge_comments=form.cleaned_data['merge_comments'], method=form.cleaned_data['method'], fuzzy=form.cleaned_data['fuzzy'], ) messages.info( request, ungettext('Processed %d string from the uploaded files.', 'Processed %d strings from the uploaded files.', count) % count) if not ret: messages.warning(request, _('There were no new strings in uploaded file!')) except Exception as error: messages.error(request, _('File content merge failed: %s') % unicode(error)) report_error(error, sys.exc_info(), request) return redirect(obj)
def upload_translation(request, project, subproject, lang): """ Handling of translation uploads. """ obj = get_translation(request, project, subproject, lang) # Check method and lock if obj.is_locked(request.user): messages.error(request, _("Access denied.")) return redirect(obj) # Get correct form handler based on permissions form = get_upload_form(request)(request.POST, request.FILES) # Check form validity if not form.is_valid(): messages.error(request, _("Please fix errors in the form.")) return redirect(obj) # Create author name author = None if ( request.user.has_perm("trans.author_translation") and form.cleaned_data["author_name"] != "" and form.cleaned_data["author_email"] != "" ): author = "%s <%s>" % (form.cleaned_data["author_name"], form.cleaned_data["author_email"]) # Check for overwriting overwrite = False if request.user.has_perm("trans.overwrite_translation"): overwrite = form.cleaned_data["overwrite"] # Do actual import try: ret, count = obj.merge_upload( request, request.FILES["file"], overwrite, author, merge_header=form.cleaned_data["merge_header"], merge_comments=form.cleaned_data["merge_comments"], method=form.cleaned_data["method"], fuzzy=form.cleaned_data["fuzzy"], ) messages.info( request, ungettext( "Processed %d string from the uploaded files.", "Processed %d strings from the uploaded files.", count ) % count, ) if not ret: messages.warning(request, _("There were no new strings in uploaded file!")) except Exception as error: messages.error(request, _("File content merge failed: %s") % unicode(error)) report_error(error, sys.exc_info(), request) return redirect(obj)
def send_mails(mails): """Sends multiple mails in single connection.""" try: connection = get_connection() connection.send_messages(mails) except SMTPException as error: LOGGER.error('Failed to send email: %s', error) report_error(error, sys.exc_info())
def handle_parse_error(self, error): """Handler for parse error.""" report_error(error, sys.exc_info()) self.notify_merge_failure( str(error), u''.join(traceback.format_stack()), ) raise ParseError(str(error))
def get_words(self, unit): """ Returns list of word pairs for an unit. """ words = set() # Prepare analyzers # - standard analyzer simply splits words # - stemming extracts stems, to catch things like plurals analyzers = [ StandardAnalyzer(), StemmingAnalyzer(), ] source_language = unit.translation.subproject.project.source_language lang_code = source_language.base_code() # Add per language analyzer if Whoosh has it if has_stemmer(lang_code): analyzers.append(LanguageAnalyzer(lang_code)) # Add ngram analyzer for languages like Chinese or Japanese if source_language.uses_ngram(): analyzers.append(NgramAnalyzer(4)) # Extract words from all plurals and from context for text in unit.get_source_plurals() + [unit.context]: for analyzer in analyzers: # Some Whoosh analyzers break on unicode try: words.update( [token.text for token in analyzer(force_text(text))] ) except (UnicodeDecodeError, IndexError) as error: report_error(error, sys.exc_info()) # Grab all words in the dictionary dictionary = self.filter( project=unit.translation.subproject.project, language=unit.translation.language ) if len(words) == 0: # No extracted words, no dictionary dictionary = dictionary.none() else: # Build the query for fetching the words # Can not use __in as we want case insensitive lookup query = Q() for word in words: query |= Q(source__iexact=word) # Filter dictionary dictionary = dictionary.filter(query) return dictionary
def auth_complete(self, *args, **kwargs): try: return super(EmailAuth, self).auth_complete(*args, **kwargs) except AuthMissingParameter as error: if error.parameter == 'email': messages.error( self.strategy.request, _('Failed to verify your registration! ' 'Probably the verification token has expired. ' 'Please try the registration again.')) report_error(error, sys.exc_info(), extra_data=self.data) return redirect(reverse('login')) raise
def report_error(self, exc, message): """Wrapper for handling error situations""" report_error( exc, sys.exc_info(), {'mt_url': self.request_url, 'mt_params': self.request_params} ) LOGGER.error( message, self.name, ) LOGGER.error( 'Last fetched URL: %s, params: %s', self.request_url, self.request_params, )
def report_error(self, exc, message): """Wrapper for handling error situations""" report_error(exc, sys.exc_info(), { 'mt_url': self.request_url, 'mt_params': self.request_params }) LOGGER.error( message, self.name, ) LOGGER.error( 'Last fetched URL: %s, params: %s', self.request_url, self.request_params, )
def auth_complete(self, *args, **kwargs): try: return super(EmailAuth, self).auth_complete(*args, **kwargs) except AuthMissingParameter as error: if error.parameter == 'email': messages.error( self.strategy.request, _( 'Failed to verify your registration! ' 'Probably the verification token has expired. ' 'Please try the registration again.' ) ) report_error( error, sys.exc_info(), extra_data=self.data ) return redirect(reverse('login')) raise
def translate(self, language, text, unit, user): ''' Returns list of machine translations. ''' language = self.convert_language(language) if text == '': return [] if not self.is_supported(language): return [] try: translations = self.download_translations( language, text, unit, user ) return [ { 'text': trans[0], 'quality': trans[1], 'service': trans[2], 'source': trans[3] } for trans in translations ] except Exception as exc: report_error( exc, sys.exc_info(), {'mt_url': self.request_url, 'mt_params': self.request_params} ) LOGGER.error( 'Failed to fetch translations from %s', self.name, ) LOGGER.error( 'Last fetched URL: %s, params: %s', self.request_url, self.request_params, ) raise MachineTranslationError('{}: {}'.format( exc.__class__.__name__, str(exc) ))
def upload_dictionary(request, project, lang): prj = get_project(request, project) lang = get_object_or_404(Language, code=lang) form = DictUploadForm(request.POST, request.FILES) if form.is_valid(): try: count = Dictionary.objects.upload(request, prj, lang, request.FILES["file"], form.cleaned_data["method"]) if count == 0: messages.warning(request, _("No words to import found in file.")) else: messages.success( request, ungettext( "Imported %d word from the uploaded file.", "Imported %d words from the uploaded file.", count ) % count, ) except Exception as error: report_error(error, sys.exc_info(), request) messages.error(request, _("File upload has failed: %s") % unicode(error)) else: messages.error(request, _("Failed to process form!")) return redirect("show_dictionary", project=prj.slug, lang=lang.code)
def supported_languages(self): ''' Returns list of supported languages. ''' cache_key = '%s-languages' % self.mtid # Try using list from cache languages = cache.get(cache_key) if languages is not None: return languages # Download try: languages = self.download_languages() except Exception as exc: report_error( exc, sys.exc_info(), {'mt_url': self.request_url, 'mt_params': self.request_params} ) LOGGER.error( 'Failed to fetch languages from %s, using defaults', self.name, ) LOGGER.error( 'Last fetched URL: %s, params: %s', self.request_url, self.request_params, ) if settings.DEBUG: raise return self.default_languages # Update cache cache.set(cache_key, languages, 3600 * 48) return languages
def upload_translation(request, project, subproject, lang): ''' Handling of translation uploads. ''' obj = get_translation(request, project, subproject, lang) # Check method and lock if obj.is_locked(request.user): messages.error(request, _('Access denied.')) return redirect(obj) # Get correct form handler based on permissions form = get_upload_form(request)(request.POST, request.FILES) # Check form validity if not form.is_valid(): messages.error(request, _('Please fix errors in the form.')) return redirect(obj) # Create author name author = None if (request.user.has_perm('trans.author_translation') and form.cleaned_data['author_name'] != '' and form.cleaned_data['author_email'] != ''): author = '%s <%s>' % ( form.cleaned_data['author_name'], form.cleaned_data['author_email'] ) # Check for overwriting overwrite = False if request.user.has_perm('trans.overwrite_translation'): overwrite = form.cleaned_data['overwrite'] # Do actual import try: ret, count = obj.merge_upload( request, request.FILES['file'], overwrite, author, merge_header=form.cleaned_data['merge_header'], method=form.cleaned_data['method'], fuzzy=form.cleaned_data['fuzzy'], ) messages.info( request, ungettext( 'Processed %d string from the uploaded files.', 'Processed %d strings from the uploaded files.', count ) % count ) if not ret: messages.warning( request, _('There were no new strings in uploaded file!') ) except Exception as error: messages.error( request, _('File content merge failed: %s') % unicode(error) ) report_error(error, sys.exc_info(), request) return redirect(obj)