Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extend DuplicateFilemask to operate on files #13302

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions weblate/templates/trans/alert/duplicatefilemask.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{% load i18n %}

<p>
{% trans "Some linked components have the same file mask." %}
{% trans "Some linked components have the same file mask or share some of the files." %}
{% trans "Please fix this by removing one of them." %}
</p>

<p>{% trans "The following file masks were found multiple times:" %}</p>
<p>{% trans "The following files were found multiple times:" %}</p>

<ul>
{% for mask in duplicates %}
{% for mask, translations in analysis.duplicates_resolved %}
<li>
<pre>{{ mask }}</pre>
<pre>{{ mask }}</pre>:
{% for translation in translations %}<a href="{{ translation.get_absolute_url }}">{{ translation }}</a>{% endfor %}
</li>
{% endfor %}
</ul>
2 changes: 1 addition & 1 deletion weblate/trans/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def post_delete_linked(sender, instance, **kwargs) -> None:
# When removing project, the linked component might be already deleted now
try:
if instance.linked_component:
instance.linked_component.update_link_alerts(noupdate=True)
instance.linked_component.update_alerts()
except Component.DoesNotExist:
pass

Expand Down
48 changes: 46 additions & 2 deletions weblate/trans/models/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sentry_sdk
from django.conf import settings
from django.db import models
from django.db.models import Count
from django.db.models import Count, Q
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.functional import cached_property
Expand All @@ -29,7 +29,9 @@
from django_stubs_ext import StrOrPromise

from weblate.auth.models import User
from weblate.trans.models import Component, Translation
from weblate.trans.models.component import Component, ComponentQuerySet
from weblate.trans.models.translation import Translation, TranslationQuerySet


ALERTS: dict[str, type[BaseAlert]] = {}
ALERTS_IMPORT: set[str] = set()
Expand Down Expand Up @@ -228,6 +230,48 @@
super().__init__(instance)
self.duplicates = duplicates

@staticmethod
def get_translations(component: Component) -> TranslationQuerySet:
from weblate.trans.models import Translation

return Translation.objects.filter(
Q(component=component) | Q(component__linked_component=component)
)

@classmethod
def check_component(cls, component: Component) -> bool | dict | None:
if component.is_repo_link:
return False

translations = set(
cls.get_translations(component)
.values_list("filename")
.annotate(count=Count("id"))
.filter(count__gt=1)
.values_list("filename", flat=True)
)
translations.discard("")
if translations:
return {"duplicates": sorted(translations)}
return False

def resolve_filename(
self, filename: str
) -> ComponentQuerySet | TranslationQuerySet:
if "*" in filename:
# Legacy path for old alerts
# TODO: Remove in Weblate 6.0
return self.instance.component.component_set.filter(filemask=filename)

Check warning on line 264 in weblate/trans/models/alert.py

View check run for this annotation

Codecov / codecov/patch

weblate/trans/models/alert.py#L264

Added line #L264 was not covered by tests
return self.get_translations(self.instance.component).filter(filename=filename)

def get_analysis(self):
return {
"duplicates_resolved": [
(filename, self.resolve_filename(filename))
for filename in self.duplicates
]
}


@register
class MergeFailure(ErrorAlert):
Expand Down
20 changes: 4 additions & 16 deletions weblate/trans/models/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
import re
import time
from collections import Counter, defaultdict
from collections import defaultdict
from copy import copy
from glob import glob
from itertools import chain
Expand Down Expand Up @@ -1107,7 +1107,7 @@
# Calculate progress for translations
if progress is None:
self.translations_progress += 1
progress = 100 * self.translations_progress // self.translations_count

Check failure on line 1110 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

Unsupported operand types for // ("int" and "None")
# Store task state
current_task.update_state(
state="PROGRESS", meta={"progress": progress, "component": self.pk}
Expand Down Expand Up @@ -1757,7 +1757,7 @@
from weblate.trans.tasks import perform_push

self.log_info("scheduling push")
perform_push.delay_on_commit(

Check failure on line 1760 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

"Task[[Any, VarArg(Any), KwArg(Any)], None]" has no attribute "delay_on_commit"
self.pk, None, force_commit=False, do_update=do_update
)

Expand Down Expand Up @@ -1962,7 +1962,7 @@
return "weblate://{}".format("/".join(self.get_url_path()))

@cached_property
def linked_childs(self):
def linked_childs(self) -> ComponentQuerySet:
"""Return list of components which links repository to us."""
children = self.component_set.prefetch()
for child in children:
Expand Down Expand Up @@ -2345,7 +2345,7 @@
.order_by("-id")[0]
.auto_status
):
self.do_lock(user=None, lock=False, auto=True)

Check failure on line 2348 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

Argument "user" to "do_lock" of "Component" has incompatible type "None"; expected "User"

if ALERTS[alert].link_wide:
for component in self.linked_childs:
Expand All @@ -2363,7 +2363,7 @@

# Automatically lock on error
if created and self.auto_lock_error and alert in LOCKING_ALERTS:
self.do_lock(user=None, lock=True, auto=True)

Check failure on line 2366 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

