def translate(self, request, new_target, new_fuzzy): ''' Stores new translation of a unit. ''' # Run AutoFixes on user input new_target, fixups = fix_target(new_target, self) # Update unit and save it self.target = join_plural(new_target) self.fuzzy = new_fuzzy saved = self.save_backend(request) return saved, fixups
def test_fix_target(self): unit = Unit(source=u'Foo…') fixed, fixups = fix_target(['Bar...'], unit) self.assertEquals(fixed, [u'Bar…']) self.assertEquals(len(fixups), 1) self.assertEquals(unicode(fixups[0]), u'Trailing ellipsis')
def handle_translate(obj, request, user_locked, this_unit_url, next_unit_url): ''' Saves translation or suggestion to database and backend. ''' # Antispam protection antispam = AntispamForm(request.POST) if not request.user.is_authenticated() and not antispam.is_valid(): # Silently redirect to next entry return HttpResponseRedirect(next_unit_url) form = TranslationForm(request.POST) if not form.is_valid(): return # Check whether translation is not outdated obj.check_sync() try: unit = Unit.objects.get_checksum( request, obj, form.cleaned_data['checksum'], ) except (Unit.DoesNotExist, IndexError): return if 'suggest' in request.POST: # Handle suggesion saving user = request.user if form.cleaned_data['target'][0] == '': messages.error(request, _('Your suggestion is empty!')) # Stay on same entry return HttpResponseRedirect(this_unit_url) # Invite user to become translator if there is nobody else recent_changes = Change.objects.content().filter( translation=unit.translation, ).exclude( user=None ) if not recent_changes.exists(): messages.info(request, _( 'There is currently no active translator for this ' 'translation, please consider becoming a translator ' 'as your suggestion might otherwise remain unreviewed.' )) # Create the suggestion Suggestion.objects.add( unit, join_plural(form.cleaned_data['target']), user, ) elif not request.user.is_authenticated(): # We accept translations only from authenticated messages.error( request, _('You need to log in to be able to save translations!') ) elif not request.user.has_perm('trans.save_translation'): # Need privilege to save messages.error( request, _('You don\'t have privileges to save translations!') ) elif not user_locked: # Run AutoFixes on user input new_target = form.cleaned_data['target'] new_target, fixups = fix_target(new_target, unit) if len(fixups) > 0: messages.info( request, _('Following fixups were applied to translation: %s') % ', '.join([unicode(f) for f in fixups]) ) # Remember old checks oldchecks = set( unit.active_checks().values_list('check', flat=True) ) # Update unit and save it unit.target = join_plural(new_target) unit.fuzzy = form.cleaned_data['fuzzy'] saved = unit.save_backend(request) if saved: # Get new set of checks newchecks = set( unit.active_checks().values_list('check', flat=True) ) # Did we introduce any new failures? if newchecks > oldchecks: # Show message to user messages.error( request, _('Some checks have failed on your translation!') ) # Stay on same entry return HttpResponseRedirect(this_unit_url) # Redirect to next entry return HttpResponseRedirect(next_unit_url)