Matthias Nagel bc4b8a1e7f Feat: Implementierung des Spieler- und Eltern-Verifizierungsprozesses
Fügt einen umfassenden Verifizierungsprozess für neu erstellte Spieler
und zugeordnete Eltern hinzu. Dies ersetzt das frühere Einladungscode-System.

Wesentliche Änderungen:
- **`CustomUser` Modell:** Erweitert um `is_verified` (Standard `False`) und
  `verification_code` (UUID) Felder. `is_active` ist nun standardmäßig `False`
  bis zur Verifizierung. Das `InvitationCode`-Modell wurde entfernt.
- **E-Mail-Utility (`accounts/utils.py`):** Eine neue Funktion `send_verification_email`
  sendet oder simuliert E-Mails (basierend auf `settings.MTP_EMAIL_SEND`).
  Simulierte E-Mails werden im `.mbox`-Format in `tmp_mails/` gespeichert.
- **`settings.py`:** `DEFAULT_FROM_EMAIL` wurde hinzugefügt.
- **`PlayerCreateView` (`accounts/views.py`):**
    - Generiert `verification_code` für neue Spieler und Eltern.
    - Setzt das Passwort für neue Benutzer auf unbrauchbar (`set_unusable_password`).
    - Löst den Versand von Verifizierungs-E-Mails aus.
- **`verify_account` View (`accounts/views.py`):**
    - Eine neue View, die über einen Link in der E-Mail aufgerufen wird.
    - Ermöglicht Spielern, ein Passwort festzulegen.
    - Ermöglicht Eltern, einen eindeutigen Benutzernamen und ein Passwort festzulegen.
    - Setzt `is_active` und `is_verified` auf `True` und invalidiert den
      Verifizierungscode nach erfolgreicher Einrichtung.
    - Loggt den Benutzer nach erfolgreicher Verifizierung direkt ein und zeigt
      eine Erfolgsmeldung an.
    - Behebt ein Problem bei der Bestimmung von Eltern-Benutzern.
- **Formulare (`accounts/forms.py`):** Neue `PlayerVerificationForm` und
  `ParentVerificationForm` für den Verifizierungsprozess.
- **E-Mail-Templates:** Neue Text- und HTML-Templates für Spieler- und
  Eltern-Verifizierungs-E-Mails (`accounts/templates/accounts/email/`).
- **Verifizierungs-Template:** Neues Template für die Verifizierungsseite
  (`accounts/templates/accounts/verify_account.html`).
- **URLs (`accounts/urls.py`):** Entfernung der alten `invitation_code` und
  `register` URLs, Hinzufügung der neuen `verify_account` URL.
- **Datenbankmigrationen:** Migrationen für die Änderungen am `CustomUser`-Modell
  erstellt und angewendet.
- **Temporäres Verzeichnis:** `tmp_mails/` Verzeichnis für E-Mail-Simulation erstellt.
2025-11-23 16:34:25 +01:00

75 lines
2.4 KiB
Python

import os
from django.conf import settings
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.urls import reverse
import uuid
def send_verification_email(user, request, is_parent=False):
"""
Sends a verification email to a new user (player or parent).
"""
# Ensure user has a verification code
if not user.verification_code:
user.verification_code = uuid.uuid4()
user.save()
# Build the verification URL
verification_path = reverse('verify_account', kwargs={'verification_code': str(user.verification_code)})
verification_url = request.build_absolute_uri(verification_path)
# Determine which template and subject to use
if is_parent:
subject = 'Verifizieren Sie Ihr Eltern-Konto für den Baseball Organisator'
template_prefix = 'accounts/email/parent_verification'
else:
subject = 'Willkommen beim Baseball Organisator! Bitte verifizieren Sie Ihr Konto.'
template_prefix = 'accounts/email/player_verification'
context = {
'user': user,
'verification_url': verification_url,
'verification_code': user.verification_code
}
# Render email body from templates
email_body_txt = render_to_string(f'{template_prefix}.txt', context)
email_body_html = render_to_string(f'{template_prefix}.html', context)
# Send or simulate email based on settings
if settings.MTP_EMAIL_SEND == 1:
send_mail(
subject=subject,
message=email_body_txt,
from_email=settings.DEFAULT_FROM_EMAIL, # Make sure this is set in settings.py
recipient_list=[user.email],
html_message=email_body_html,
fail_silently=False,
)
else:
# Simulate email by saving to a file
mbox_content = f"""From: {settings.DEFAULT_FROM_EMAIL}
To: {user.email}
Subject: {subject}
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="boundary"
--boundary
Content-Type: text/plain; charset="utf-8"
{email_body_txt}
--boundary
Content-Type: text/html; charset="utf-8"
{email_body_html}
--boundary--
"""
# Ensure the tmp_mails directory exists
os.makedirs('tmp_mails', exist_ok=True)
# Save the email to a file
file_path = os.path.join('tmp_mails', f'{user.email}_{user.verification_code}.mbox')
with open(file_path, 'w') as f:
f.write(mbox_content)