summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoradityacp2017-04-27 15:29:09 +0530
committeradityacp2017-04-27 15:29:09 +0530
commit82e30d89fb1814d12a1ee40a0f1b81e164f0ed85 (patch)
tree39fe6b90c12cbb9ad5dfd12f273ec55844146757
parent966fd7f33c67b9d20e42c35bd3825119e3ba066f (diff)
downloadonline_test-82e30d89fb1814d12a1ee40a0f1b81e164f0ed85.tar.gz
online_test-82e30d89fb1814d12a1ee40a0f1b81e164f0ed85.tar.bz2
online_test-82e30d89fb1814d12a1ee40a0f1b81e164f0ed85.zip
Changes in email_verification.py, views.py, forms.py
- Updation of user email if multiple users found with same email - Update user registration form - Change email_verification.py to send email
-rw-r--r--yaksh/email_verification.py27
-rw-r--r--yaksh/forms.py4
-rw-r--r--yaksh/views.py140
3 files changed, 119 insertions, 52 deletions
diff --git a/yaksh/email_verification.py b/yaksh/email_verification.py
index 4eded1c..721fa61 100644
--- a/yaksh/email_verification.py
+++ b/yaksh/email_verification.py
@@ -4,14 +4,13 @@ try:
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
+import smtplib
# 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):
@@ -27,8 +26,8 @@ def send_user_mail(user_mail, key):
"""
try:
to = user_mail
- subject = "Yaksh Email Verification"
- msg = dedent("""\
+ subject = 'Yaksh Email Verification'
+ message = dedent("""\
To activate your account and verify your email address,
please click the following link:
{0}/exam/activate/{1}
@@ -41,15 +40,29 @@ def send_user_mail(user_mail, key):
""".format(settings.PRODUCTION_URL, key, settings.REPLY_EMAIL)
)
- send_mail(subject, msg, settings.SENDER_EMAIL, [to])
+ user = settings.EMAIL_HOST_USER
+ pwd = settings.EMAIL_HOST_PASSWORD
+ smtpserver = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
+ smtpserver.ehlo()
+ smtpserver.starttls()
+ smtpserver.ehlo()
+ smtpserver.esmtp_features['auth']='LOGIN DIGEST-MD5 PLAIN'
+
+ smtpserver.login(user, pwd)
+ header = 'To:{0}\nFrom:{1}\nSubject:{2}\n'.format(to,
+ settings.SENDER_EMAIL, subject)
+ message = '{0}\n{1}\n\n'.format(header, message)
+ smtpserver.sendmail(user, to, message)
+ smtpserver.close()
+
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)
+ Please contact {1}.""".format(exc_msg, settings.REPLY_EMAIL)
success = False
return success, msg
diff --git a/yaksh/forms.py b/yaksh/forms.py
index ac4e1b0..b34ad59 100644
--- a/yaksh/forms.py
+++ b/yaksh/forms.py
@@ -144,8 +144,6 @@ class UserRegisterForm(forms.Form):
new_profile.timezone = cleaned_data["timezone"]
if settings.IS_DEVELOPMENT:
new_profile.is_email_verified = True
- new_profile.save()
- return u_name, pwd
else:
new_profile.activation_key = generate_activation_key(new_user.username)
new_profile.key_expiry_time = datetime.datetime.strftime(
@@ -154,7 +152,7 @@ class UserRegisterForm(forms.Form):
"%Y-%m-%d %H:%M:%S"
)
new_profile.save()
- return new_user.email, new_profile.activation_key
+ return u_name, pwd, new_user.email, new_profile.activation_key
class UserLoginForm(forms.Form):
diff --git a/yaksh/views.py b/yaksh/views.py
index fa72008..751efb5 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -16,7 +16,7 @@ from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import Group
from django.forms.models import inlineformset_factory
from django.utils import timezone
-from django.core.exceptions import MultipleObjectsReturned
+from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.conf import settings
import pytz
from taggit.models import Tag
@@ -43,7 +43,7 @@ from .settings import URL_ROOT
from yaksh.models import AssignmentUpload
from .file_utils import extract_files
from .email_verification import send_user_mail, generate_activation_key
-
+from .decorators import email_verified
def my_redirect(url):
@@ -75,7 +75,7 @@ def add_to_group(users):
if not is_moderator(user):
user.groups.add(group)
-
+@email_verified
def index(request):
"""The start page.
"""
@@ -101,15 +101,15 @@ def user_register(request):
form = UserRegisterForm(request.POST)
if form.is_valid():
data = form.cleaned_data
- if settings.IS_DEVELOPMENT:
- u_name, pwd = form.save()
- new_user = authenticate(username=u_name, password=pwd)
- login(request, new_user)
- return index(request)
- user_email, key = form.save()
- success, msg = send_user_mail(user_email, key)
- context = {'activation_msg': msg}
- return my_render_to_response('yaksh/activation_status.html', context)
+ u_name, pwd, user_email, key = form.save()
+ new_user = authenticate(username=u_name, password=pwd)
+ login(request, new_user)
+ if user_email and key:
+ success, msg = send_user_mail(user_email, key)
+ context = {'activation_msg': msg}
+ return my_render_to_response('yaksh/activation_status.html',
+ context)
+ return index(request)
else:
return my_render_to_response('yaksh/register.html', {'form': form},
context_instance=ci)
@@ -127,6 +127,7 @@ def user_logout(request):
@login_required
+@email_verified
def quizlist_user(request, enrolled=None):
"""Show All Quizzes that is available to logged-in user."""
user = request.user
@@ -141,6 +142,7 @@ def quizlist_user(request, enrolled=None):
@login_required
+@email_verified
def results_user(request):
"""Show list of Results of Quizzes that is taken by logged-in user."""
user = request.user
@@ -150,6 +152,7 @@ def results_user(request):
@login_required
+@email_verified
def add_question(request, question_id=None):
user = request.user
ci = RequestContext(request)
@@ -226,6 +229,7 @@ def add_question(request, question_id=None):
@login_required
+@email_verified
def add_quiz(request, course_id, quiz_id=None):
"""To add a new quiz in the database.
Create a new quiz and store it."""
@@ -268,6 +272,7 @@ def add_quiz(request, course_id, quiz_id=None):
@login_required
+@email_verified
def show_all_questionpapers(request, questionpaper_id=None):
user = request.user
ci = RequestContext(request)
@@ -291,6 +296,7 @@ def show_all_questionpapers(request, questionpaper_id=None):
@login_required
+@email_verified
def prof_manage(request, msg=None):
"""Take credentials of the user with professor/moderator
rights/permissions and log in."""
@@ -340,25 +346,12 @@ def user_login(request):
ci = RequestContext(request)
context = {}
if user.is_authenticated():
- if not settings.IS_DEVELOPMENT:
- if not user.profile.is_email_verified:
- context['success'] = False
- context['msg'] = "Your account is not verified"
- return my_render_to_response('yaksh/activation_status.html',
- context, context_instance=ci)
- return index(request)
+ return index(request)
if request.method == "POST":
form = UserLoginForm(request.POST)
if form.is_valid():
user = form.cleaned_data
- if not settings.IS_DEVELOPMENT:
- if not user.profile.is_email_verified:
- context['success'] = False
- context['msg'] = "Your account is not verified. \
- Please verify your account"
- return my_render_to_response('yaksh/activation_status.html',
- context, context_instance=ci)
login(request, user)
return index(request)
else:
@@ -372,8 +365,8 @@ def user_login(request):
context_instance=ci)
-
@login_required
+@email_verified
def start(request, questionpaper_id=None, attempt_num=None):
"""Check the user cedentials and if any quiz is available,
start the exam."""
@@ -430,6 +423,7 @@ def start(request, questionpaper_id=None, attempt_num=None):
@login_required
+@email_verified
def show_question(request, question, paper, error_message=None, notification=None):
"""Show a question if possible."""
user = request.user
@@ -461,6 +455,7 @@ def show_question(request, question, paper, error_message=None, notification=Non
@login_required
+@email_verified
def skip(request, q_id, next_q=None, attempt_num=None, questionpaper_id=None):
user = request.user
paper = get_object_or_404(AnswerPaper, user=request.user, attempt_number=attempt_num,
@@ -482,6 +477,7 @@ def skip(request, q_id, next_q=None, attempt_num=None, questionpaper_id=None):
@login_required
+@email_verified
def check(request, q_id, attempt_num=None, questionpaper_id=None):
"""Checks the answers of the user for particular question"""
user = request.user
@@ -607,6 +603,7 @@ def quit(request, reason=None, attempt_num=None, questionpaper_id=None):
@login_required
+@email_verified
def complete(request, reason=None, attempt_num=None, questionpaper_id=None):
"""Show a page to inform user that the quiz has been compeleted."""
user = request.user
@@ -627,6 +624,7 @@ def complete(request, reason=None, attempt_num=None, questionpaper_id=None):
@login_required
+@email_verified
def add_course(request, course_id=None):
user = request.user
ci = RequestContext(request)
@@ -655,6 +653,7 @@ def add_course(request, course_id=None):
@login_required
+@email_verified
def enroll_request(request, course_id):
user = request.user
ci = RequestContext(request)
@@ -672,6 +671,7 @@ def enroll_request(request, course_id):
@login_required
+@email_verified
def self_enroll(request, course_id):
user = request.user
ci = RequestContext(request)
@@ -686,6 +686,7 @@ def self_enroll(request, course_id):
@login_required
+@email_verified
def courses(request):
user = request.user
ci = RequestContext(request)
@@ -699,6 +700,7 @@ def courses(request):
@login_required
+@email_verified
def course_detail(request, course_id):
user = request.user
ci = RequestContext(request)
@@ -715,6 +717,7 @@ def course_detail(request, course_id):
@login_required
+@email_verified
def enroll(request, course_id, user_id=None, was_rejected=False):
user = request.user
ci = RequestContext(request)
@@ -743,6 +746,7 @@ def enroll(request, course_id, user_id=None, was_rejected=False):
@login_required
+@email_verified
def reject(request, course_id, user_id=None, was_enrolled=False):
user = request.user
ci = RequestContext(request)
@@ -766,6 +770,7 @@ def reject(request, course_id, user_id=None, was_enrolled=False):
@login_required
+@email_verified
def toggle_course_status(request, course_id):
user = request.user
if not is_moderator(user):
@@ -784,6 +789,7 @@ def toggle_course_status(request, course_id):
@login_required
+@email_verified
def show_statistics(request, questionpaper_id, attempt_number=None):
user = request.user
if not is_moderator(user):
@@ -810,6 +816,7 @@ def show_statistics(request, questionpaper_id, attempt_number=None):
@login_required
+@email_verified
def monitor(request, quiz_id=None):
"""Monitor the progress of the papers taken so far."""
@@ -906,6 +913,7 @@ def _remove_already_present(questionpaper_id, questions):
@login_required
+@email_verified
def design_questionpaper(request, quiz_id, questionpaper_id=None):
user = request.user
@@ -992,6 +1000,7 @@ def design_questionpaper(request, quiz_id, questionpaper_id=None):
@login_required
+@email_verified
def show_all_questions(request):
"""Show a list of all the questions currently in the database."""
@@ -1061,6 +1070,7 @@ def show_all_questions(request):
context_instance=ci)
@login_required
+@email_verified
def user_data(request, user_id, questionpaper_id=None):
"""Render user data."""
current_user = request.user
@@ -1075,6 +1085,7 @@ def user_data(request, user_id, questionpaper_id=None):
@login_required
+@email_verified
def download_csv(request, questionpaper_id):
user = request.user
if not is_moderator(user):
@@ -1120,6 +1131,7 @@ def download_csv(request, questionpaper_id):
return response
@login_required
+@email_verified
def grade_user(request, quiz_id=None, user_id=None, attempt_number=None):
"""Present an interface with which we can easily grade a user's papers
and update all their marks and also give comments for each paper.
@@ -1190,6 +1202,7 @@ def grade_user(request, quiz_id=None, user_id=None, attempt_number=None):
@login_required
+@email_verified
def view_profile(request):
""" view moderators and users profile """
user = request.user
@@ -1212,6 +1225,7 @@ def view_profile(request):
@login_required
+@email_verified
def edit_profile(request):
""" edit profile details facility for moderator and students """
@@ -1250,6 +1264,7 @@ def edit_profile(request):
@login_required
+@email_verified
def search_teacher(request, course_id):
""" search teachers for the course """
user = request.user
@@ -1279,6 +1294,7 @@ def search_teacher(request, course_id):
@login_required
+@email_verified
def add_teacher(request, course_id):
""" add teachers to the course """
@@ -1307,6 +1323,7 @@ def add_teacher(request, course_id):
@login_required
+@email_verified
def remove_teachers(request, course_id):
""" remove user from a course """
@@ -1340,6 +1357,7 @@ def test_mode(user, godmode=False, questions_list=None, quiz_id=None):
@login_required
+@email_verified
def test_quiz(request, mode, quiz_id):
"""creates a trial quiz for the moderators"""
godmode = True if mode == "godmode" else False
@@ -1353,6 +1371,7 @@ def test_quiz(request, mode, quiz_id):
@login_required
+@email_verified
def view_answerpaper(request, questionpaper_id):
user = request.user
quiz = get_object_or_404(QuestionPaper, pk=questionpaper_id).quiz
@@ -1368,6 +1387,7 @@ def view_answerpaper(request, questionpaper_id):
@login_required
+@email_verified
def create_demo_course(request):
""" creates a demo course for user """
user = request.user
@@ -1384,6 +1404,7 @@ def create_demo_course(request):
@login_required
+@email_verified
def grader(request, extra_context=None):
user = request.user
if not is_moderator(user):
@@ -1397,6 +1418,7 @@ def grader(request, extra_context=None):
@login_required
+@email_verified
def regrade(request, course_id, question_id=None, answerpaper_id=None, questionpaper_id=None):
user = request.user
course = get_object_or_404(Course, pk=course_id)
@@ -1418,6 +1440,7 @@ def regrade(request, course_id, question_id=None, answerpaper_id=None, questionp
return grader(request, extra_context={'details': details})
@login_required
+@email_verified
def download_course_csv(request, course_id):
user = request.user
if not is_moderator(user):
@@ -1464,6 +1487,11 @@ def activate_user(request, key):
profile = get_object_or_404(Profile, activation_key=key)
context = {}
context['success'] = False
+ if profile.is_email_verified:
+ context['activation_msg'] = "Your account is already verified"
+ return my_render_to_response('yaksh/activation_status.html', context,
+ context_instance=ci)
+
if timezone.now() > profile.key_expiry_time:
context['msg'] = dedent("""
Your activation time expired.
@@ -1477,31 +1505,59 @@ def activate_user(request, key):
return my_render_to_response('yaksh/activation_status.html', context,
context_instance=ci)
-def new_activation(request):
+def new_activation(request, email=None):
ci = RequestContext(request)
context = {}
if request.method == "POST":
email = request.POST.get('email')
- user = get_object_or_404(User, email=email)
- if not user.profile.is_email_verified:
- user.profile.activation_key = generate_activation_key(user.username)
- user.profile.key_expiry_time = datetime.strftime(
- datetime.now() + \
- timedelta(minutes=20), "%Y-%m-%d %H:%M:%S"
- )
- user.profile.save()
- success, msg = send_user_mail(user.email, user.profile.activation_key)
- if success:
- context['new_activation_msg'] = msg
- else:
- context['msg'] = msg
+
+ try:
+ user = User.objects.get(email=email)
+ except MultipleObjectsReturned:
+ context['email_err_msg'] = "Multiple entries found for this email"\
+ "Please change your email"
+ return my_render_to_response('yaksh/activation_status.html', context,
+ context_instance=ci)
+ except ObjectDoesNotExist:
+ context['success'] = False
+ context['msg'] = "Your account is not verified. \
+ Please verify your account"
+ return render_to_response('yaksh/activation_status.html',
+ context, context_instance=ci)
+
+ if not user.profile.is_email_verified:
+ user.profile.activation_key = generate_activation_key(user.username)
+ user.profile.key_expiry_time = datetime.strftime(
+ datetime.now() + \
+ timedelta(minutes=20), "%Y-%m-%d %H:%M:%S"
+ )
+ user.profile.save()
+ success, msg = send_user_mail(user.email, user.profile.activation_key)
+ if success:
+ context['activation_msg'] = msg
else:
- context['new_activation_msg'] = "Your account is already verified"
+ context['msg'] = msg
+ else:
+ context['activation_msg'] = "Your account is already verified"
return my_render_to_response('yaksh/activation_status.html', context,
context_instance=ci)
+def update_email(request):
+ if request.method == "POST":
+ email = request.POST.get('email')
+ username = request.POST.get('username')
+ user = get_object_or_404(User, username=username)
+ user.email = email
+ user.save()
+ return new_activation(request, email)
+ else:
+ context['email_err_msg'] = "Please Update your email"
+ return my_render_to_response('yaksh/activation_status.html', context,
+ context_instance=ci)
+
@login_required
+@email_verified
def download_assignment_file(request, quiz_id, question_id=None, user_id=None):
user = request.user
qp = QuestionPaper.objects.get(quiz_id=quiz_id)