diff options
-rw-r--r-- | yaksh/templates/yaksh/course_students.html | 154 | ||||
-rw-r--r-- | yaksh/test_views.py | 360 | ||||
-rw-r--r-- | yaksh/urls.py | 34 | ||||
-rw-r--r-- | yaksh/views.py | 118 |
4 files changed, 518 insertions, 148 deletions
diff --git a/yaksh/templates/yaksh/course_students.html b/yaksh/templates/yaksh/course_students.html index 2052a69..e8ca800 100644 --- a/yaksh/templates/yaksh/course_students.html +++ b/yaksh/templates/yaksh/course_students.html @@ -3,7 +3,7 @@ <div id="accordian-upload" class="card"> <div class="card-header"> <a class="card-link" data-toggle="collapse" href="#upload_users_csv"> - Upload Users <i class="fa fa-angle-down"></i> + Upload Users <i class="fa fa-angle-down"></i> </a> </div> </div> @@ -48,60 +48,59 @@ <div id="accordian-request" class="card"> <div class="card-header"> <a class="card-link" data-toggle="collapse" href="#requested"> - Requested Students <i class="fa fa-angle-down"></i> + Requested Students <i class="fa fa-angle-down"></i> </a> </div> </div> <div id="requested" class="collapse show" data-parent="#accordion-request"> - {% if requested %} + {% if requested_users %} <br> - <input type="checkbox" class="checkall"/> - <font size="5">Select all</font> + <input type="checkbox" class="checkall"/> Select all <div id="enroll-all"> - <form action="{% url 'yaksh:enroll_users' course.id %}" method="post"> + <form action="{% url 'yaksh:enroll_reject_user' course.id %}" method="post"> {% csrf_token %} <table id="requested_table" class="tablesorter table table-striped table-responsive-sm course-detail" data-sortlist="[1,0]"> <thead> <th></th> - <th>Full Name <i class="fa fa-sort"></i></th> - <th>Email <i class="fa fa-sort"></i></th> - <th>Roll Number <i class="fa fa-sort"></i></th> - <th>Institute <i class="fa fa-sort"></i></th> - <th>Department <i class="fa fa-sort"></i></th> + <th>Full Name <i class="fa fa-sort"></i></th> + <th>Email <i class="fa fa-sort"></i></th> + <th>Roll Number <i class="fa fa-sort"></i></th> + <th>Institute <i class="fa fa-sort"></i></th> + <th>Department <i class="fa fa-sort"></i></th> <th>Enroll/Reject</th> </thead> <tbody> - {% for request in requested %} + {% for user in requested_users %} <tr> <td> {{ forloop.counter }}. - <input type="checkbox" name="check" value="{{ request.id }}"> + <input type="checkbox" name="check" value="{{ user.id }}"> </td> - <td>{{request.get_full_name}}</td> - <td> {{request.email}}</td> - {% with request.profile as request_profile %} - <td> {{request_profile.roll_number}}</td> - <td> {{request_profile.institute}}</td> - <td> {{request_profile.department}}</td> + <td>{{user.get_full_name}}</td> + <td> {{user.email}}</td> + {% with user.profile as user_profile %} + <td> {{user_profile.roll_number}}</td> + <td> {{user_profile.institute}}</td> + <td> {{user_profile.department}}</td> {% endwith %} <td> - <a class="btn btn-success" - href="{% url 'yaksh:enroll_user' course.id request.id %}"> - <i class="fa fa-plus-square"></i> - Enroll </a> - <a class="btn btn-danger" - href="{% url 'yaksh:reject_user' course.id request.id %}"> - <i class="fa fa-minus-square"></i> - Reject </a> + <a class="btn btn-success" href="{% url 'yaksh:enroll_user' course.id user.id %}"> + <i class="fa fa-plus-square"></i> + Enroll + </a> + <a class="btn btn-danger" href="{% url 'yaksh:reject_user' course.id user.id %}"> + <i class="fa fa-minus-square"></i> + Reject + </a> </td> </tr> {% endfor %} </tbody> </table> - <button class="btn btn-success btn-lg" type="submit" name='enroll' value='enroll'> - <i class="fa fa-plus-square"></i> - Enroll Selected - </button> <br> + <input class="btn btn-success btn-lg" type="submit" name='enroll' value='enroll selected'> + </input> + <input class="btn btn-danger btn-lg" type="submit" name='reject' value='reject selected'> + </input> </form> </div> {% else %} @@ -116,45 +115,43 @@ <div id="accordian-enrolled" class="card"> <div class="card-header"> <a class="card-link" data-toggle="collapse" href="#enrolled"> - Enrolled Students <i class="fa fa-angle-down"></i> + Enrolled Students <i class="fa fa-angle-down"></i> </a> </div> </div> <div id="enrolled" class="collapse hide" data-parent="#accordion-enrolled"> - {% if enrolled %} + {% if enrolled_users %} <br> - <input type="checkbox" class="reject"/> - <font size="5">Select all</font> + <input type="checkbox" class="reject"/> Select all <div id="reject"> - <form action="{% url 'yaksh:reject_users' course.id %}" method="post" id="reject-form"> + <form action="{% url 'yaksh:reject_enrolled_users' course.id %}" method="post" id="reject-form"> {% csrf_token %} <table id="enrolled_table" class="tablesorter table table-striped table-responsive-sm course-detail" data-sortlist="[1,0]" style="width: 100%"> <thead> <th></th> - <th>Full Name <i class="fa fa-sort"></i></th> - <th>Email <i class="fa fa-sort"></i></th> - <th>Roll Number <i class="fa fa-sort"></i></th> - <th>Institute <i class="fa fa-sort"></i></th> - <th>Department <i class="fa fa-sort"></i></th> + <th>Full Name <i class="fa fa-sort"></i></th> + <th>Email <i class="fa fa-sort"></i></th> + <th>Roll Number <i class="fa fa-sort"></i></th> + <th>Institute <i class="fa fa-sort"></i></th> + <th>Department <i class="fa fa-sort"></i></th> <th>Reject</th> </thead> <tbody> - {% for enroll in enrolled %} + {% for user in enrolled_users %} <tr> <td> {{ forloop.counter }}. - <input type="checkbox" name="check" value="{{ enroll.id }}"> + <input type="checkbox" name="check" value="{{ user.id }}"> </td> - <td> {{ enroll.get_full_name|title }} </td> - <td> {{enroll.email}}</td> - {% with enroll.profile as enroll_profile %} - <td> {{enroll_profile.roll_number}}</td> - <td> {{enroll_profile.institute}}</td> - <td> {{enroll_profile.department}}</td> + <td> {{ user.get_full_name|title }} </td> + <td> {{user.email}}</td> + {% with user.profile as user_profile %} + <td> {{user_profile.roll_number}}</td> + <td> {{user_profile.institute}}</td> + <td> {{user_profile.department}}</td> {% endwith %} <td> - <a class="btn btn-danger" - href="{% url 'yaksh:reject_user' course.id enroll.id %}"> + <a class="btn btn-danger" href="{% url 'yaksh:reject_enrolled_user' course.id user.id %}"> <i class="fa fa-minus-square"></i> Reject </a> @@ -163,10 +160,8 @@ {% endfor %} </tbody> </table> - <button class="btn btn-danger btn-lg" type="submit" name='reject' value='reject'> - <i class="fa fa-minus-square"></i> - Reject Selected - </button> <br> + <input class="btn btn-danger btn-lg" type="submit" name='reject' value='reject selected'> + </input> </form> </div> {% else %} @@ -182,56 +177,53 @@ <div id="accordian-rejected" class="card"> <div class="card-header"> <a class="card-link" data-toggle="collapse" href="#rejected"> - Rejected Students <i class="fa fa-angle-down"></i> + Rejected Students <i class="fa fa-angle-down"></i> </a> </div> </div> <div id="rejected" class="collapse hide" data-parent="#accordion-rejected"> - {% if rejected %} + {% if rejected_users %} <br> - <input type="checkbox" class="enroll"/> - <font size="5">Select all</font> + <input type="checkbox" class="enroll"/> Select all <div id="enroll"> - <form action="{% url 'yaksh:enroll_rejected' course.id %}" method="post"> + <form action="{% url 'yaksh:enroll_rejected_users' course.id %}" method="post"> {% csrf_token %} <table id="rejected_table" class="tablesorter table table-striped table-responsive-sm course-detail" data-sortlist="[1,0]"> <thead> - <th>Full Name <i class="fa fa-sort"></i></th> - <th>Email <i class="fa fa-sort"></i></th> - <th>Roll Number <i class="fa fa-sort"></i></th> - <th>Institute <i class="fa fa-sort"></i></th> - <th>Department <i class="fa fa-sort"></i></th> + <th>Full Name <i class="fa fa-sort"></i></th> + <th>Email <i class="fa fa-sort"></i></th> + <th>Roll Number <i class="fa fa-sort"></i></th> + <th>Institute <i class="fa fa-sort"></i></th> + <th>Department <i class="fa fa-sort"></i></th> <th>Enroll</th> </thead> <tbody> - {% for reject in rejected %} + {% for user in rejected_users %} <tr> <td> {{ forloop.counter }}. - <input type="checkbox" name="check" value="{{ reject.id }}"> + <input type="checkbox" name="check" value="{{ user.id }}"> </td> - <td>{{reject.get_full_name|title}}</td> - <td> {{reject.email}}</td> - {% with reject.profile as reject_profile %} - <td> {{reject_profile.roll_number}}</td> - <td> {{reject_profile.institute}}</td> - <td> {{reject_profile.department}}</td> + <td>{{user.get_full_name|title}}</td> + <td> {{user.email}}</td> + {% with user.profile as user_profile %} + <td> {{user_profile.roll_number}}</td> + <td> {{user_profile.institute}}</td> + <td> {{user_profile.department}}</td> {% endwith %} <td> - <a class="btn btn-success" - href="{% url 'yaksh:enroll_rejected' course.id reject.id %}"> - <i class="fa fa-plus-square"></i> - Enroll </a> + <a class="btn btn-success" href="{% url 'yaksh:enroll_rejected_user' course.id user.id %}"> + <i class="fa fa-plus-square"></i> + Enroll + </a> </td> </tr> {% endfor %} </tbody> </table> <br> - <button class="btn btn-success btn-lg" type="submit" name='enroll' value='enroll'> - <i class="fa fa-plus-square"></i> - Enroll Selected - </button> + <input class="btn btn-success btn-lg" type="submit" name='enroll' value='enroll selected'> + </input> <br> </form> </div> diff --git a/yaksh/test_views.py b/yaksh/test_views.py index 8a5a03a..1418ee0 100644 --- a/yaksh/test_views.py +++ b/yaksh/test_views.py @@ -2635,10 +2635,19 @@ class TestCourseDetail(TestCase): name="Python Course", enrollment="Enroll Request", creator=self.user1 ) + self.user1_othercourse = Course.objects.create( name="Python Course II", enrollment="Enroll Request", creator=self.user1 ) + + self.user1_deactive_course = Course.objects.create( + name="Python Course II", + enrollment="Enroll Request", + creator=self.user1, + end_enroll_time=timezone.now() + ) + self.learning_module = LearningModule.objects.create( name="test module", description="test description module", html_data="test html description module", creator=self.user1, @@ -2977,15 +2986,74 @@ class TestCourseDetail(TestCase): username=self.user1.username, password=self.user1_plaintext_pass ) - response = self.client.post( - reverse('yaksh:enroll_users', - kwargs={'course_id': self.user1_course.id}), - data={'check': self.student1.id} - ) + url = reverse('yaksh:enroll_reject_user', kwargs={ + 'course_id': self.user1_course.id + }) + data = { + 'check': self.student1.id, + 'enroll': 'enroll' + } + response = self.client.post(url, data) enrolled_student = self.user1_course.students.all() self.assertEqual(response.status_code, 302) self.assertSequenceEqual([self.student1], enrolled_student) + def test_student_course_enroll_post_without_enroll_in_request(self): + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + url = reverse('yaksh:enroll_reject_user', kwargs={ + 'course_id': self.user1_course.id + }) + data = { + 'check': self.student1.id, + } + response = self.client.post(url, data) + self.assertEqual(response.status_code, 302) + + def test_student_course_enroll_post_without_enroll_ids(self): + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + url = reverse('yaksh:enroll_reject_user', kwargs={ + 'course_id': self.user1_course.id + }) + data = { + 'enroll': 'enroll' + } + response = self.client.post(url, data) + self.assertEqual(response.status_code, 302) + + def test_student_course_reject_post_without_reject_in_request(self): + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + url = reverse('yaksh:enroll_reject_user', kwargs={ + 'course_id': self.user1_course.id + }) + data = { + 'check': self.student1.id, + } + response = self.client.post(url, data) + self.assertEqual(response.status_code, 302) + + def test_student_course_reject_post_without_reject_ids(self): + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + url = reverse('yaksh:enroll_reject_user', kwargs={ + 'course_id': self.user1_course.id + }) + data = { + 'reject': 'reject' + } + response = self.client.post(url, data) + self.assertEqual(response.status_code, 302) + def test_student_course_reject_get(self): """ Reject student in a course using get request @@ -3011,15 +3079,123 @@ class TestCourseDetail(TestCase): username=self.user1.username, password=self.user1_plaintext_pass ) - response = self.client.post( - reverse('yaksh:reject_users', - kwargs={'course_id': self.user1_course.id}), - data={'check': self.student1.id} - ) + url = reverse('yaksh:enroll_reject_user', kwargs={ + 'course_id': self.user1_course.id + }) + data = { + 'check': self.student1.id, + 'reject': 'reject' + } + response = self.client.post(url, data) enrolled_student = self.user1_course.rejected.all() self.assertEqual(response.status_code, 302) self.assertSequenceEqual([self.student1], enrolled_student) + def test_enroll_user_not_moderator(self): + self.client.login( + username=self.student.username, + password=self.student_plaintext_pass + ) + url = reverse('yaksh:enroll_user', kwargs={ + 'course_id': self.user1_course.id, + 'user_id': self.user1.id + }) + response = self.client.post(url) + self.assertEqual(response.status_code, 404) + + def test_enroll_user_in_expired_course(self): + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + url = reverse('yaksh:enroll_user', kwargs={ + 'course_id': self.user1_deactive_course.id, + 'user_id': self.student.id + }) + response = self.client.post(url) + self.assertEqual(response.status_code, 302) + + def test_enroll_user_where_moderator_is_neither_creator_nor_teacher(self): + self.client.login( + username=self.user2.username, + password=self.user2_plaintext_pass + ) + url = reverse('yaksh:enroll_user', kwargs={ + 'course_id': self.user1_course.id, + 'user_id': self.user1.id + }) + response = self.client.post(url) + self.assertEqual(response.status_code, 404) + + def test_reject_user_not_moderator(self): + self.client.login( + username=self.student.username, + password=self.student_plaintext_pass + ) + url = reverse('yaksh:reject_user', kwargs={ + 'course_id': self.user1_course.id, + 'user_id': self.user1.id + }) + response = self.client.post(url) + self.assertEqual(response.status_code, 404) + + def test_reject_user_where_moderator_is_neither_creator_nor_teacher(self): + self.client.login( + username=self.user2.username, + password=self.user2_plaintext_pass + ) + url = reverse('yaksh:reject_user', kwargs={ + 'course_id': self.user1_course.id, + 'user_id': self.user1.id + }) + response = self.client.post(url) + self.assertEqual(response.status_code, 404) + + def test_enroll_reject_user_not_moderator(self): + self.client.login( + username=self.student.username, + password=self.student_plaintext_pass + ) + url = reverse('yaksh:enroll_reject_user', kwargs={ + 'course_id': self.user1_course.id, + }) + response = self.client.post(url) + self.assertEqual(response.status_code, 404) + + def test_enroll_reject_user_in_deactivated_course(self): + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + url = reverse('yaksh:enroll_reject_user', kwargs={ + 'course_id': self.user1_deactive_course.id, + }) + response = self.client.post(url) + self.assertEqual(response.status_code, 302) + + def test_enroll_reject_user_where_moderator_is_neither_creator_nor_teacher( + self): + self.client.login( + username=self.user2.username, + password=self.user2_plaintext_pass + ) + url = reverse('yaksh:enroll_reject_user', kwargs={ + 'course_id': self.user1_course.id, + }) + response = self.client.post(url) + self.assertEqual(response.status_code, 404) + + def test_get_enroll_reject_user_view(self): + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + url = reverse('yaksh:enroll_reject_user', kwargs={ + 'course_id': self.user1_course.id, + }) + response = self.client.get(url) + self.assertEqual(response.status_code, 302) + def test_toggle_course_status_get(self): self.client.login( username=self.user1.username, @@ -3213,6 +3389,170 @@ class TestCourseDetail(TestCase): self.assertIn("Per Module Progress", data) +class TestCourseStudents(TestCase): + def setUp(self): + self.client = Client() + self.mod_group = Group.objects.create(name='moderator') + + # Create Moderator with profile + self.user1_plaintext_pass = 'demo1' + self.user1 = User.objects.create_user( + username='demo_user1', + password=self.user1_plaintext_pass, + first_name='user1_first_name', + last_name='user1_last_name', + email='demo@test.com' + ) + + Profile.objects.create( + user=self.user1, + roll_number=10, + institute='IIT', + department='Chemical', + position='Moderator', + timezone='UTC', + is_moderator=True + ) + + self.user2_plaintext_pass = 'demo2' + self.user2 = User.objects.create_user( + username='demo_user2', + password=self.user2_plaintext_pass, + first_name='user2_first_name', + last_name='user2_last_name', + email='demo2@test.com' + ) + + Profile.objects.create( + user=self.user2, + roll_number=10, + institute='IIT', + department='Aeronautical', + position='Moderator', + timezone='UTC', + is_moderator=True + ) + + self.student_plaintext_pass = 'demo_student' + self.student = User.objects.create_user( + username='demo_student', + password=self.student_plaintext_pass, + first_name='student_first_name', + last_name='student_last_name', + email='demo_student@test.com' + ) + self.student1_plaintext_pass = 'demo_student' + self.student1 = User.objects.create_user( + username='demo_student1', + password=self.student1_plaintext_pass, + first_name='student1_first_name', + last_name='student1_last_name', + email='demo_student1@test.com' + ) + + self.student2_plaintext_pass = 'demo_student' + self.student2 = User.objects.create_user( + username='demo_student2', + password=self.student2_plaintext_pass, + first_name='student2_first_name', + last_name='student2_last_name', + email='demo_student2@test.com' + ) + + # Add to moderator group + self.mod_group.user_set.add(self.user1) + self.mod_group.user_set.add(self.user2) + + self.user1_course = Course.objects.create( + name="Python Course", + enrollment="Enroll Request", creator=self.user1 + ) + + self.user1_course.enroll(False, self.student) + self.user1_course.reject(False, self.student1) + self.user1_course.request(self.student2) + + def test_enrolled_users(self): + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + url = reverse('yaksh:course_students', kwargs={ + 'course_id': self.user1_course.id + }) + response = self.client.get(url) + enrolled_users = self.user1_course.get_enrolled() + self.assertTrue(enrolled_users.exists()) + + def test_requested_users(self): + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + url = reverse('yaksh:course_students', kwargs={ + 'course_id': self.user1_course.id + }) + response = self.client.get(url) + requested_users = self.user1_course.get_requests() + self.assertTrue(requested_users.exists()) + + def test_rejected_users(self): + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + url = reverse('yaksh:course_students', kwargs={ + 'course_id': self.user1_course.id + }) + response = self.client.get(url) + rejected_users = self.user1_course.get_rejected() + self.assertTrue(rejected_users.exists()) + + def test_course_students_context(self): + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + url = reverse('yaksh:course_students', kwargs={ + 'course_id': self.user1_course.id + }) + response = self.client.get(url) + self.assertTrue('enrolled_users' in response.context) + self.assertTrue('requested_users' in response.context) + self.assertTrue('rejected_users' in response.context) + + def test_course_students_where_moderator_is_neither_creator_nor_teacher( + self): + self.client.login( + username=self.user2.username, + password=self.user2_plaintext_pass + ) + url = reverse('yaksh:course_students', kwargs={ + 'course_id': self.user1_course.id, + }) + response = self.client.get(url) + self.assertEqual(response.status_code, 404) + + def test_course_students_where_user_is_not_moderator(self): + self.client.login( + username=self.student1, + password=self.student1_plaintext_pass + ) + url = reverse('yaksh:course_students', kwargs={ + 'course_id': self.user1_course.id, + }) + response = self.client.get(url) + self.assertEqual(response.status_code, 404) + + def tearDown(self): + self.user1.delete() + self.user2.delete() + self.student.delete() + self.student1.delete() + self.student2.delete() + self.user1_course.delete() + + class TestEnrollRequest(TestCase): def setUp(self): self.client = Client() diff --git a/yaksh/urls.py b/yaksh/urls.py index b530e99..0639b25 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -123,30 +123,38 @@ urlpatterns = [ name='edit_course'), url(r'manage/course_detail/(?P<course_id>\d+)/$', views.course_detail, name='course_detail'), - url(r'manage/enroll/(?P<course_id>\d+)/(?P<user_id>\d+)/$', views.enroll, - name="enroll_user"), + + url(r'manage/enroll/(?P<course_id>\d+)/$', views.enroll_reject_user, + name="enroll_reject_user"), + url(r'manage/enroll/(?P<course_id>\d+)/(?P<user_id>\d+)/$', + views.enroll_user, name="enroll_user"), + url(r'manage/reject/(?P<course_id>\d+)/(?P<user_id>\d+)/$', + views.reject_user, name="reject_user"), + url(r'manage/enrolled/reject/(?P<course_id>\d+)/(?P<user_id>\d+)/$', + views.reject_user, {'was_enrolled': True}, + name="reject_enrolled_user"), + url(r'manage/enrolled/reject/(?P<course_id>\d+)/$', + views.enroll_reject_user, {'was_enrolled': True}, + name="reject_enrolled_users"), url(r'manage/enroll/rejected/(?P<course_id>\d+)/(?P<user_id>\d+)/$', - views.enroll, {'was_rejected': True}, name="enroll_rejected"), + views.enroll_user, {'was_rejected': True}, + name="enroll_rejected_user"), + url(r'manage/enroll/rejected/(?P<course_id>\d+)/$', + views.enroll_reject_user, {'was_rejected': True}, + name="enroll_rejected_users"), + url(r'manage/upload_users/(?P<course_id>\d+)/$', views.upload_users, name="upload_users"), url(r'manage/send_mail/(?P<course_id>\d+)/$', views.send_mail, name="send_mail"), - url(r'manage/reject/(?P<course_id>\d+)/(?P<user_id>\d+)/$', views.reject, - name="reject_user"), - url(r'manage/enrolled/reject/(?P<course_id>\d+)/(?P<user_id>\d+)/$', - views.reject, {'was_enrolled': True}, name="reject_user"), + url(r'manage/toggle_status/(?P<course_id>\d+)/$', views.toggle_course_status, name="toggle_course_status"), url(r'^questions/filter$', views.questions_filter, name="questions_filter"), url(r'^editprofile/$', views.edit_profile, name='edit_profile'), url(r'^viewprofile/$', views.view_profile, name='view_profile'), - url(r'^manage/enroll/(?P<course_id>\d+)/$', views.enroll, - name="enroll_users"), - url(r'manage/enroll/rejected/(?P<course_id>\d+)/$', - views.enroll, {'was_rejected': True}, name="enroll_rejected"), - url(r'manage/enrolled/reject/(?P<course_id>\d+)/$', - views.reject, {'was_enrolled': True}, name="reject_users"), + url(r'^manage/searchteacher/(?P<course_id>\d+)/$', views.search_teacher, name="search_teacher"), url(r'^manage/addteacher/(?P<course_id>\d+)/$', views.add_teacher, diff --git a/yaksh/views.py b/yaksh/views.py index 277196d..e4a9038 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -1116,12 +1116,54 @@ def course_detail(request, course_id): @login_required @email_verified -def enroll(request, course_id, user_id=None, was_rejected=False): +def enroll_user(request, course_id, user_id=None, was_rejected=False): user = request.user if not is_moderator(user): raise Http404('You are not allowed to view this page') - course = get_object_or_404(Course, pk=course_id) + course = get_object_or_404(Course, id=course_id) + if not course.is_active_enrollment(): + msg = ( + 'Enrollment for this course has been closed,' + ' please contact your ' + 'instructor/administrator.' + ) + messages.warning(request, msg) + return redirect('yaksh:course_students', course_id=course_id) + + if not course.is_creator(user) and not course.is_teacher(user): + raise Http404('This course does not belong to you') + + user = User.objects.get(id=user_id) + course.enroll(was_rejected, user) + messages.success(request, 'Enrolled student successfully') + return redirect('yaksh:course_students', course_id=course_id) + + +@login_required +@email_verified +def reject_user(request, course_id, user_id=None, was_enrolled=False): + user = request.user + if not is_moderator(user): + raise Http404('You are not allowed to view this page') + course = get_object_or_404(Course, id=course_id) + if not course.is_creator(user) and not course.is_teacher(user): + raise Http404('This course does not belong to you') + user = User.objects.get(id=user_id) + course.reject(was_enrolled, user) + messages.success(request, "Rejected students successfully") + return redirect('yaksh:course_students', course_id=course_id) + + +@login_required +@email_verified +def enroll_reject_user(request, + course_id, was_enrolled=False, was_rejected=False): + user = request.user + if not is_moderator(user): + raise Http404('You are not allowed to view this page') + course = get_object_or_404(Course, id=course_id) + if not course.is_active_enrollment(): msg = ( 'Enrollment for this course has been closed,' @@ -1129,23 +1171,31 @@ def enroll(request, course_id, user_id=None, was_rejected=False): 'instructor/administrator.' ) messages.warning(request, msg) - return my_redirect(reverse('yaksh:course_students', args=[course_id])) + return redirect('yaksh:course_students', course_id=course_id) if not course.is_creator(user) and not course.is_teacher(user): raise Http404('This course does not belong to you') if request.method == 'POST': - enroll_ids = request.POST.getlist('check') - else: - enroll_ids = [user_id] - if not enroll_ids: - messages.warning(request, "Please select atleast one student") - return my_redirect(reverse('yaksh:course_students', args=[course_id])) - - users = User.objects.filter(id__in=enroll_ids) - course.enroll(was_rejected, *users) - messages.success(request, "Enrolled student(s) successfully") - return my_redirect(reverse('yaksh:course_students', args=[course_id])) + if 'enroll' in request.POST: + enroll_ids = request.POST.getlist('check') + if not enroll_ids: + messages.warning(request, "Please select atleast one student") + return redirect('yaksh:course_students', course_id=course_id) + users = User.objects.filter(id__in=enroll_ids) + course.enroll(was_rejected, *users) + messages.success(request, "Enrolled student(s) successfully") + return redirect('yaksh:course_students', course_id=course_id) + if 'reject' in request.POST: + reject_ids = request.POST.getlist('check') + if not reject_ids: + messages.warning(request, "Please select atleast one student") + return redirect('yaksh:course_students', course_id=course_id) + users = User.objects.filter(id__in=reject_ids) + course.reject(was_enrolled, *users) + messages.success(request, "Rejected students successfully") + return redirect('yaksh:course_students', course_id=course_id) + return redirect('yaksh:course_students', course_id=course_id) @login_required @@ -1181,31 +1231,6 @@ def send_mail(request, course_id, user_id=None): @login_required @email_verified -def reject(request, course_id, user_id=None, was_enrolled=False): - user = request.user - if not is_moderator(user): - raise Http404('You are not allowed to view this page') - - course = get_object_or_404(Course, pk=course_id) - if not course.is_creator(user) and not course.is_teacher(user): - raise Http404('This course does not belong to you') - - if request.method == 'POST': - reject_ids = request.POST.getlist('check') - else: - reject_ids = [user_id] - if not reject_ids: - messages.warning(request, "Please select atleast one student") - return my_redirect(reverse('yaksh:course_students', args=[course_id])) - - users = User.objects.filter(id__in=reject_ids) - course.reject(was_enrolled, *users) - messages.success(request, "Rejected students successfully") - return my_redirect(reverse('yaksh:course_students', args=[course_id])) - - -@login_required -@email_verified def toggle_course_status(request, course_id): user = request.user if not is_moderator(user): @@ -3183,11 +3208,16 @@ def course_students(request, course_id): if not course.is_creator(user) and not course.is_teacher(user): raise Http404("You are not allowed to view {0}".format( course.name)) - enrolled = course.get_enrolled() - requested = course.get_requests() - rejected = course.get_rejected() - context = {"enrolled": enrolled, "requested": requested, "course": course, - "rejected": rejected, "is_students": True} + enrolled_users = course.get_enrolled() + requested_users = course.get_requests() + rejected_users = course.get_rejected() + context = { + "enrolled_users": enrolled_users, + "requested_users": requested_users, + "course": course, + "rejected_users": rejected_users, + "is_students": True + } return my_render_to_response(request, 'yaksh/course_detail.html', context) |