feat: add EMAIL_FROM_ADDRESS setting
This commit is contained in:
parent
f61bfc60d2
commit
1639fca725
@ -1,5 +1,6 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
from django.core.mail import send_mail
|
from django.core.mail import send_mail
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
@ -22,7 +23,7 @@ class StaffAddForm(UserCreationForm):
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
label="Username",
|
label="Username",
|
||||||
required=False
|
required=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
first_name = forms.CharField(
|
first_name = forms.CharField(
|
||||||
@ -89,7 +90,7 @@ class StaffAddForm(UserCreationForm):
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
label="Password",
|
label="Password",
|
||||||
required=False
|
required=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
password2 = forms.CharField(
|
password2 = forms.CharField(
|
||||||
@ -101,7 +102,7 @@ class StaffAddForm(UserCreationForm):
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
label="Password Confirmation",
|
label="Password Confirmation",
|
||||||
required=False
|
required=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
class Meta(UserCreationForm.Meta):
|
class Meta(UserCreationForm.Meta):
|
||||||
@ -118,13 +119,17 @@ class StaffAddForm(UserCreationForm):
|
|||||||
user.email = self.cleaned_data.get("email")
|
user.email = self.cleaned_data.get("email")
|
||||||
|
|
||||||
# Generate a username based on first and last name and registration date
|
# Generate a username based on first and last name and registration date
|
||||||
registration_date = datetime.now().strftime('%Y%m%d%H%M')
|
registration_date = datetime.now().strftime("%Y%m%d%H%M")
|
||||||
generated_username = f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}"
|
generated_username = (
|
||||||
|
f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}"
|
||||||
|
)
|
||||||
|
|
||||||
# Check if the generated username already exists, and regenerate if needed
|
# Check if the generated username already exists, and regenerate if needed
|
||||||
while User.objects.filter(username=generated_username).exists():
|
while User.objects.filter(username=generated_username).exists():
|
||||||
registration_date = datetime.now().strftime('%Y%m%d%H%M')
|
registration_date = datetime.now().strftime("%Y%m%d%H%M")
|
||||||
generated_username = f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}".replace(" ", "")
|
generated_username = f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}".replace(
|
||||||
|
" ", ""
|
||||||
|
)
|
||||||
|
|
||||||
user.username = generated_username
|
user.username = generated_username
|
||||||
|
|
||||||
@ -136,9 +141,9 @@ class StaffAddForm(UserCreationForm):
|
|||||||
|
|
||||||
# Send email with the generated credentials
|
# Send email with the generated credentials
|
||||||
send_mail(
|
send_mail(
|
||||||
'Your account credentials',
|
"Your account credentials",
|
||||||
f'Your username: {generated_username}\nYour password: {generated_password}',
|
f"Your username: {generated_username}\nYour password: {generated_password}",
|
||||||
'from@example.com',
|
"from@example.com",
|
||||||
[user.email],
|
[user.email],
|
||||||
fail_silently=False,
|
fail_silently=False,
|
||||||
)
|
)
|
||||||
@ -153,8 +158,7 @@ class StudentAddForm(UserCreationForm):
|
|||||||
attrs={"type": "text", "class": "form-control", "id": "username_id"}
|
attrs={"type": "text", "class": "form-control", "id": "username_id"}
|
||||||
),
|
),
|
||||||
label="Username",
|
label="Username",
|
||||||
required=False
|
required=False,
|
||||||
|
|
||||||
)
|
)
|
||||||
address = forms.CharField(
|
address = forms.CharField(
|
||||||
max_length=30,
|
max_length=30,
|
||||||
@ -245,8 +249,7 @@ class StudentAddForm(UserCreationForm):
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
label="Password",
|
label="Password",
|
||||||
required=False
|
required=False,
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
password2 = forms.CharField(
|
password2 = forms.CharField(
|
||||||
@ -258,7 +261,7 @@ class StudentAddForm(UserCreationForm):
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
label="Password Confirmation",
|
label="Password Confirmation",
|
||||||
required=False
|
required=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
# def validate_email(self):
|
# def validate_email(self):
|
||||||
@ -282,13 +285,17 @@ class StudentAddForm(UserCreationForm):
|
|||||||
user.email = self.cleaned_data.get("email")
|
user.email = self.cleaned_data.get("email")
|
||||||
|
|
||||||
# Generate a username based on first and last name and registration date
|
# Generate a username based on first and last name and registration date
|
||||||
registration_date = datetime.now().strftime('%Y%m%d%H%M')
|
registration_date = datetime.now().strftime("%Y%m%d%H%M")
|
||||||
generated_username = f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}"
|
generated_username = (
|
||||||
|
f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}"
|
||||||
|
)
|
||||||
|
|
||||||
# Check if the generated username already exists, and regenerate if needed
|
# Check if the generated username already exists, and regenerate if needed
|
||||||
while User.objects.filter(username=generated_username).exists():
|
while User.objects.filter(username=generated_username).exists():
|
||||||
registration_date = datetime.now().strftime('%Y%m%d%H%M')
|
registration_date = datetime.now().strftime("%Y%m%d%H%M")
|
||||||
generated_username = f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}".replace(" ", "")
|
generated_username = f"{user.first_name.lower()}{user.last_name.lower()}{registration_date}".replace(
|
||||||
|
" ", ""
|
||||||
|
)
|
||||||
|
|
||||||
user.username = generated_username
|
user.username = generated_username
|
||||||
|
|
||||||
@ -300,9 +307,9 @@ class StudentAddForm(UserCreationForm):
|
|||||||
|
|
||||||
# Send email with the generated credentials
|
# Send email with the generated credentials
|
||||||
send_mail(
|
send_mail(
|
||||||
'Your account credentials',
|
"Your account credentials",
|
||||||
f'Your username: {generated_username}\nYour password: {generated_password}',
|
f"Your username: {generated_username}\nYour password: {generated_password}",
|
||||||
'from@example.com',
|
settings.EMAIL_FROM_ADDRESS,
|
||||||
[user.email],
|
[user.email],
|
||||||
fail_silently=False,
|
fail_silently=False,
|
||||||
)
|
)
|
||||||
|
|||||||
@ -189,12 +189,17 @@ MEDIA_ROOT = os.path.join(BASE_DIR, "media")
|
|||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
# E-mail configuration
|
# E-mail configuration
|
||||||
|
|
||||||
EMAIL_BACKEND = config("EMAIL_BACKEND", default="django.core.mail.backends.smtp.EmailBackend")
|
EMAIL_BACKEND = config(
|
||||||
EMAIL_HOST = config("EMAIL_HOST", default="smtp.gmail.com") # Gmail as the email host, but you can change it
|
"EMAIL_BACKEND", default="django.core.mail.backends.smtp.EmailBackend"
|
||||||
|
)
|
||||||
|
EMAIL_HOST = config(
|
||||||
|
"EMAIL_HOST", default="smtp.gmail.com"
|
||||||
|
) # Gmail as the email host, but you can change it
|
||||||
EMAIL_PORT = config("EMAIL_PORT", default=587)
|
EMAIL_PORT = config("EMAIL_PORT", default=587)
|
||||||
EMAIL_USE_TLS = True
|
EMAIL_USE_TLS = True
|
||||||
EMAIL_HOST_USER = config("USER_EMAIL")
|
EMAIL_HOST_USER = config("USER_EMAIL")
|
||||||
EMAIL_HOST_PASSWORD = config("USER_PASSWORD")
|
EMAIL_HOST_PASSWORD = config("USER_PASSWORD")
|
||||||
|
EMAIL_FROM_ADDRESS = config("EMAIL_FROM_ADDRESS")
|
||||||
|
|
||||||
# crispy config
|
# crispy config
|
||||||
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
|
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user