def handle_translate(request, translation, this_unit_url, next_unit_url): """Save translation or suggestion to database and backend.""" # Antispam protection antispam = AntispamForm(request.POST) if not antispam.is_valid(): # Silently redirect to next entry return HttpResponseRedirect(next_unit_url) form = TranslationForm(request.user, translation, None, request.POST) if not form.is_valid(): show_form_errors(request, form) return None unit = form.cleaned_data["unit"] go_next = True if "suggest" in request.POST: go_next = perform_suggestion(unit, form, request) elif not request.user.has_perm("unit.edit", unit): messages.error(request, _("Insufficient privileges for saving translations.")) else: go_next = perform_translation(unit, form, request) # Redirect to next entry if go_next: return HttpResponseRedirect(next_unit_url) return HttpResponseRedirect(this_unit_url)
def handle_translate(request, translation, this_unit_url, next_unit_url): """Save translation or suggestion to database and backend.""" # Antispam protection antispam = AntispamForm(request.POST) if not antispam.is_valid(): # Silently redirect to next entry return HttpResponseRedirect(next_unit_url) form = TranslationForm( request.user, translation, None, request.POST ) if not form.is_valid(): show_form_errors(request, form) return None unit = form.cleaned_data['unit'] go_next = True if 'suggest' in request.POST: go_next = perform_suggestion(unit, form, request) elif not request.user.has_perm('unit.edit', unit): messages.error( request, _('Insufficient privileges for saving translations.') ) else: go_next = perform_translation(unit, form, request) # Redirect to next entry if go_next: return HttpResponseRedirect(next_unit_url) return HttpResponseRedirect(this_unit_url)
def handle_translate(translation, 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 antispam.is_valid(): # Silently redirect to next entry return HttpResponseRedirect(next_unit_url) # Check whether translation is not outdated translation.check_sync() form = TranslationForm(translation, None, request.POST) if not form.is_valid(): return unit = form.cleaned_data['unit'] go_next = True if 'suggest' in request.POST: go_next = perform_suggestion(unit, form, request) elif (translation.is_template() and not request.user.has_perm('trans.save_template')): # Need privilege to save messages.error( request, _('You don\'t have privileges to save templates!') ) 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 (unit.only_vote_suggestions() and not request.user.has_perm('trans.override_suggestion')): messages.error( request, _('Only suggestions are allowed in this translation!') ) elif not user_locked: # Custom commit message message = request.POST.get('commit_message') if message and message != unit.translation.commit_message: # Commit pending changes so that they don't get new message unit.translation.commit_pending(request, request.user) # Store new commit message unit.translation.commit_message = message unit.translation.save() go_next = perform_translation(unit, form, request) # Redirect to next entry if go_next: return HttpResponseRedirect(next_unit_url) else: return HttpResponseRedirect(this_unit_url)
def handle_translate(translation, 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 antispam.is_valid(): # Silently redirect to next entry return HttpResponseRedirect(next_unit_url) # Check whether translation is not outdated translation.check_sync() form = TranslationForm(translation, None, request.POST) if not form.is_valid(): return unit = form.cleaned_data['unit'] go_next = True if 'suggest' in request.POST: go_next = perform_suggestion(unit, form, request) 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 (unit.only_vote_suggestions() and not request.user.has_perm('trans.save_translation')): messages.error( request, _('Only suggestions are allowed in this translation!') ) elif not user_locked: # Custom commit message if ('commit_message' in request.POST and request.POST['commit_message'] != unit.translation.commit_message): # Commit pending changes so that they don't get new message unit.translation.commit_pending(request, request.user) # Store new commit message unit.translation.commit_message = request.POST['commit_message'] unit.translation.save() go_next = perform_translation(unit, form, request) # Redirect to next entry if go_next: return HttpResponseRedirect(next_unit_url) else: return HttpResponseRedirect(this_unit_url)
def handle_translate(translation, request, user_locked, this_unit_url, next_unit_url): """Save translation or suggestion to database and backend.""" # Antispam protection antispam = AntispamForm(request.POST) if not antispam.is_valid(): # Silently redirect to next entry return HttpResponseRedirect(next_unit_url) # Check whether translation is not outdated translation.check_sync() form = TranslationForm( request.user.profile, translation, None, request.POST ) if not form.is_valid(): show_form_errors(request, form) return unit = form.cleaned_data['unit'] go_next = True if 'suggest' in request.POST: go_next = perform_suggestion(unit, form, request) elif not can_translate(request.user, unit.translation): messages.error( request, _('You don\'t have privileges to save translations!') ) elif not user_locked: # Custom commit message message = request.POST.get('commit_message') if message is not None and message != unit.translation.commit_message: # Commit pending changes so that they don't get new message unit.translation.commit_pending(request, request.user) # Store new commit message unit.translation.commit_message = message unit.translation.save() go_next = perform_translation(unit, form, request) # Redirect to next entry if go_next: return HttpResponseRedirect(next_unit_url) else: return HttpResponseRedirect(this_unit_url)
def handle_translate(translation, 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 antispam.is_valid(): # Silently redirect to next entry return HttpResponseRedirect(next_unit_url) # Check whether translation is not outdated translation.check_sync() form = TranslationForm(translation, None, request.POST) if not form.is_valid(): return unit = form.cleaned_data["unit"] go_next = True if "suggest" in request.POST: go_next = perform_suggestion(unit, form, request) 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 unit.only_vote_suggestions() and not request.user.has_perm("trans.save_translation"): messages.error(request, _("Only suggestions are allowed in this translation!")) elif not user_locked: # Custom commit message if "commit_message" in request.POST and request.POST["commit_message"] != unit.translation.commit_message: # Commit pending changes so that they don't get new message unit.translation.commit_pending(request, request.user) # Store new commit message unit.translation.commit_message = request.POST["commit_message"] unit.translation.save() go_next = perform_translation(unit, form, request) # Redirect to next entry if go_next: return HttpResponseRedirect(next_unit_url) else: return HttpResponseRedirect(this_unit_url)
def handle_translate(translation, request, this_unit_url, next_unit_url): """Save translation or suggestion to database and backend.""" # Antispam protection antispam = AntispamForm(request.POST) if not antispam.is_valid(): # Silently redirect to next entry return HttpResponseRedirect(next_unit_url) form = TranslationForm( request.user, translation, None, request.POST ) if not form.is_valid(): show_form_errors(request, form) return None unit = form.cleaned_data['unit'] go_next = True if 'suggest' in request.POST: go_next = perform_suggestion(unit, form, request) elif not request.user.has_perm('unit.edit', unit): messages.error( request, _('Insufficient privileges for saving translations.') ) else: # Custom commit message message = request.POST.get('commit_message') if message is not None and message != unit.translation.commit_message: # Commit pending changes so that they don't get new message unit.translation.commit_pending('commit message', request) # Store new commit message unit.translation.commit_message = message unit.translation.save() go_next = perform_translation(unit, form, request) # Redirect to next entry if go_next: return HttpResponseRedirect(next_unit_url) return HttpResponseRedirect(this_unit_url)
def handle_translate(translation, request, this_unit_url, next_unit_url): """Save translation or suggestion to database and backend.""" # Antispam protection antispam = AntispamForm(request.POST) if not antispam.is_valid(): # Silently redirect to next entry return HttpResponseRedirect(next_unit_url) form = TranslationForm( request.user, translation, None, request.POST ) if not form.is_valid(): show_form_errors(request, form) return None unit = form.cleaned_data['unit'] go_next = True if 'suggest' in request.POST: go_next = perform_suggestion(unit, form, request) elif not request.user.has_perm('unit.edit', unit): messages.error( request, _('Insufficient privileges for saving translations.') ) else: # Custom commit message message = request.POST.get('commit_message') if message is not None and message != unit.translation.commit_message: # Commit pending changes so that they don't get new message unit.translation.commit_pending(request) # Store new commit message unit.translation.commit_message = message unit.translation.save() go_next = perform_translation(unit, form, request) # Redirect to next entry if go_next: return HttpResponseRedirect(next_unit_url) return HttpResponseRedirect(this_unit_url)
def translate(request, project, subproject, lang): obj = get_object_or_404(Translation, language__code = lang, subproject__slug = subproject, subproject__project__slug = project, enabled = True) # Check locks locked = obj.is_locked(request) if request.user.is_authenticated(): profile = request.user.get_profile() antispam = None else: profile = None antispam = AntispamForm() secondary = None unit = None rqtype, direction, pos, search_query, search_exact, search_source, search_target, search_context, search_url = parse_search_url(request) # Any form submitted? if request.method == 'POST': # Antispam protection if not request.user.is_authenticated(): antispam = AntispamForm(request.POST) if not antispam.is_valid(): # Silently redirect to next entry return HttpResponseRedirect('%s?type=%s&pos=%d%s' % ( obj.get_translate_url(), rqtype, pos, search_url )) form = TranslationForm(request.POST) if form.is_valid() and not locked: # Check whether translation is not outdated obj.check_sync() try: try: unit = Unit.objects.get(checksum = form.cleaned_data['checksum'], translation = obj) except Unit.MultipleObjectsReturned: # Possible temporary inconsistency caused by ongoing update of repo, # let's pretend everyting is okay unit = Unit.objects.filter(checksum = form.cleaned_data['checksum'], translation = obj)[0] if 'suggest' in request.POST: # Handle suggesion saving user = request.user if isinstance(user, AnonymousUser): user = None if form.cleaned_data['target'] == len(form.cleaned_data['target']) * ['']: messages.error(request, _('Your suggestion is empty!')) # Stay on same entry return HttpResponseRedirect('%s?type=%s&pos=%d&dir=stay%s' % ( obj.get_translate_url(), rqtype, pos, search_url )) # Create the suggestion sug = Suggestion.objects.create( target = join_plural(form.cleaned_data['target']), checksum = unit.checksum, language = unit.translation.language, project = unit.translation.subproject.project, user = user) # Notify subscribed users from weblate.accounts.models import Profile subscriptions = Profile.objects.subscribed_new_suggestion(obj.subproject.project, obj.language) for subscription in subscriptions: subscription.notify_new_suggestion(obj, sug) # Update suggestion stats if profile is not None: profile.suggested += 1 profile.save() 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!')) else: # 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'] unit.save_backend(request) # Update stats profile.translated += 1 profile.save() # 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('%s?type=%s&pos=%d&dir=stay%s' % ( obj.get_translate_url(), rqtype, pos, search_url )) # Redirect to next entry return HttpResponseRedirect('%s?type=%s&pos=%d%s' % ( obj.get_translate_url(), rqtype, pos, search_url )) except Unit.DoesNotExist: logger.error('message %s disappeared!', form.cleaned_data['checksum']) messages.error(request, _('Message you wanted to translate is no longer available!')) # Handle translation merging if 'merge' in request.GET and not locked: if not request.user.has_perm('trans.save_translation'): # Need privilege to save messages.error(request, _('You don\'t have privileges to save translations!')) else: try: mergeform = MergeForm(request.GET) if mergeform.is_valid(): try: unit = Unit.objects.get(checksum = mergeform.cleaned_data['checksum'], translation = obj) except Unit.MultipleObjectsReturned: # Possible temporary inconsistency caused by ongoing update of repo, # let's pretend everyting is okay unit = Unit.objects.filter(checksum = mergeform.cleaned_data['checksum'], translation = obj)[0] merged = Unit.objects.get(pk = mergeform.cleaned_data['merge']) if unit.checksum != merged.checksum: messages.error(request, _('Can not merge different messages!')) else: unit.target = merged.target unit.fuzzy = merged.fuzzy unit.save_backend(request) # Update stats profile.translated += 1 profile.save() # Redirect to next entry return HttpResponseRedirect('%s?type=%s&pos=%d%s' % ( obj.get_translate_url(), rqtype, pos, search_url )) except Unit.DoesNotExist: logger.error('message %s disappeared!', form.cleaned_data['checksum']) messages.error(request, _('Message you wanted to translate is no longer available!')) # Handle accepting/deleting suggestions if not locked and ('accept' in request.GET or 'delete' in request.GET): # Check for authenticated users if not request.user.is_authenticated(): messages.error(request, _('You need to log in to be able to manage suggestions!')) return HttpResponseRedirect('%s?type=%s&pos=%d&dir=stay%s' % ( obj.get_translate_url(), rqtype, pos, search_url )) # Parse suggestion ID if 'accept' in request.GET: if not request.user.has_perm('trans.accept_suggestion'): messages.error(request, _('You do not have privilege to accept suggestions!')) return HttpResponseRedirect('%s?type=%s&pos=%d&dir=stay%s' % ( obj.get_translate_url(), rqtype, pos, search_url )) sugid = request.GET['accept'] else: if not request.user.has_perm('trans.delete_suggestion'): messages.error(request, _('You do not have privilege to delete suggestions!')) return HttpResponseRedirect('%s?type=%s&pos=%d&dir=stay%s' % ( obj.get_translate_url(), rqtype, pos, search_url )) sugid = request.GET['delete'] try: sugid = int(sugid) suggestion = Suggestion.objects.get(pk = sugid) except: suggestion = None if suggestion is not None: if 'accept' in request.GET: # Accept suggesiont suggestion.accept(request) # Invalidate caches for unit in Unit.objects.filter(checksum = suggestion.checksum): unit.translation.invalidate_cache('suggestions') # Delete suggestion in both cases (accepted ones are no longer needed) suggestion.delete() else: messages.error(request, _('Invalid suggestion!')) # Redirect to same entry for possible editing return HttpResponseRedirect('%s?type=%s&pos=%d&dir=stay%s' % ( obj.get_translate_url(), rqtype, pos, search_url )) reviewform = ReviewForm(request.GET) if reviewform.is_valid(): allunits = obj.unit_set.review(reviewform.cleaned_data['date'], request.user) # Review if direction == 'stay': units = allunits.filter(position = pos) elif direction == 'back': units = allunits.filter(position__lt = pos).order_by('-position') else: units = allunits.filter(position__gt = pos) elif search_query != '': # Apply search conditions if search_exact: query = Q() if search_source: query |= Q(source = search_query) if search_target: query |= Q(target = search_query) if search_context: query |= Q(context = search_query) allunits = obj.unit_set.filter(query) else: allunits = obj.unit_set.search(search_query, search_source, search_context, search_target) if direction == 'stay': units = obj.unit_set.filter(position = pos) elif direction == 'back': units = allunits.filter(position__lt = pos).order_by('-position') else: units = allunits.filter(position__gt = pos) else: allunits = obj.unit_set.filter_type(rqtype, obj) # What unit set is about to show if direction == 'stay': units = obj.unit_set.filter(position = pos) elif direction == 'back': units = allunits.filter(position__lt = pos).order_by('-position') else: units = allunits.filter(position__gt = pos) # If we failed to get unit above or on no POST if unit is None: # Grab actual unit try: unit = units[0] except IndexError: messages.info(request, _('You have reached end of translating.')) return HttpResponseRedirect(obj.get_absolute_url()) # Show secondary languages for logged in users if profile: secondary = Unit.objects.filter( checksum = unit.checksum, translated = True, translation__subproject__project = unit.translation.subproject.project, translation__language__in = profile.secondary_languages.exclude(id = unit.translation.language.id) ) # distinct('target') works with Django 1.4 so let's emulate that # based on presumption we won't get too many results targets = {} res = [] for s in secondary: if s.target in targets: continue targets[s.target] = 1 res.append(s) secondary = res # Prepare form form = TranslationForm(initial = { 'checksum': unit.checksum, 'target': (unit.translation.language, unit.get_target_plurals()), 'fuzzy': unit.fuzzy, }) total = obj.unit_set.all().count() filter_count = allunits.count() return render_to_response('translate.html', RequestContext(request, { 'object': obj, 'unit': unit, 'changes': unit.change_set.all()[:10], 'total': total, 'type': rqtype, 'filter_name': get_filter_name(rqtype, search_query), 'filter_count': filter_count, 'filter_pos': filter_count + 1 - units.count(), 'form': form, 'antispam': antispam, 'target_language': obj.language.code, 'secondary': secondary, 'search_query': search_query, 'search_url': search_url, 'search_query': search_query, 'search_source': bool2str(search_source), 'search_exact': bool2str(search_exact), 'search_target': bool2str(search_target), 'search_context': bool2str(search_context), }))