diff options
author | adityacp | 2017-04-07 15:58:55 +0530 |
---|---|---|
committer | adityacp | 2017-04-07 16:12:36 +0530 |
commit | 6ac440c1ce1d26ce86376ee6bbba70525c5da945 (patch) | |
tree | 05ebbf8d4d3f776ebe5e5822bcf29f6b4e0f82d0 /yaksh/email_verification.py | |
parent | 09429a3a01a591e1a87df51e8584bb4358508b67 (diff) | |
download | online_test-6ac440c1ce1d26ce86376ee6bbba70525c5da945.tar.gz online_test-6ac440c1ce1d26ce86376ee6bbba70525c5da945.tar.bz2 online_test-6ac440c1ce1d26ce86376ee6bbba70525c5da945.zip |
Add new module to send email for verification
Diffstat (limited to 'yaksh/email_verification.py')
-rw-r--r-- | yaksh/email_verification.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/yaksh/email_verification.py b/yaksh/email_verification.py new file mode 100644 index 0000000..4eded1c --- /dev/null +++ b/yaksh/email_verification.py @@ -0,0 +1,55 @@ +# Local imports +try: + from string import letters +except ImportError: + from string import ascii_letters as letters +from string import digits, punctuation +# Local imports +import hashlib +from random import randint +from textwrap import dedent + +# Django imports +from django.utils.crypto import get_random_string +from django.core.mail import send_mail +from django.conf import settings + +def generate_activation_key(username): + """ Generate hashed secret key for email activation """ + chars = letters + digits + punctuation + secret_key = get_random_string(randint(10, 40), chars) + return hashlib.sha256((secret_key + username).encode('utf-8')).hexdigest() + +def send_user_mail(user_mail, key): + """ Send mail to user whose email is to be verified + This function should get two args i.e user_email and secret_key. + The activation url is generated from settings.PRODUCTION_URL and key. + """ + try: + to = user_mail + subject = "Yaksh Email Verification" + msg = dedent("""\ + To activate your account and verify your email address, + please click the following link: + {0}/exam/activate/{1} + If clicking the link above does not work, + copy and paste the URL in a new browser window instead. + For any issue, please write us on {2} + + Regards + Yaksh Team + """.format(settings.PRODUCTION_URL, key, settings.REPLY_EMAIL) + ) + + send_mail(subject, msg, settings.SENDER_EMAIL, [to]) + msg = "An activation link is sent to your registered email.\ + Please activate the link within 20 minutes." + success = True + except Exception as exc_msg: + msg = """Error: {0}. Please check your email address.\ + If email address is correct then + Please contact your \ + instructor/administrator.""".format(exc_msg) + success = False + + return success, msg |