diff options
Diffstat (limited to 'yaksh')
-rw-r--r-- | yaksh/fixtures/sample_user_upload.csv | 2 | ||||
-rw-r--r-- | yaksh/templates/yaksh/course_detail.html | 35 | ||||
-rw-r--r-- | yaksh/urls.py | 2 | ||||
-rw-r--r-- | yaksh/views.py | 41 |
4 files changed, 53 insertions, 27 deletions
diff --git a/yaksh/fixtures/sample_user_upload.csv b/yaksh/fixtures/sample_user_upload.csv new file mode 100644 index 0000000..2edf710 --- /dev/null +++ b/yaksh/fixtures/sample_user_upload.csv @@ -0,0 +1,2 @@ +firstname,lastname,email,username,password,institute,department,roll_no,remove +sample,user,sampleuser@xyz.com,sample_user,sample,sample,sample,sample123,False diff --git a/yaksh/templates/yaksh/course_detail.html b/yaksh/templates/yaksh/course_detail.html index 4b88ecb..93a7048 100644 --- a/yaksh/templates/yaksh/course_detail.html +++ b/yaksh/templates/yaksh/course_detail.html @@ -41,23 +41,31 @@ <a href="{{URL_ROOT}}/exam/manage/send_mail/{{ course.id }}/"> Send Mail</a> </li> - <li> - <form id="upload_users" action="{{ URL_ROOT }}/exam/manage/upload_users/{{course.id}}/" - method="POST" enctype="multipart/form-data"> - {% csrf_token %} - <input type="file" name="csv_file" /> - <button class="btn btn-primary" type=submit> Upload Users <span class="glyphicon glyphicon-open"/></button> - </form> - <div class="alert alert-info" role="alert"> - - The uploaded csv should have headers exactly same as mentioned below:<br> - <b>firstname, lastname, email</b><br></br> - - <b>Users created will have username and password same as their email</b> - </div> - </li> </ul> </div> </div> <div class="col-md-9 col-md-offset-2 main"> + <form id="upload_users" action="{{ URL_ROOT }}/exam/manage/upload_users/{{course.id}}/" + method="POST" enctype="multipart/form-data"> + {% csrf_token %} + <input type="file" name="csv_file" /> + <button class="btn btn-primary" type=submit> Upload Users <span class="glyphicon glyphicon-open"/></button> + </form> + <div class="alert alert-info" role="alert"> + <p> + - The uploaded csv should have headers exactly same as mentioned below:<br /> + <b>firstname, lastname, email, username, password, institute, roll_no, department, + remove</b><br /> + - Mandatory fields are <b> firstname, lastname and email. </b><br /> + - Other fields are optional. <br /> + - If username and password are not provided then + <b>Users created will have username and password same as their email</b> + </p> + <p> + <b> Click <a class="btn btn-success" href="{{ URL_ROOT }}/exam/manage/download_sample_csv/ +">here</a> to download a sample CSV, edit and upload it</b> + </p> + </div> <div class="row"> {% if message %} <div class="alert alert-warning" role="alert"> @@ -73,6 +81,7 @@ {% endfor %} </div> {% endif %} + <hr> {% if state == 'mail' %} <div id="enrolled-students"> <center><b><u>Send Mails to Students</u></b></center><br> diff --git a/yaksh/urls.py b/yaksh/urls.py index 1a50ca2..7f484b9 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -111,4 +111,6 @@ urlpatterns = [ views.download_assignment_file, name="download_quiz_assignment"), url(r'^manage/courses/download_yaml_template/', views.download_yaml_template, name="download_yaml_template"), + url(r'^manage/download_sample_csv/', + views.download_sample_csv, name="download_sample_csv"), ] diff --git a/yaksh/views.py b/yaksh/views.py index 8d965f1..c46329f 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -37,7 +37,7 @@ from yaksh.models import ( HookTestCase, IntegerTestCase, McqTestCase, Profile, QuestionPaper, QuestionSet, Quiz, Question, StandardTestCase, StdIOBasedTestCase, StringTestCase, TestCase, User, - get_model_class + get_model_class, FIXTURES_DIR_PATH ) from yaksh.forms import ( UserRegisterForm, UserLoginForm, QuizForm, QuestionForm, @@ -1912,14 +1912,12 @@ def _read_user_csv(reader, course): continue user_defaults = {'email': email, 'first_name': first_name, 'last_name': last_name} - user, created = _create_or_update_user(username, user_defaults) + user, created = _create_or_update_user(username, password, user_defaults) profile_defaults = {'institute': institute, 'roll_number': roll_no, 'department': department, 'is_email_verified': True} _create_or_update_profile(user, profile_defaults) if created: state = "Added" - user.set_password(password) - user.save() course.students.add(user) else: state = "Updated" @@ -1936,12 +1934,10 @@ def _get_csv_values(row, fields): email, first_name, last_name = map(str.strip, [row['email'], row['firstname'], row['lastname']]) - if 'password' in fields: + password = email + username = email + if 'password' in fields and row['password']: password = row['password'].strip() - if not password: - password = email - else: - password = email if 'roll_no' in fields: roll_no = row['roll_no'].strip() if 'institute' in fields: @@ -1950,10 +1946,8 @@ def _get_csv_values(row, fields): department = row['department'].strip() if 'remove' in fields: remove = row['remove'].strip() - if 'username' in fields: + if 'username' in fields and row['username']: username = row['username'].strip() - else: - username = email if 'remove' in fields: remove = row['remove'] return (username, email, first_name, last_name, password, roll_no, institute, @@ -1972,8 +1966,12 @@ def _add_to_course(user, course): return True -def _create_or_update_user(username, defaults): - return User.objects.update_or_create(username=username, defaults=defaults) +def _create_or_update_user(username, password, defaults): + user, created = User.objects.update_or_create(username=username, + defaults=defaults) + user.set_password(password) + user.save() + return user, created def _create_or_update_profile(user, defaults): @@ -1982,6 +1980,21 @@ def _create_or_update_profile(user, defaults): @login_required @email_verified +def download_sample_csv(request): + user = request.user + if not is_moderator(user): + raise Http404('You are not allowed to view this page!') + csv_file_path = os.path.join(FIXTURES_DIR_PATH, + "sample_user_upload.csv") + with open(csv_file_path, 'rb') as csv_file: + response = HttpResponse(csv_file.read(), content_type='text/csv') + response['Content-Disposition'] = 'attachment;\ + filename="sample_user_upload"' + return response + + +@login_required +@email_verified def duplicate_course(request, course_id): user = request.user course = get_object_or_404(Course, pk=course_id) |