Skip to content

Commit

Permalink
send through email
Browse files Browse the repository at this point in the history
  • Loading branch information
domdinicola committed Aug 16, 2023
1 parent 79a71bb commit 8ce6489
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/aurora/security/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from .ad import ADUSerMixin
from .forms import AuroraRoleForm
from .utils import generate_pwd

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -61,6 +62,10 @@ def hijack(self, request, pk):
hijacked = self.get_object(request, pk)
impersonate(request, hijacked)

@button()
def generate_password(self, request, pk):
generate_pwd(pk)


class UserProfileAdmin(SmartModelAdmin):
search_fields = ("user__username",)
Expand Down
40 changes: 40 additions & 0 deletions src/aurora/security/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import random
import string

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.mail import send_mail


def generate_password(length=20):
pwd = ""
count = 0
length = max(8, length)
while count < length:
upper = [random.choice(string.ascii_uppercase)]
lower = [random.choice(string.ascii_lowercase)]
num = [random.choice(string.digits)]
symbol = [random.choice(string.punctuation)]
everything = upper + lower + num + symbol
pwd += random.choice(everything)
count += 1
return pwd


def generate_pwd(user_pk):
subject = "Aurora Credentials"
pwd = generate_password()
user = get_user_model().objects.get(pk=user_pk)
user.set_password(pwd)
user.save()

message = (
f"Dear {user.first_name}, \n"
f"you can login to http://register.unicef.org using {user.email} and {pwd} \n\n"
f"Regards, \n"
f"Aurora team"
)
recipient_list = [
user.email,
]
send_mail(subject, message, settings.EMAIL_HOST_USER, recipient_list)

0 comments on commit 8ce6489

Please sign in to comment.