def test_get_terms(self): self.add_term("hello", "ahoj") self.add_term("thank", "děkujeme") unit = self.get_unit("Thank you for using Weblate.") self.assertEqual( set(get_glossary_terms(unit).values_list("source", flat=True)), {"thank"}) self.add_term("thank", "díky", "other") unit.glossary_terms = None self.assertEqual( set(get_glossary_terms(unit).values_list("source", flat=True)), {"thank"}) self.add_term("thank you", "děkujeme vám") unit.glossary_terms = None self.assertEqual( set(get_glossary_terms(unit).values_list("source", flat=True)), {"thank", "thank you"}, ) self.add_term("thank you for using Weblate", "děkujeme vám za použití Weblate") unit.glossary_terms = None self.assertEqual( set(get_glossary_terms(unit).values_list("source", flat=True)), {"thank", "thank you", "thank you for using Weblate"}, ) self.add_term("web", "web") unit.glossary_terms = None self.assertEqual( set(get_glossary_terms(unit).values_list("source", flat=True)), {"thank", "thank you", "thank you for using Weblate"}, )
def test_phrases(self): self.add_term("Destructive Breach", "x") self.add_term("Flame Breach", "x") self.add_term("Frost Breach", "x") self.add_term("Icereach", "x") self.add_term("Reach", "x") self.add_term("Reachable", "x") self.add_term("Skyreach", "x") unit = self.get_unit() unit.source = "During invasion from the Reach. Town burn, prior records lost.\n" self.assertEqual( list(get_glossary_terms(unit).values_list("source", flat=True)), ["Reach"]) self.add_term("Town", "x") unit.glossary_terms = None self.assertEqual( set(get_glossary_terms(unit).values_list("source", flat=True)), {"Reach", "Town"}, ) self.add_term("The Reach", "x") unit.glossary_terms = None self.assertEqual( set(get_glossary_terms(unit).values_list("source", flat=True)), {"Reach", "The Reach", "Town"}, )
def get_zen_unitdata(obj, project, unit_set, request): """Load unit data for zen mode.""" # Search results search_result = search(obj, project, unit_set, request) # Handle redirects if isinstance(search_result, HttpResponse): return search_result, None offset = search_result["offset"] - 1 search_result["last_section"] = offset + 20 >= len(search_result["ids"]) units = unit_set.prefetch_full().get_ordered( search_result["ids"][offset:offset + 20]) unitdata = [{ "unit": unit, "secondary": (unit.get_secondary_units(request.user) if request.user.is_authenticated and request.user.profile.secondary_in_zen else None), "form": ZenTranslationForm(request.user, unit, tabindex=100 + (unit.position * 10)), "offset": offset + pos + 1, "glossary": get_glossary_terms(unit), } for pos, unit in enumerate(units)] return search_result, unitdata
def test_get_long(self): """Test parsing long source string.""" unit = self.get_unit() unit.source = LONG unit.save() self.assertEqual(get_glossary_terms(unit).count(), 0) return unit
def check_single(self, source, target, unit): from weblate.glossary.models import get_glossary_terms forbidden = set() mismatched = set() matched = set() for term in get_glossary_terms(unit): term_source = term.source flags = term.all_flags expected = term_source if "read-only" in flags else term.target if "forbidden" in flags: if re.search(rf"\b{re.escape(expected)}\b", target, re.IGNORECASE): forbidden.add(term_source) else: if term_source in matched: continue if re.search(rf"\b{re.escape(expected)}\b", target, re.IGNORECASE): mismatched.discard(term_source) matched.add(term_source) else: mismatched.add(term_source) return forbidden | mismatched
def test_get_single(self): unit = self.get_unit("Thank you for using Weblate.") unit.source = "thank" self.add_term("thank", "díky") self.assertEqual( set(get_glossary_terms(unit).values_list("source", flat=True)), {"thank"}, )
def test_get_dash(self): unit = self.get_unit("Thank you for using Weblate.") unit.source = "Nordrhein-Westfalen" self.add_term("Nordrhein-Westfalen", "Northrhine Westfalia") self.assertEqual( set(get_glossary_terms(unit).values_list("source", flat=True)), {"Nordrhein-Westfalen"}, )
def test_substrings(self): self.add_term("reach", "dojet") self.add_term("breach", "prolomit") unit = self.get_unit() unit.source = "Reach summit" self.assertEqual( list(get_glossary_terms(unit).values_list("source", flat=True)), ["reach"])
def test_stoplist(self): unit = self.test_get_long() self.add_term("the blue", "modrý") self.add_term("the red", "červený") unit.glossary_terms = None self.assertEqual( set(get_glossary_terms(unit).values_list("source", flat=True)), {"the red"})
def test_get_long(self): """Test parsing long source string.""" unit = self.get_unit() unit.source = LONG unit.save() self.assertEqual( set(get_glossary_terms(unit).values_list("source", flat=True)), set()) return unit
def test_get_dash(self): unit = self.get_unit("Thank you for using Weblate.") unit.source = "Nordrhein-Westfalen" self.glossary.unit_set.create( source="Nordrhein-Westfalen", target="Northrhine Westfalia", id_hash=1, position=1, ) self.assertEqual(get_glossary_terms(unit).count(), 1)
def add_glossary_term(request, unit_id): unit = get_object_or_404(Unit, pk=int(unit_id)) component = unit.translation.component request.user.check_access_component(component) code = 403 results = "" details = "" terms = [] if request.user.has_perm("glossary.add", component.project): form = TermForm(unit, request.POST) if form.is_valid(): translation = form.cleaned_data["translation"] added = translation.add_unit(request, **form.as_kwargs()) terms = form.cleaned_data["terms"] terms.append(added.pk) code = 200 results = render_to_string( "snippets/glossary.html", { "glossary": ( # distinct is needed as get_glossary_terms is distict # and mixed queries can not be combined get_glossary_terms(unit) | translation.unit_set.filter(pk__in=terms).distinct()), "unit": unit, "user": request.user, }, ) else: messages = [] for error in form.non_field_errors(): messages.append(error) for field in form: for error in field.errors: messages.append( _("Error in parameter %(field)s: %(error)s") % { "field": field.name, "error": error }, ) details = "\n".join(messages) return JsonResponse( data={ "responseCode": code, "responseDetails": details, "results": results, "terms": ",".join(str(x) for x in terms), })
def test_get_terms(self): self.glossary.unit_set.create( source="hello", target="ahoj", id_hash=1, position=1, ) self.glossary.unit_set.create( source="thank", target="děkujeme", id_hash=2, position=2, ) unit = self.get_unit("Thank you for using Weblate.") self.assertEqual(get_glossary_terms(unit).count(), 1) self.glossary.unit_set.create( source="thank", target="díky", id_hash=3, position=3, ) self.assertEqual(get_glossary_terms(unit).count(), 2) self.glossary.unit_set.create( source="thank you", target="děkujeme vám", id_hash=4, position=4, ) self.assertEqual(get_glossary_terms(unit).count(), 3) self.glossary.unit_set.create( source="thank you for using Weblate", target="děkujeme vám za použití Weblate", id_hash=5, position=5, ) self.assertEqual(get_glossary_terms(unit).count(), 4)
def add_glossary_term(request, unit_id): unit = get_object_or_404(Unit, pk=int(unit_id)) component = unit.translation.component request.user.check_access_component(component) code = 403 results = "" terms = [] if request.user.has_perm("glossary.add", component.project): form = TermForm(unit, request.POST) if form.is_valid(): translation = form.cleaned_data["translation"] context = "" suffix = 0 source = form.cleaned_data["source"] while translation.unit_set.filter(context=context, source=source).exists(): suffix += 1 context = str(suffix) translation.add_units( request, [(context, source, form.cleaned_data["target"])]) terms = form.cleaned_data["terms"] terms.append( translation.unit_set.get(context=context, source=source).pk) code = 200 results = render_to_string( "snippets/glossary.html", { "glossary": ( # distinct is needed as get_glossary_terms is distict # and mixed queries can not be combined get_glossary_terms(unit) | translation.unit_set.filter(pk__in=terms).distinct()), "unit": unit, "user": request.user, }, ) return JsonResponse( data={ "responseCode": code, "results": results, "terms": ",".join(str(x) for x in terms), })
def test_stoplist(self): unit = self.test_get_long() self.glossary.unit_set.create( source="the blue", target="modrý", id_hash=1, position=1, ) self.glossary.unit_set.create( source="the red", target="červený", id_hash=2, position=2, ) self.assertEqual(get_glossary_terms(unit).count(), 1)
def translate(request, project, component, lang): """Generic entry point for translating, suggesting and searching.""" obj, project, unit_set = parse_params(request, project, component, lang) # Search results search_result = search(obj, 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 checksum_form = ChecksumForm(unit_set, request.GET or request.POST) if checksum_form.is_valid(): unit = checksum_form.cleaned_data["unit"] try: offset = search_result["ids"].index(unit.id) + 1 except ValueError: messages.warning(request, _("No string matched your search!")) 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) # Check locks locked = unit.translation.component.locked # 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 (not locked and "accept" not in request.POST and "accept_edit" not in request.POST and "delete" not in request.POST and "spam" not in request.POST and "upvote" not in request.POST and "downvote" not in request.POST): # Handle translation response = handle_translate(request, unit, this_unit_url, next_unit_url) elif not locked or "delete" in request.POST or "spam" in request.POST: # Handle accepting/deleting suggestions response = handle_suggestions(request, unit, this_unit_url, next_unit_url) # Handle translation merging elif "merge" in request.POST and not locked: response = handle_merge(unit, request, next_unit_url) # Handle reverting elif "revert" in request.GET and not locked: 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 request.user.is_authenticated: secondary = unit.get_secondary_units(request.user) else: secondary = None # Spam protection antispam = AntispamForm() # Prepare form form = TranslationForm(request.user, unit) sort = get_sort_name(request, obj) 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(request.user.profile.nearby_strings), "nearby_keys": unit.nearby_keys(request.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, "antispam": antispam, "comment_form": CommentForm( project, initial={ "scope": "global" if unit.is_source else "translation" }, ), "context_form": ContextForm(instance=unit.source_unit, user=request.user), "search_form": search_result["form"].reset_offset(), "secondary": secondary, "locked": locked, "glossary": get_glossary_terms(unit), "addterm_form": TermForm(unit), "last_changes": unit.change_set.prefetch().order()[:10], "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())), }, )