diff options
author | CruiseDevice | 2018-08-20 14:31:55 +0530 |
---|---|---|
committer | CruiseDevice | 2018-08-20 14:40:31 +0530 |
commit | 583c0f813306c43eb623198158ad82dd37e72a4b (patch) | |
tree | 5811b8d47724b5c3dec00a761a6e1dbffcdf532d /sbhs | |
parent | 1df056c7e273d07e920f04fe6b41b7a68ee267df (diff) | |
download | sbhs_server-583c0f813306c43eb623198158ad82dd37e72a4b.tar.gz sbhs_server-583c0f813306c43eb623198158ad82dd37e72a4b.tar.bz2 sbhs_server-583c0f813306c43eb623198158ad82dd37e72a4b.zip |
add send email and email verify decorator
Diffstat (limited to 'sbhs')
-rw-r--r-- | sbhs/decorators.py | 27 | ||||
-rw-r--r-- | sbhs/send_emails.py | 57 |
2 files changed, 84 insertions, 0 deletions
diff --git a/sbhs/decorators.py b/sbhs/decorators.py new file mode 100644 index 0000000..cd05e38 --- /dev/null +++ b/sbhs/decorators.py @@ -0,0 +1,27 @@ +from django.shortcuts import render +from django.conf import settings + +def user_has_profile(user): + return hasattr(user, 'profile') + +def email_verified(func): + """ + This decorator is used to check if email is verified. + If email is not verified then redirect user for email + verification. + """ + + def is_email_verified(request, *args, **kwargs): + user = request.user + context = {} + if not settings.IS_DEVELOPMENT: + if user.is_authenticated() and user_has_profile(user): + if not user.profile.is_email_verified: + context['success'] = False + context['msg'] = "Your account is not verified. \ + Please verify your account" + return render( + request, 'account/activation_status.html', context + ) + return func(request, *args, **kwargs) + return is_email_verified
\ No newline at end of file diff --git a/sbhs/send_emails.py b/sbhs/send_emails.py new file mode 100644 index 0000000..c713704 --- /dev/null +++ b/sbhs/send_emails.py @@ -0,0 +1,57 @@ +import hashlib +try: + from string import letters +except ImportError: + from string import ascii_letters as letters +from string import digits, punctuation + +from django.utils.crypto import get_random_string +from django.conf import settings +from django.core.mail import send_mail +from textwrap import dedent + +def generate_activation_key(username): + """ + Generate hashed secret key for email activation + """ + chars = letters + digits + punctuation + secret_key = get_random_string(20,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 = 'SBHS Email Verification' + message = dedent("""\ + To activate your account and verify your email address, + please click the following link: + {0}/account/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, + {3} + """.format(settings.PRODUCTION_URL, key, settings.REPLY_EMAIL, + settings.SENDER_NAME + ) + ) + + send_mail(subject, message, 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 {1}.""".format(exc_msg, settings.REPLY_EMAIL) + success = False + + return success, msg
\ No newline at end of file |