- Refactored monetary fields across models to use `DecimalField` for improved precision. - Implemented two-factor authentication (2FA) for admin logins with OTP codes. - Added ability to generate admin OTP via management commands. - Updated Docker Compose override for dev-specific port bindings. - Included template for 2FA OTP verification to enhance security. Additional changes: - Upgraded and downgraded various dependencies (e.g., django-celery-beat and yarl). - Replaced float-based calculations with decimal for consistent rounding behavior. - Improved admin user management commands for activation and OTP generation.
36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
import secrets
|
|
|
|
from celery.app import shared_task
|
|
from constance import config
|
|
from django.conf import settings
|
|
from django.core.mail import EmailMessage
|
|
|
|
from engine.core.utils import get_dynamic_email_connection
|
|
|
|
|
|
def generate_otp_code(user) -> str:
|
|
from engine.vibes_auth.models import AdminOTPCode
|
|
|
|
AdminOTPCode.objects.filter(user=user, is_used=False).update(is_used=True)
|
|
code = f"{secrets.randbelow(1000000):06d}"
|
|
AdminOTPCode.objects.create(user=user, code=code)
|
|
return code
|
|
|
|
|
|
@shared_task(queue="default")
|
|
def send_admin_otp_email_task(user_pk: str, code: str) -> tuple[bool, str]:
|
|
from engine.vibes_auth.models import User
|
|
|
|
try:
|
|
user = User.objects.get(pk=user_pk)
|
|
email = EmailMessage(
|
|
subject=f"{settings.PROJECT_NAME} | Admin Login Code",
|
|
body=f"Your admin login code: {code}\n\nValid for 5 minutes.",
|
|
from_email=f"{settings.PROJECT_NAME} <{config.EMAIL_FROM}>",
|
|
to=[user.email],
|
|
connection=get_dynamic_email_connection(),
|
|
)
|
|
email.send()
|
|
except Exception as e:
|
|
return False, str(e)
|
|
return True, str(user.uuid)
|