Skip to content

Commit

Permalink
Fix glossary term duplication bug
Browse files Browse the repository at this point in the history
Because of  add_glossary_term view handler that duplicates the term in the returned results.
Plus bad returned html table template that breaks rendering when injected into the right side panel.
closes: #12624
closes: #12693
  • Loading branch information
meel-hd authored and nijel committed Nov 5, 2024
1 parent 3d55be2 commit 42e2262
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
18 changes: 13 additions & 5 deletions weblate/glossary/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,24 @@ def add_glossary_term(request: AuthenticatedHttpRequest, unit_id):
translation = form.cleaned_data["translation"]
added = translation.add_unit(request, **form.as_kwargs())
terms = form.cleaned_data["terms"]
terms.append(added.pk)
# Only append the new term if it's not already in the list
if added.pk not in terms:
terms.append(added.pk)
code = 200

# Fetch matching terms
all_terms = get_glossary_terms(unit)
# Add newly added one
all_terms.append(added)

# Add previously present ones
missing_terms = set(terms) - {term.pk for term in all_terms}
# Create a set of existing term IDs
existing_term_ids = {term.pk for term in all_terms}

# Add newly added term if not already present
if added.pk not in existing_term_ids:
all_terms.append(added)
existing_term_ids.add(added.pk)

# Add any missing terms that aren't already present
missing_terms = set(terms) - existing_term_ids
if missing_terms:
all_terms.extend(translation.unit_set.filter(pk__in=missing_terms))

Expand Down
3 changes: 1 addition & 2 deletions weblate/templates/snippets/glossary.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
{% load icons %}

{% for item in glossary %}
<tbody class="glossary-embed {% if "forbidden" in item.all_flags %}unclickable-row danger{% elif "read-only" in item.all_flags %}clickable-row warning{% else %}clickable-row{% endif %}">
<tr
class="glossary-embed {% if "forbidden" in item.all_flags %}unclickable-row danger{% elif "read-only" in item.all_flags %}clickable-row warning{% else %}clickable-row{% endif %}"
title="
{% if "forbidden" in item.all_flags %}
{% trans "This translation is forbidden." %}
Expand Down Expand Up @@ -53,7 +53,6 @@
</td>
</tr>
{% endif %}
</tbody>
{% empty %}
<tr>
<td colspan="4"><em>{% trans "No related strings found in the glossary." %}</em></td>
Expand Down

0 comments on commit 42e2262

Please sign in to comment.