From 8bbcd2163c20bc3bd7b501d108ede59614deced5 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Tue, 14 Nov 2017 17:27:05 +0530 Subject: Added sample csv for user to download and upload. --- yaksh/fixtures/sample_user_upload.csv | 2 ++ yaksh/templates/yaksh/course_detail.html | 35 +++++++++++++++++---------- yaksh/urls.py | 2 ++ yaksh/views.py | 41 +++++++++++++++++++++----------- 4 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 yaksh/fixtures/sample_user_upload.csv (limited to 'yaksh') 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 @@ Send Mail -
  • -
    - {% csrf_token %} - - -
    - -
  • +
    + {% csrf_token %} + + +
    +
    {% if message %} {% endif %} +
    {% if state == 'mail' %}
    Send Mails to Students

    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,14 +1966,33 @@ 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): Profile.objects.update_or_create(user=user, defaults=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): -- cgit