def new_unit(request, project, component, lang): translation = get_translation(request, project, component, lang) if not request.user.has_perm("unit.add", translation): raise PermissionDenied() form = get_new_unit_form(translation, request.user, request.POST) if not form.is_valid(): show_form_errors(request, form) else: if form.unit_exists(translation): messages.error(request, _("This string seems to already exist.")) else: # This is slow way of detecting unit, add_units should directly # create the database units, return them and they should be saved to # file as regular pending ones. existing = list(translation.unit_set.values_list("pk", flat=True)) translation.add_units(request, [form.as_tuple()]) messages.success(request, _("New string has been added.")) new_units = translation.unit_set.exclude(pk__in=existing) if form.cleaned_data["variant"]: for new_unit in new_units: flags = Flags(new_unit.extra_flags) flags.set_value("variant", form.cleaned_data["variant"]) new_unit.extra_flags = flags.format() new_unit.save( update_fields=["extra_flags"], same_content=True, run_checks=False, ) return redirect(new_unit) return redirect(translation)
def new_unit(request, project, component, lang): translation = get_translation(request, project, component, lang) if not request.user.has_perm("unit.add", translation): raise PermissionDenied() form = get_new_unit_form(translation, request.user, request.POST) if not form.is_valid(): show_form_errors(request, form) else: created_unit = translation.add_unit(request, **form.as_kwargs()) messages.success(request, _("New string has been added.")) return redirect(created_unit) return redirect(translation)
def new_unit(request, project, component, lang): translation = get_translation(request, project, component, lang) if not request.user.has_perm("unit.add", translation): raise PermissionDenied() form = get_new_unit_form(translation, request.user, request.POST) if not form.is_valid(): show_form_errors(request, form) else: if form.unit_exists(translation): messages.error(request, _("This string seems to already exist.")) else: translation.add_units(request, [form.as_tuple()]) messages.success(request, _("New string has been added.")) return redirect(translation)
def new_unit(request, project, component, lang): translation = get_translation(request, project, component, lang) if not request.user.has_perm("unit.add", translation): raise PermissionDenied() form = get_new_unit_form(translation, request.user, request.POST) if not form.is_valid(): show_form_errors(request, form) else: new_unit = translation.add_unit(request, *form.as_tuple()) messages.success(request, _("New string has been added.")) if form.cleaned_data["variant"]: flags = Flags(new_unit.extra_flags) flags.set_value("variant", form.cleaned_data["variant"]) new_unit.extra_flags = flags.format() new_unit.save( update_fields=["extra_flags"], same_content=True, run_checks=False, ) return redirect(new_unit) return redirect(translation)
def show_translation(request, project, component, lang): obj = get_translation(request, project, component, lang) component = obj.component project = component.project obj.stats.ensure_all() last_changes = obj.change_set.prefetch().order()[:10] user = request.user # Get form form = get_upload_form(user, obj) search_form = SearchForm(request.user, language=obj.language) # Translations to same language from other components in this project other_translations = prefetch_stats( list(Translation.objects.prefetch().filter( component__project=project, language=obj.language).exclude(pk=obj.pk))) # Include ghost translations for other components, this # adds quick way to create translations in other components existing = { translation.component.slug for translation in other_translations } existing.add(component.slug) for test_component in project.child_components.filter_access(user).exclude( slug__in=existing): if test_component.can_add_new_language(user, fast=True): other_translations.append( GhostTranslation(test_component, obj.language)) # Limit the number of other components displayed to 10, preferring untranslated ones other_translations = sorted(other_translations, key=lambda t: t.stats.translated_percent)[:10] return render( request, "translation.html", { "allow_index": True, "object": obj, "project": project, "form": form, "download_form": DownloadForm(auto_id="id_dl_%s"), "autoform": optional_form( AutoForm, user, "translation.auto", obj, obj=component), "search_form": search_form, "replace_form": optional_form(ReplaceForm, user, "unit.edit", obj), "bulk_state_form": optional_form( BulkEditForm, user, "translation.auto", obj, user=user, obj=obj, project=project, auto_id="id_bulk_%s", ), "new_unit_form": get_new_unit_form(obj, user), "announcement_form": optional_form(AnnouncementForm, user, "component.edit", obj), "delete_form": optional_form(TranslationDeleteForm, user, "translation.delete", obj, obj=obj), "last_changes": last_changes, "last_changes_url": urlencode(obj.get_reverse_url_kwargs()), "other_translations": other_translations, "exporters": EXPORTERS.list_exporters(obj), }, )
def translate(request, project, component, lang): # noqa: C901 """Generic entry point for translating, suggesting and searching.""" obj, project, unit_set = parse_params(request, project, component, lang) user = request.user # Search results search_result = search(obj, project, unit_set, request) # Handle redirects if isinstance(search_result, HttpResponse): return search_result # Get numer of results num_results = len(search_result["ids"]) # Search offset offset = search_result["offset"] # Checksum unit access payload = request.GET or request.POST if payload.get("checksum"): checksum_form = ChecksumForm(unit_set, payload) if checksum_form.is_valid(): unit = checksum_form.cleaned_data["unit"] try: offset = search_result["ids"].index(unit.id) + 1 except ValueError: offset = None else: offset = None if offset is None: messages.warning(request, _("No strings found!")) return redirect(obj) else: # Check boundaries if not 0 < offset <= num_results: messages.info(request, _("The translation has come to an end.")) # Delete search del request.session[search_result["key"]] return redirect(obj) # Grab actual unit try: unit = unit_set.get(pk=search_result["ids"][offset - 1]) except Unit.DoesNotExist: # Can happen when using SID for other translation messages.error(request, _("Invalid search string!")) return redirect(obj) # Some URLs we will most likely use base_unit_url = "{}?{}&offset=".format(obj.get_translate_url(), search_result["url"]) this_unit_url = base_unit_url + str(offset) next_unit_url = base_unit_url + str(offset + 1) response = None # Any form submitted? if "skip" in request.POST: return redirect(next_unit_url) if request.method == "POST" and "merge" not in request.POST: if ("accept" in request.POST or "accept_edit" in request.POST or "delete" in request.POST or "spam" in request.POST or "upvote" in request.POST or "downvote" in request.POST): # Handle accepting/deleting suggestions response = handle_suggestions(request, unit, this_unit_url, next_unit_url) else: # Handle translation response = handle_translate(request, unit, this_unit_url, next_unit_url) # Handle translation merging elif "merge" in request.POST: response = handle_merge(unit, request, next_unit_url) # Handle reverting elif "revert" in request.GET: response = handle_revert(unit, request, this_unit_url) # Pass possible redirect further if response is not None: return response # Show secondary languages for signed in users if user.is_authenticated: secondary = unit.get_secondary_units(user) else: secondary = None # Prepare form form = TranslationForm(user, unit) sort = get_sort_name(request, obj) screenshot_form = None if user.has_perm("screenshot.add", unit.translation): screenshot_form = ScreenshotForm( unit.translation.component, initial={"translation": unit.translation}) return render( request, "translate.html", { "this_unit_url": this_unit_url, "first_unit_url": base_unit_url + "1", "last_unit_url": base_unit_url + str(num_results), "next_unit_url": next_unit_url, "prev_unit_url": base_unit_url + str(offset - 1), "object": obj, "project": project, "unit": unit, "nearby": unit.nearby(user.profile.nearby_strings), "nearby_keys": unit.nearby_keys(user.profile.nearby_strings), "others": get_other_units(unit), "search_url": search_result["url"], "search_items": search_result["items"], "search_query": search_result["query"], "offset": offset, "sort_name": sort["name"], "sort_query": sort["query"], "filter_name": search_result["name"], "filter_count": num_results, "filter_pos": offset, "form": form, "comment_form": CommentForm( project, initial={ "scope": "global" if unit.is_source else "translation" }, ), "context_form": ContextForm(instance=unit.source_unit, user=user), "search_form": search_result["form"].reset_offset(), "secondary": secondary, "locked": unit.translation.component.locked, "glossary": get_glossary_terms(unit), "addterm_form": TermForm(unit, user), "last_changes": unit.change_set.prefetch().order()[:10].preload("unit"), "screenshots": (unit.source_unit.screenshots.all() | unit.screenshots.all()).order, "last_changes_url": urlencode(unit.translation.get_reverse_url_kwargs()), "display_checks": list(get_display_checks(unit)), "machinery_services": json.dumps(list(MACHINE_TRANSLATION_SERVICES.keys())), "new_unit_form": get_new_unit_form( unit.translation, user, initial={"variant": unit.pk}), "screenshot_form": screenshot_form, }, )