def test_delete_instructors(self): print('- Test: delete instructors') self.login() job = adminApi.get_job_by_session_slug_job_slug(SESSION, JOB) self.assertEqual( len(job.instructors.all()), 2 ) data1 = { 'instructors': ['15'] } response = self.client.post( reverse('administrators:delete_job_instructors', args=[SESSION, JOB]), data=urlencode(data1, True), content_type=ContentType ) self.assertEqual(response.status_code, 200) content = json.loads(response.content) self.assertEqual(content['status'], 'error') self.assertTrue('An error occurred' in content['message']) data2 = { 'instructors': ['56'] } response = self.client.post( reverse('administrators:delete_job_instructors', args=[SESSION, JOB]), data=urlencode(data2, True), content_type=ContentType ) self.assertEqual(response.status_code, 200) content = json.loads(response.content) self.assertEqual(content['status'], 'success') self.assertTrue('Success' in content['message']) job = adminApi.get_job_by_session_slug_job_slug(SESSION, JOB) self.assertEqual( len(job.instructors.all()), 1 ) data3 = { 'instructors': ['56'] } response = self.client.post( reverse('administrators:delete_job_instructors', args=[SESSION, JOB]), data=urlencode(data3, True), content_type=ContentType ) self.assertEqual(response.status_code, 200) content = json.loads(response.content) self.assertEqual(content['status'], 'error') self.assertTrue('An error occurred' in content['message'])
def test_edit_job(self): print('- Test: edit a job') self.login() job = adminApi.get_job_by_session_slug_job_slug(SESSION, JOB) response = self.client.get( reverse('administrators:edit_job', args=[SESSION, JOB]) + PREPARE_JOB) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['loggedin_user'].username, self.user.username) self.assertEqual(response.context['loggedin_user'].roles, ['Admin']) form = response.context['form'] self.assertFalse(form.is_bound) self.assertEqual(form.instance.id, job.id) data1 = { 'course_overview': 'new course overview', 'description': 'new description', 'note': 'new note', 'assigned_ta_hours': '180.00', 'accumulated_ta_hours': '35.0', 'is_active': False, 'next': '/administrators/Jobs/prepare/?page=2' } response = self.client.post( reverse('administrators:edit_job', args=[SESSION, JOB]) + PREPARE_JOB, data=urlencode(data1, True), content_type=ContentType ) self.assertEqual(response.status_code, 404) data2 = { 'course_overview': 'new course overview', 'description': 'new description', 'note': 'new note', 'assigned_ta_hours': '180.00', 'accumulated_ta_hours': '35.0', 'is_active': False, 'next': reverse('administrators:prepare_jobs') + '?page=2' } response = self.client.post( reverse('administrators:edit_job', args=[SESSION, JOB]) + PREPARE_JOB, data=urlencode(data2, True), content_type=ContentType ) messages = self.messages(response) self.assertTrue('Success' in messages[0]) self.assertEqual(response.status_code, 302) self.assertEquals(response.url, reverse('administrators:prepare_jobs') + '?page=2') self.assertRedirects(response, response.url) updated_job = adminApi.get_job_by_session_slug_job_slug(SESSION, JOB) self.assertEqual(updated_job.course_overview, data2['course_overview']) self.assertEqual(updated_job.description, data2['description']) self.assertEqual(updated_job.note, data2['note']) self.assertEqual(updated_job.assigned_ta_hours, float(data2['assigned_ta_hours'])) self.assertEqual(updated_job.accumulated_ta_hours, float(data2['accumulated_ta_hours'])) self.assertEqual(updated_job.is_active, data2['is_active'])
def test_show_job(self): print('- Test: display a job') self.login() job = adminApi.get_job_by_session_slug_job_slug(SESSION, JOB) response = self.client.get( reverse('administrators:show_job', args=[SESSION, JOB]) + '?next=/administrators/jobs/prepare/?page=2' ) self.assertEqual(response.status_code, 404) response = self.client.get( reverse('administrators:show_job', args=[SESSION, JOB]) + '?nex=/administrators/jobs/prepare/?page=2&p=Prepare%20Jobs' ) self.assertEqual(response.status_code, 404) response = self.client.get( reverse('administrators:show_job', args=[SESSION, JOB]) + '?next=/administrator/jobs/prepare/?page=2&p=Prepare%20Jobs' ) self.assertEqual(response.status_code, 404) response = self.client.get( reverse('administrators:show_job', args=[SESSION, JOB]) + '?next=/administrators/jobs/prepare/?page=2&a=Prepare%20Jobs' ) self.assertEqual(response.status_code, 404) response = self.client.get( reverse('administrators:show_job', args=[SESSION, JOB]) + '?next=/administrators/jobs/prepare/?page=2&p=Prepare%20Job' ) self.assertEqual(response.status_code, 404) response = self.client.get( reverse('administrators:show_job', args=[SESSION, JOB]) + PREPARE_JOB ) self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['loggedin_user'].username, USERS[0]) self.assertEqual(response.context['job'].id, job.id) self.assertEqual(response.context['job'].session.slug, SESSION) self.assertEqual(response.context['job'].course.slug, JOB)
def show_job(request, session_slug, job_slug): ''' Display job details ''' request = userApi.has_user_access(request, Role.INSTRUCTOR) return render( request, 'instructors/jobs/show_job.html', { 'loggedin_user': request.user, 'job': adminApi.get_job_by_session_slug_job_slug(session_slug, job_slug), 'next_first': request.session.get('next_first') })
def edit_job(request, session_slug, job_slug): ''' Update job details of instructors ''' request = userApi.has_user_access(request, Role.INSTRUCTOR) job = adminApi.get_job_by_session_slug_job_slug(session_slug, job_slug) if request.method == 'POST': form = InstructorJobForm(request.POST, instance=job) if form.is_valid(): job = form.save(commit=False) job.updated_at = datetime.now() job.save() if job: messages.success( request, 'Success! {0} {1} - {2} {3} {4}: job details updated'. format(job.session.year, job.session.term.code, job.course.code.name, job.course.number.name, job.course.section.name)) return HttpResponseRedirect(request.POST.get('next_first')) else: messages.error( request, 'An error occurred while updating job details.') else: errors = form.errors.get_json_data() messages.error( request, 'An error occurred. Form is invalid. {0}'.format( userApi.get_error_messages(errors))) return HttpResponseRedirect(request.get_full_path()) return render( request, 'instructors/jobs/edit_job.html', { 'loggedin_user': request.user, 'job': job, 'form': InstructorJobForm(data=None, instance=job), 'jobs': adminApi.get_recent_ten_job_details(job.course, job.session.year), 'next_first': request.session.get('next_first') })
def show_applications(request, session_slug, job_slug): ''' Display applications applied by students ''' request = userApi.has_user_access(request, Role.INSTRUCTOR) request.session['next_second'] = adminApi.build_new_next(request) job = adminApi.get_job_by_session_slug_job_slug(session_slug, job_slug) apps = Application.objects.filter( Q(job__session__slug=session_slug) & Q(job__course__slug=job_slug)) if request.method == 'POST': instructor_preference = request.POST.get('instructor_preference') assigned_hours = request.POST.get('assigned_hours') if adminApi.is_valid_float(assigned_hours) == False: messages.error( request, 'An error occurred. Please check assigned hours. Assign TA Hours must be numerival value only.' ) return HttpResponseRedirect(request.get_full_path()) if adminApi.is_valid_integer(assigned_hours) == False: messages.error( request, 'An error occurred. Please check assigned hours. Assign TA Hours must be non-negative integers.' ) return HttpResponseRedirect(request.get_full_path()) assigned_hours = int(float(assigned_hours)) if assigned_hours < 0: messages.error( request, 'An error occurred. Please check assigned hours. Assign TA Hours must be greater than 0.' ) return HttpResponseRedirect(request.get_full_path()) if instructor_preference == Application.NONE: messages.error( request, 'An error occurred. Please select your preference, then try again.' ) return HttpResponseRedirect(request.get_full_path()) if instructor_preference == Application.NO_PREFERENCE and assigned_hours > 0: messages.error( request, 'An error occurred. Please leave 0 for Assign TA Hours if you would like to select No Preference, then try again.' ) return HttpResponseRedirect(request.get_full_path()) if assigned_hours > int(job.assigned_ta_hours): messages.error( request, 'An error occurred. You cannot assign {0} hours because Total Assigned TA Hours is {1}. then try again.' .format(assigned_hours, int(job.assigned_ta_hours))) return HttpResponseRedirect(request.get_full_path()) if assigned_hours == 0 and instructor_preference != Application.NO_PREFERENCE: messages.error( request, 'An error occurred. Please assign TA hours, then try again.') return HttpResponseRedirect(request.get_full_path()) instructor_app_form = InstructorApplicationForm(request.POST) if instructor_app_form.is_valid(): app_status_form = ApplicationStatusForm(request.POST) if app_status_form.is_valid(): app_id = request.POST.get('application') updated_app = adminApi.update_application_instructor_preference( app_id, instructor_preference) if updated_app: if app_status_form.save(): messages.success( request, 'Success! {0} (CWL: {1}) is selected.'.format( updated_app.applicant.get_full_name(), updated_app.applicant.username)) else: messages.error( request, 'An error occurred while updating an application status.' ) else: messages.error( request, 'An error occurred while updating an instructor_preference.' ) else: errors = app_status_form.errors.get_json_data() messages.error( request, 'An error occurred. Form is invalid. {0}'.format( userApi.get_error_messages(errors))) else: errors = instructor_app_form.errors.get_json_data() messages.error( request, 'An error occurred. Form is invalid. {0}'.format( userApi.get_error_messages(errors))) return HttpResponseRedirect(request.get_full_path()) else: for app in apps: app.selected = None selected = app.applicationstatus_set.filter( assigned=ApplicationStatus.SELECTED) if selected.exists(): app.selected = selected.last() app.applicant = adminApi.get_applicant_status( app.job.session.year, app.job.session.term.code, app.applicant) return render( request, 'instructors/jobs/show_applications.html', { 'loggedin_user': request.user, 'job': job, 'apps': apps, 'full_job_name': job.course.code.name + '_' + job.course.number.name + '_' + job.course.section.name, 'instructor_preference_choices': Application.INSTRUCTOR_PREFERENCE_CHOICES, 'app_status': APP_STATUS, 'next_first': request.session.get('next_first', None) })
def test_show_applications(self): print('- Display applications applied by students') self.login() job = adminApi.get_job_by_session_slug_job_slug(SESSION, JOB) session = self.client.session session['next_first'] = reverse('instructors:show_jobs') + '?page=1' session.save() response = self.client.get( reverse('instructors:show_applications', args=[SESSION, JOB])) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['loggedin_user'].username, USER) self.assertEqual(response.context['loggedin_user'].roles, ['Instructor']) self.assertEqual(response.context['job'].course.slug, JOB) self.assertEqual(len(response.context['job'].application_set.all()), 4) self.assertEqual( response.context['job'].application_set.first(). instructor_preference, '0') self.assertEqual( len(response.context['instructor_preference_choices']), 5) self.assertEqual(response.context['full_job_name'], 'APBI_200_002') self.assertEqual( response.context['app_status'], { 'none': '0', 'applied': '0', 'selected': '1', 'offered': '2', 'accepted': '3', 'declined': '4', 'cancelled': '5' }) self.assertEqual(response.context['next_first'], '/instructors/jobs/?page=1') applications = [{ 'id': 6, 'accepted_apps': ['APBI 260 001 (45.0 hours)'] }, { 'id': 11, 'accepted_apps': ['APBI 200 002 (65.0 hours)'] }, { 'id': 16, 'accepted_apps': [] }, { 'id': 21, 'accepted_apps': ['APBI 200 001 (30.0 hours)', 'APBI 260 001 (70.0 hours)'] }] c = 0 for app in response.context['apps']: self.assertEqual(app.id, applications[c]['id']) if len(app.applicant.accepted_apps) > 0: d = 0 for accepted_app in app.applicant.accepted_apps: self.assertEqual( accepted_app.job.course.code.name + ' ' + accepted_app.job.course.number.name + ' ' + accepted_app.job.course.section.name + ' (' + str(accepted_app.accepted.assigned_hours) + ' hours)', applications[c]['accepted_apps'][d]) d += 1 c += 1