summaryrefslogtreecommitdiff
path: root/yaksh/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'yaksh/views.py')
-rw-r--r--yaksh/views.py65
1 files changed, 64 insertions, 1 deletions
diff --git a/yaksh/views.py b/yaksh/views.py
index b4cb844..838041e 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -45,7 +45,7 @@ from yaksh.forms import (
UploadFileForm, get_object_form, FileForm, QuestionPaperForm
)
from .settings import URL_ROOT
-from .file_utils import extract_files
+from .file_utils import extract_files, is_csv, headers_present
from .send_emails import send_user_mail, generate_activation_key, send_bulk_mail
from .decorators import email_verified, has_profile
@@ -1796,6 +1796,69 @@ def download_assignment_file(request, quiz_id, question_id=None, user_id=None):
@login_required
@email_verified
+def upload_users(request, course_id):
+ user = request.user
+ ci = RequestContext(request)
+ course = get_object_or_404(Course, pk=course_id)
+ context = {'course': course}
+
+ if not (course.is_teacher(user) or course.is_creator(user)):
+ msg = 'You do not have permissions to this course.'
+ return complete(request, reason=msg)
+ if request.method == 'POST':
+ if 'csv_file' not in request.FILES:
+ context['message'] = "Please upload a CSV file."
+ return my_render_to_response('yaksh/course_detail.html', context,
+ context_instance=ci)
+ csv_file = request.FILES['csv_file']
+ if not is_csv(csv_file):
+ context['message'] = "The file uploaded is not a CSV file."
+ return my_render_to_response('yaksh/course_detail.html', context,
+ context_instance=ci)
+ headers = {'firstname':'', 'lastname':'', 'email':''}
+ reader = csv.DictReader(csv_file.read().decode('utf-8').splitlines())
+ if not headers_present(reader, headers):
+ context['message'] = "The CSV file does not contain the required headers"
+ return my_render_to_response('yaksh/course_detail.html', context,
+ context_instance=ci)
+ context['upload_details'] = _read_user_csv(reader, headers, course)
+ return my_render_to_response('yaksh/course_detail.html', context,
+ context_instance=ci)
+
+
+def _read_user_csv(reader, headers, course):
+ upload_details = ["Upload Summary:"]
+ counter = 0;
+ for row in reader:
+ counter += 1
+ email, first_name, last_name = map(str.strip, [row[headers['email']],
+ row[headers['firstname']], row[headers['lastname']]])
+ if not email or not first_name or not last_name:
+ upload_details.append("{0} -- Missing Values".format(counter))
+ continue
+ user = User.objects.filter(email=email)
+ if user.exists():
+ upload_details.append("{0} -- {1} -- Email Already Exists".format(
+ counter, email))
+ else:
+ try:
+ user = User.objects.create_user(username=email, password=email,
+ email=email, first_name=first_name, last_name=last_name)
+ except IntegrityError:
+ upload_details.append("{0} -- {1} -- User Already Exists".format(
+ counter, email))
+ else:
+ Profile.objects.create(user=user, is_email_verified=True)
+ course.students.add(user)
+ upload_details.append("{0} -- {1} -- User Added Successfully".format(
+ counter, email))
+ if counter == 0:
+ upload_details.append("No rows in the CSV file")
+ return upload_details
+
+
+@login_required
+@email_verified
def duplicate_course(request, course_id):
user = request.user
course = get_object_or_404(Course, pk=course_id)