-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from unicef/generate-pwd
send through email
- Loading branch information
Showing
3 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |