mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-28 11:36:44 +00:00
Offload initial email task (#8413)
- Prevent blocking on email task - Allow worker to try multiple times - Don't pass 500 error back to form validation
This commit is contained in:
parent
4308f0c2f9
commit
d1c2213836
@ -510,6 +510,7 @@ class UserCreateSerializer(ExtendedUserSerializer):
|
||||
def create(self, validated_data):
|
||||
"""Send an e email to the user after creation."""
|
||||
from InvenTree.helpers_model import get_base_url
|
||||
from InvenTree.tasks import email_user, offload_task
|
||||
|
||||
base_url = get_base_url()
|
||||
|
||||
@ -527,8 +528,12 @@ class UserCreateSerializer(ExtendedUserSerializer):
|
||||
if base_url:
|
||||
message += f'\n\nURL: {base_url}'
|
||||
|
||||
subject = _('Welcome to InvenTree')
|
||||
|
||||
# Send the user an onboarding email (from current site)
|
||||
instance.email_user(subject=_('Welcome to InvenTree'), message=message)
|
||||
offload_task(
|
||||
email_user, instance.pk, str(subject), str(message), force_async=True
|
||||
)
|
||||
|
||||
return instance
|
||||
|
||||
|
@ -12,6 +12,7 @@ from datetime import datetime, timedelta
|
||||
from typing import Callable, Optional
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.exceptions import AppRegistryNotReady
|
||||
from django.core.management import call_command
|
||||
from django.db import DEFAULT_DB_ALIAS, connections
|
||||
@ -693,3 +694,14 @@ def check_for_migrations(force: bool = False, reload_registry: bool = True):
|
||||
# We should be current now - triggering full reload to make sure all models
|
||||
# are loaded fully in their new state.
|
||||
registry.reload_plugins(full_reload=True, force_reload=True, collect=True)
|
||||
|
||||
|
||||
def email_user(user_id: int, subject: str, message: str) -> None:
|
||||
"""Send a message to a user."""
|
||||
try:
|
||||
user = get_user_model().objects.get(pk=user_id)
|
||||
except Exception:
|
||||
logger.warning('User <%s> not found - cannot send welcome message', user_id)
|
||||
return
|
||||
|
||||
user.email_user(subject=subject, message=message)
|
||||
|
Loading…
x
Reference in New Issue
Block a user