snapshot: preserve role-aware notification preferences and operational alerts

This commit is contained in:
Md Bayazid Bostame
2026-03-27 11:26:57 +01:00
parent fe3a8933fd
commit aa54f41731
25 changed files with 2958 additions and 633 deletions

View File

@@ -28,6 +28,7 @@ from .forms import (
SOFTWARE_EXTRA_CHOICES,
WORKSPACE_GROUP_CHOICES,
)
from .notifications import notify_user_by_email
# These templates are the product-level defaults for fresh deployments.
# Runtime branding and company config can override the company-facing identity
@@ -251,6 +252,32 @@ DEFAULT_NOTIFICATION_TEMPLATES = {
}
def _notify_request_result(*, recipient_email: str, title: str, body: str, level: str, event_key: str) -> None:
notify_user_by_email(
email=recipient_email,
title=title,
body=body,
level=level,
link_url='/requests/',
event_key=event_key,
)
def _notify_welcome_email_result(*, recipient_email: str, full_name: str, body: str, level: str, event_key: str) -> None:
notify_user_by_email(
email=recipient_email,
title=(
_('Welcome E-Mail gesendet: %(name)s') % {'name': full_name}
if event_key == 'welcome_email_success'
else _('Welcome E-Mail fehlgeschlagen: %(name)s') % {'name': full_name}
),
body=body,
level=level,
link_url='/admin-tools/welcome-emails/',
event_key=event_key,
)
def _start_task_log(task_name: str, *, target_type: str = '', target_id: int | None = None, target_label: str = '') -> AsyncTaskLog:
task_request = getattr(current_task, 'request', None)
return AsyncTaskLog.objects.create(
@@ -1331,11 +1358,25 @@ def process_onboarding_request(onboarding_request_id: int) -> None:
request_obj.processing_status = 'completed'
request_obj.last_error = ''
request_obj.save(update_fields=['processing_status', 'last_error'])
_notify_request_result(
recipient_email=request_obj.onboarded_by_email,
title=_('Onboarding abgeschlossen: %(name)s') % {'name': request_obj.full_name},
body=_('Die Onboarding-Anfrage wurde erfolgreich verarbeitet.'),
level='success',
event_key='onboarding_success',
)
_finish_task_log(task_log, status='succeeded')
except Exception as exc:
request_obj.processing_status = 'failed'
request_obj.last_error = str(exc)
request_obj.save(update_fields=['processing_status', 'last_error'])
_notify_request_result(
recipient_email=request_obj.onboarded_by_email,
title=_('Onboarding fehlgeschlagen: %(name)s') % {'name': request_obj.full_name},
body=str(exc),
level='error',
event_key='onboarding_failure',
)
_finish_task_log(task_log, status='failed', error_message=str(exc))
raise
@@ -1355,7 +1396,7 @@ def process_offboarding_request(offboarding_request_id: int) -> None:
try:
branding_copy = get_branding_email_copy()
company_contact = get_company_contact_copy()
it_email, general_info_email, _, hr_works_email, _ = _resolve_workflow_emails()
it_email, general_info_email, business_card_email_unused, hr_works_email, key_email_unused = _resolve_workflow_emails()
pdf_path = _generate_offboarding_pdf(request_obj)
request_obj.generated_pdf_path = str(pdf_path)
@@ -1418,11 +1459,25 @@ def process_offboarding_request(offboarding_request_id: int) -> None:
request_obj.processing_status = 'completed'
request_obj.last_error = ''
request_obj.save(update_fields=['processing_status', 'last_error'])
_notify_request_result(
recipient_email=request_obj.requested_by_email,
title=_('Offboarding abgeschlossen: %(name)s') % {'name': request_obj.full_name},
body=_('Die Offboarding-Anfrage wurde erfolgreich verarbeitet.'),
level='success',
event_key='offboarding_success',
)
_finish_task_log(task_log, status='succeeded')
except Exception as exc:
request_obj.processing_status = 'failed'
request_obj.last_error = str(exc)
request_obj.save(update_fields=['processing_status', 'last_error'])
_notify_request_result(
recipient_email=request_obj.requested_by_email,
title=_('Offboarding fehlgeschlagen: %(name)s') % {'name': request_obj.full_name},
body=str(exc),
level='error',
event_key='offboarding_failure',
)
_finish_task_log(task_log, status='failed', error_message=str(exc))
raise
@@ -1490,10 +1545,24 @@ def send_scheduled_welcome_email(scheduled_email_id: int, force_now: bool = Fals
scheduled.status = 'sent'
scheduled.sent_at = timezone.now()
scheduled.last_error = ''
_notify_welcome_email_result(
recipient_email=request_obj.onboarded_by_email,
full_name=request_obj.full_name,
body=_('Die geplante Welcome E-Mail wurde erfolgreich versendet.'),
level='success',
event_key='welcome_email_success',
)
_finish_task_log(task_log, status='succeeded')
except Exception as exc:
scheduled.status = 'failed'
scheduled.last_error = str(exc)
_notify_welcome_email_result(
recipient_email=request_obj.onboarded_by_email,
full_name=request_obj.full_name,
body=str(exc),
level='error',
event_key='welcome_email_failure',
)
_finish_task_log(task_log, status='failed', error_message=str(exc))
raise
finally: