Skip to content

Commit

Permalink
fix(add-ons): fix daily execution
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed May 3, 2024
1 parent c2216c1 commit 7ac414b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Not yet released.
**Bug fixes**

* Fixed crashes with librsvg older than 2.46.
* Daily execution of some :ref:`addons`.

**Compatibility**

Expand Down
2 changes: 1 addition & 1 deletion weblate/addons/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def execute_addon_event(
getattr(addon.addon, method)(*args)
else:
# Callback is used in tasks
method(addon)
method(addon, component)
except DjangoDatabaseError:
raise
except Exception as error:
Expand Down
13 changes: 7 additions & 6 deletions weblate/addons/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,18 @@ def language_consistency(


@app.task(trail=False)
def daily_addons() -> None:
def daily_callback(addon) -> None:
addon.addon.daily(addon.component)
def daily_addons(modulo: bool = True) -> None:
def daily_callback(addon, component) -> None:
addon.addon.daily(component)

today = timezone.now()
addons = Addon.objects.filter(event__event=AddonEvent.EVENT_DAILY)
if modulo:
addons = addons.annotate(hourmod=F("id") % 24).filter(hourmod=today.hour)
handle_addon_event(
AddonEvent.EVENT_DAILY,
daily_callback,
addon_queryset=Addon.objects.annotate(hourmod=F("id") % 24).filter(
hourmod=today.hour, event__event=AddonEvent.EVENT_DAILY
),
addon_queryset=addons,
auto_scope=True,
)

Expand Down
24 changes: 21 additions & 3 deletions weblate/addons/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,14 +1225,18 @@ def test_append_trailers(self) -> None:


class TestRemoval(ViewTestCase):
def install(self):
def install(self, sitewide: bool = False):
self.assertTrue(RemoveComments.can_install(self.component, None))
self.assertTrue(RemoveSuggestions.can_install(self.component, None))
return (
RemoveSuggestions.create(
component=self.component, configuration={"age": 7}
component=None if sitewide else self.component,
configuration={"age": 7},
),
RemoveComments.create(
component=None if sitewide else self.component,
configuration={"age": 7},
),
RemoveComments.create(component=self.component, configuration={"age": 7}),
)

def assert_count(self, comments=0, suggestions=0) -> None:
Expand Down Expand Up @@ -1284,7 +1288,21 @@ def test_votes(self) -> None:

def test_daily(self) -> None:
self.install()
self.add_content()
self.age_content()
daily_addons()
# Ensure the add-on is executed
daily_addons(modulo=False)
self.assert_count()

def test_daily_sitewide(self) -> None:
self.install(sitewide=True)
self.add_content()
self.age_content()
daily_addons()
# Ensure the add-on is executed
daily_addons(modulo=False)
self.assert_count()


class AutoTranslateAddonTest(ViewTestCase):
Expand Down

0 comments on commit 7ac414b

Please sign in to comment.