mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-23 16:26:17 +00:00
feat(backend): add signal for currency change (#13207)
* feat(backend): add signal for currency change * fix receiver
This commit is contained in:
1 parent
4ba5cfbb67
commit
35428bcb9f
4 files changed
+28
-12
No files matched your search
@@ -19,6 +19,7 @@ from django.core.management import call_command
|
|||||||
from django.db import DEFAULT_DB_ALIAS, connections, transaction
|
from django.db import DEFAULT_DB_ALIAS, connections, transaction
|
||||||
from django.db.migrations.executor import MigrationExecutor
|
from django.db.migrations.executor import MigrationExecutor
|
||||||
from django.db.utils import NotSupportedError, OperationalError, ProgrammingError
|
from django.db.utils import NotSupportedError, OperationalError, ProgrammingError
|
||||||
|
from django.dispatch import receiver
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
@@ -32,6 +33,7 @@ from maintenance_mode.core import (
|
|||||||
from opentelemetry import trace
|
from opentelemetry import trace
|
||||||
|
|
||||||
from common.settings import get_global_setting, set_global_setting
|
from common.settings import get_global_setting, set_global_setting
|
||||||
|
from common.signals import post_currency_change
|
||||||
from plugin import registry
|
from plugin import registry
|
||||||
|
|
||||||
from .version import isInvenTreeUpToDate
|
from .version import isInvenTreeUpToDate
|
||||||
@@ -1095,6 +1097,12 @@ def update_exchange_rates(force: bool = False):
|
|||||||
logger.exception('Error updating exchange rates: %s', type(e))
|
logger.exception('Error updating exchange rates: %s', type(e))
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(post_currency_change, dispatch_uid='receive_update_exchange_rates')
|
||||||
|
def receive_update_exchange_rates(*args, **kwargs):
|
||||||
|
"""Trigger exchange rate update."""
|
||||||
|
update_exchange_rates(True)
|
||||||
|
|
||||||
|
|
||||||
@tracer.start_as_current_span('run_backup')
|
@tracer.start_as_current_span('run_backup')
|
||||||
@scheduled_task(ScheduledTask.DAILY)
|
@scheduled_task(ScheduledTask.DAILY)
|
||||||
def run_backup():
|
def run_backup():
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ from moneyed import CURRENCIES
|
|||||||
|
|
||||||
import InvenTree.helpers
|
import InvenTree.helpers
|
||||||
import InvenTree.ready
|
import InvenTree.ready
|
||||||
|
from common.signals import post_currency_change
|
||||||
|
|
||||||
logger = structlog.get_logger('inventree')
|
logger = structlog.get_logger('inventree')
|
||||||
|
|
||||||
@@ -99,24 +100,14 @@ def after_change_currency(setting) -> None:
|
|||||||
- Update exchange rates
|
- Update exchange rates
|
||||||
- Recalculate prices for all parts
|
- Recalculate prices for all parts
|
||||||
"""
|
"""
|
||||||
import InvenTree.ready
|
|
||||||
import InvenTree.tasks
|
|
||||||
|
|
||||||
if InvenTree.ready.isImportingData():
|
if InvenTree.ready.isImportingData():
|
||||||
return
|
return
|
||||||
|
|
||||||
if not InvenTree.ready.canAppAccessDatabase():
|
if not InvenTree.ready.canAppAccessDatabase():
|
||||||
return
|
return
|
||||||
|
|
||||||
from part import tasks as part_tasks
|
# trigger signal
|
||||||
|
post_currency_change.send(setting)
|
||||||
# Immediately update exchange rates
|
|
||||||
InvenTree.tasks.update_exchange_rates(force=True)
|
|
||||||
|
|
||||||
# Offload update of part prices to a background task
|
|
||||||
InvenTree.tasks.offload_task(
|
|
||||||
part_tasks.check_missing_pricing, force_async=True, group='pricing'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def validate_currency_codes(value):
|
def validate_currency_codes(value):
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
"""Signals for InvenTree actions.
|
||||||
|
|
||||||
|
These should be used slectevily, mostly to avoid circular imports and tight coupeling with modular/extensible parts of the codebase.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from django.dispatch import Signal
|
||||||
|
|
||||||
|
post_currency_change = Signal()
|
||||||
|
"""Send after the default currency was changed."""
|
||||||
@@ -5,6 +5,7 @@ from typing import Optional
|
|||||||
|
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db.models import Model
|
from django.db.models import Model
|
||||||
|
from django.dispatch import receiver
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
import structlog
|
import structlog
|
||||||
@@ -15,6 +16,7 @@ import common.notifications
|
|||||||
import InvenTree.helpers
|
import InvenTree.helpers
|
||||||
import InvenTree.helpers_model
|
import InvenTree.helpers_model
|
||||||
from common.settings import get_global_setting
|
from common.settings import get_global_setting
|
||||||
|
from common.signals import post_currency_change
|
||||||
from InvenTree.tasks import (
|
from InvenTree.tasks import (
|
||||||
ScheduledTask,
|
ScheduledTask,
|
||||||
batch_offload_tasks,
|
batch_offload_tasks,
|
||||||
@@ -314,6 +316,12 @@ def check_missing_pricing(limit=250):
|
|||||||
pricing.schedule_for_update()
|
pricing.schedule_for_update()
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(post_currency_change, dispatch_uid='dispatch_check_missing_pricing')
|
||||||
|
def dispatch_check_missing_pricing(*args, **kwargs):
|
||||||
|
"""Trigger pricing update."""
|
||||||
|
offload_task(check_missing_pricing, force_async=True, group='pricing')
|
||||||
|
|
||||||
|
|
||||||
@tracer.start_as_current_span('scheduled_stocktake_reports')
|
@tracer.start_as_current_span('scheduled_stocktake_reports')
|
||||||
@scheduled_task(ScheduledTask.DAILY)
|
@scheduled_task(ScheduledTask.DAILY)
|
||||||
def scheduled_stocktake_reports():
|
def scheduled_stocktake_reports():
|
||||||
|
|||||||
Reference in new issue
Block a user