Argument "user" to "do_lock" of "Component" has incompatible type "None"; expected "User"

# Update details with exception of component removal
if not created and not noupdate:
Expand Down Expand Up @@ -2568,7 +2568,7 @@
)
self.handle_parse_error(error.__cause__, filename=self.template)
self.update_import_alerts()
raise error.__cause__ from error # pylint: disable=E0710

Check failure on line 2571 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

Exception must be derived from BaseException
was_change |= bool(translation.reason)
translations[translation.id] = translation
languages[lang.code] = translation
Expand Down Expand Up @@ -2632,7 +2632,7 @@
if self.needs_cleanup and not self.template:
from weblate.trans.tasks import cleanup_component

cleanup_component.delay_on_commit(self.id)

Check failure on line 2635 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

"Task[[Any], None]" has no attribute "delay_on_commit"

if was_change:
if self.needs_variants_update:
Expand Down Expand Up @@ -2664,7 +2664,7 @@
if settings.CELERY_TASK_ALWAYS_EAGER:
batch_update_checks(self.id, batched_checks, component=self)
else:
batch_update_checks.delay_on_commit(self.id, batched_checks)

Check failure on line 2667 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

"Task[[Any, Any, Component | None], None]" has no attribute "delay_on_commit"
self.batch_checks = False
self.batched_checks = set()

Expand Down Expand Up @@ -2815,7 +2815,7 @@
raise ValidationError(
{"filemask": gettext("The file mask did not match any files.")}
)
langs = {}

Check failure on line 2818 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

Need type annotation for "langs" (hint: "langs: dict[<type>, <type>] = ...")
existing_langs = set()

for match in matches:
Expand Down Expand Up @@ -2882,7 +2882,7 @@
if (not self.new_base and self.new_lang != "add") or not self.file_format:
return
# File is valid or no file is needed
errors = []

Check failure on line 2885 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

Need type annotation for "errors" (hint: "errors: list[<type>] = ...")
if self.is_valid_base_for_new(errors):
return
# File is needed, but not present
Expand Down Expand Up @@ -3218,6 +3218,8 @@

# Update alerts after stats update
self.update_alerts()
if self.linked_component:
self.linked_component.update_alerts()

# Make sure we create glossary
if create and settings.CREATE_GLOSSARIES:
Expand Down Expand Up @@ -3256,7 +3258,7 @@
if self.variant_regex:
variant_re = re.compile(self.variant_regex)
units = process_units.filter(context__regex=self.variant_regex)
variant_updates = {}

Check failure on line 3261 in weblate/trans/models/component.py

View workflow job for this annotation

GitHub Actions / mypy

Need type annotation for "variant_updates" (hint: "variant_updates: dict[<type>, <type>] = ...")
for unit in units.iterator():
if variant_re.findall(unit.context):
key = variant_re.sub("", unit.context)
Expand Down Expand Up @@ -3293,27 +3295,13 @@
variant_regex="", unit_count=0
).delete()

def update_link_alerts(self, noupdate: bool = False) -> None:
base = self.linked_component if self.is_repo_link else self
masks = [base.filemask]
masks.extend(base.linked_childs.values_list("filemask", flat=True))
duplicates = [item for item, count in Counter(masks).items() if count > 1]
if duplicates:
self.add_alert(
"DuplicateFilemask", duplicates=duplicates, noupdate=noupdate
)
else:
self.delete_alert("DuplicateFilemask")

def _update_alerts(self):
self._alerts_scheduled = False
# Flush alerts case, mostly needed for tests
self.__dict__.pop("all_alerts", None)

update_alerts(self)

self.update_link_alerts()

# Update libre checklist upon save on all components in a project
if (
settings.OFFER_HOSTING
Expand Down
24 changes: 20 additions & 4 deletions weblate/trans/tests/test_alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ def test_monolingual(self) -> None:
component.alert_set.filter(name="MonolingualTranslation").exists()
)

def test_duplicate_mask(self):
component = self.component
self.assertFalse(component.alert_set.filter(name="DuplicateFilemask").exists())
response = self.client.get(component.get_absolute_url())
self.assertNotContains(
response, "The following files were found multiple times"
)

other = self.create_link_existing()

self.assertTrue(component.alert_set.filter(name="DuplicateFilemask").exists())
response = self.client.get(component.get_absolute_url())
self.assertContains(response, "The following files were found multiple times")

other.delete()

self.assertFalse(component.alert_set.filter(name="DuplicateFilemask").exists())


class LanguageAlertTest(ViewTestCase):
def create_component(self):
Expand All @@ -120,10 +138,8 @@ def create_component(self):
def test_ambiguous_language(self) -> None:
component = self.component
self.assertFalse(component.alert_set.filter(name="AmbiguousLanguage").exists())
self.component.add_new_language(
Language.objects.get(code="ku"), self.get_request()
)
self.component.update_alerts()
component.add_new_language(Language.objects.get(code="ku"), self.get_request())
component.update_alerts()
self.assertTrue(component.alert_set.filter(name="AmbiguousLanguage").exists())


Expand Down
Loading