def translate(request, project, subproject, lang): ''' Generic entry point for translating, suggesting and searching. ''' obj = get_translation(request, project, subproject, lang) # Check locks project_locked, user_locked, own_lock = obj.is_locked(request, True) locked = project_locked or user_locked # Search results search_result = search(obj, request) # Handle redirects if isinstance(search_result, HttpResponse): return search_result # Get numer of results num_results = len(search_result['ids']) # Search offset try: offset = int(request.GET.get('offset', search_result.get('offset', 0))) except ValueError: offset = 0 # Check boundaries if offset < 0 or offset >= num_results: messages.info(request, _('You have reached end of translating.')) # Delete search del request.session['search_%s' % search_result['search_id']] # Redirect to translation return HttpResponseRedirect(obj.get_absolute_url()) # Some URLs we will most likely use base_unit_url = '%s?sid=%s&offset=' % ( obj.get_translate_url(), search_result['search_id'], ) this_unit_url = base_unit_url + str(offset) next_unit_url = base_unit_url + str(offset + 1) response = None # Any form submitted? if request.method == 'POST' and not project_locked: response = handle_translate( obj, request, user_locked, this_unit_url, next_unit_url ) # Handle translation merging elif 'merge' in request.GET and not locked: response = handle_merge(obj, request, next_unit_url) # Handle accepting/deleting suggestions elif not locked and ('accept' in request.GET or 'delete' in request.GET): response = handle_suggestions(obj, request, this_unit_url) # Pass possible redirect further if response is not None: return response # Grab actual unit try: unit = obj.unit_set.get(pk=search_result['ids'][offset]) except Unit.DoesNotExist: # Can happen when using SID for other translation messages.error(request, _('Invalid search string!')) return HttpResponseRedirect(obj.get_absolute_url()) # Show secondary languages for logged in users if request.user.is_authenticated(): profile = request.user.get_profile() secondary_langs = profile.secondary_languages.exclude( id=unit.translation.language.id ) project = unit.translation.subproject.project secondary = get_distinct_translations( Unit.objects.filter( checksum=unit.checksum, translated=True, translation__subproject__project=project, translation__language__in=secondary_langs, ) ) antispam = None else: secondary = None antispam = AntispamForm() # Prepare form form = TranslationForm(initial={ 'checksum': unit.checksum, 'target': (unit.translation.language, unit.get_target_plurals()), 'fuzzy': unit.fuzzy, }) return render_to_response( 'translate.html', RequestContext( request, { 'this_unit_url': this_unit_url, 'first_unit_url': base_unit_url + '0', 'last_unit_url': base_unit_url + str(num_results - 1), 'next_unit_url': next_unit_url, 'prev_unit_url': base_unit_url + str(offset - 1), 'object': obj, 'unit': unit, 'last_changes': unit.change_set.all()[:10], 'last_changes_rss': reverse( 'rss-translation', kwargs=obj.get_kwargs(), ), 'last_changes_url': urlencode(obj.get_kwargs()), 'total': obj.unit_set.all().count(), 'search_id': search_result['search_id'], 'offset': offset, 'filter_name': search_result['name'], 'filter_count': num_results, 'filter_pos': offset + 1, 'form': form, 'antispam': antispam, 'comment_form': CommentForm(), 'search_form': SearchForm(), 'update_lock': own_lock, 'secondary': secondary, 'locked': locked, 'user_locked': user_locked, 'project_locked': project_locked, }, ) )
def handle_translate(obj, request, user_locked, this_unit_url, next_unit_url): ''' Saves translation or suggestion to database and backend. ''' # Antispam protection if not request.user.is_authenticated(): antispam = AntispamForm(request.POST) if 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: 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: # Remember old checks oldchecks = set( unit.active_checks().values_list('check', flat=True) ) # Update unit and save it unit.target = join_plural(form.cleaned_data['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)