def get_context_data(self, **kwargs): courselet_pk = self.kwargs.get('courselet_pk') course = self.get_course() if courselet_pk: courselet = CourseUnit.objects.get(id=courselet_pk) else: courselet = CourseUnit.objects.filter(course=course).first() # TODO: Cover this kwargs['invites'] = Invite.objects.my_invites(request=self.request).filter( enroll_unit_code=EnrollUnitCode.get_code(courselet, give_instance=True, isTest=True)) # pragma: no cover kwargs['invite_tester_form'] = self.form_class( initial={ 'type': 'tester', 'course': self.get_course(), } ) kwargs.update({ 'u_lessons': self.get_units_by_courselet(courselet) }) if waffle.switch_is_active('ctms_invite_students'): # We no longer need a form # kwargs['invite_student_form'] = self.form_class(initial={'type': 'student', 'course': self.get_course()}) if courselet: kwargs['enroll_code'] = EnrollUnitCode.get_code(courselet) kwargs['courselet'] = courselet kwargs['course'] = course kwargs['domain'] = '{0}://{1}'.format(self.request.scheme, Site.objects.get_current().domain) kwargs['courselets_email'] = settings.COURSELETS_EMAIL return kwargs
def get_courselets(self, request, course): # User can see courselets only by getting here from the Shared Courses page # TODO: Cover this invites = Invite.objects.filter(user=self.request.user, course=course, status='joined', type='tester').distinct('enroll_unit_code__courseUnit') # pragma: no cover courselets = [] if invites: for invite in invites: # backward compability for old Invites if not invite.enroll_unit_code: first_courselet = course.courseunit_set.all().first() if not first_courselet: continue invite.enroll_unit_code = EnrollUnitCode.get_code(first_courselet, give_instance=True) invite.save() courselet = ( invite.enroll_unit_code.courseUnit, EnrollUnitCode.get_code(invite.enroll_unit_code.courseUnit, isTest=True), len(invite.enroll_unit_code.courseUnit.unit.get_exercises()), Chat.objects.filter( enroll_code__courseUnit=invite.enroll_unit_code.courseUnit, user=request.user, state__isnull=False, is_live=False, is_test=True, is_preview=False ).first() ) courselets.append(courselet) return courselets raise Http404('Message')
def test_get_redirect_url_with_invite(self): student = User.objects.create_user('student', '*****@*****.**', '123123') instructor_user = User.objects.create_user('instructor', '*****@*****.**', '123123') Instructor.objects.create( user=instructor_user, ) course = Course.objects.create(title='Test title', description='test description', access='Public', enrollCode='111', lockout='222', addedBy=instructor_user) unit = Unit.objects.create(title='Test title', addedBy=instructor_user) courseunit = CourseUnit.objects.create( unit=unit, course=course, order=0, addedBy=instructor_user, releaseTime=timezone.now() ) enroll_unit_code = EnrollUnitCode.get_code(courseunit, give_instance=True) # create invite Invite.create_new(True, course, instructor_user.instructor, student.email, 'student', enroll_unit_code) # simulate student has come in chat but haven't started it yet Role.objects.create( user=student, course=course, role=Role.ENROLLED ) self.assertEqual(get_redirect_url(student), reverse('lms:course_view', kwargs={'course_id': course.id})) # to lms/course from role course_2 = Course.objects.create( title='Test title2', description='test description2', access='Public', enrollCode='1112', lockout='2222', addedBy=instructor_user ) unit_2 = Unit.objects.create(title='Test title 2', addedBy=instructor_user) courseunit_2 = CourseUnit.objects.create( unit=unit_2, course=course_2, order=1, addedBy=instructor_user, releaseTime=timezone.now() ) enroll_unit_code_2 = EnrollUnitCode.get_code(courseunit_2, give_instance=True) Chat.objects.create( user=student, instructor=instructor_user, enroll_code=enroll_unit_code_2 ) self.assertEqual(get_redirect_url(student), reverse('lms:course_view', kwargs={'course_id': course_2.id})) # to lms/course from role
def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') self.course = Course(title='test_title', addedBy=self.user) self.course.save() self.unit = Unit(title='test unit title', addedBy=self.user) self.unit.save() self.course_unit = CourseUnit( course=self.course, unit=self.unit, order=0, addedBy=self.user ) self.course_unit.releaseTime = timezone.now() - datetime.timedelta(days=1) self.course_unit.save() self.enroll = EnrollUnitCode(courseUnit=self.course_unit) self.enroll.save() self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR) self.role.save() self.student_role = Role(course=self.course, user=self.user, role=Role.ENROLLED) self.student_role.save() self.concept = Concept.new_concept('bad', 'idea', self.unit, self.user) self.lesson = Lesson( title='New York Test Lesson', text='brr', addedBy=self.user, kind=Lesson.ORCT_QUESTION ) self.lesson.save_root(self.concept) self.unit_lesson = UnitLesson( unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id, order=0 ) self.unit_lesson.save() self.unit_lesson_answer = UnitLesson( parent=self.unit_lesson, unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id, kind=UnitLesson.ANSWERS ) self.unit_lesson_answer.save() self.user = User.objects.create_user(username='******', password='******') call_command('fsm_deploy')
def setUp(self): super(SharedCoursesListViewTests, self).setUp() self.unit2 = Unit(title='Test title2', addedBy=self.user) self.unit2.save() self.course2 = Course(title='Test title2', description='test description2', access='Public2', enrollCode='1112', lockout='1222', addedBy=self.user) self.course2.save() self.courseunit2 = CourseUnit( unit=self.unit2, course=self.course, order=0, addedBy=self.user, releaseTime=timezone.now() ) self.courseunit2.save() self.courseunit3 = CourseUnit( unit=self.unit, course=self.course2, order=0, addedBy=self.user, releaseTime=timezone.now() ) self.courseunit3.save() enroll_unit_code = EnrollUnitCode.get_code(self.courseunit, give_instance=True) enroll_unit_code2 = EnrollUnitCode.get_code(self.courseunit2, give_instance=True) enroll_unit_code3 = EnrollUnitCode.get_code(self.courseunit3, give_instance=True) self.tester_shared_course = Invite.create_new( invite_type='tester', commit=True, instructor=self.instructor2, email=self.user.email, course=self.course, enroll_unit_code=enroll_unit_code ) self.tester_shared_course2 = Invite.create_new( invite_type='tester', commit=True, instructor=self.instructor2, email=self.user.email, course=self.course, enroll_unit_code=enroll_unit_code2 ) self.tester_shared_course3 = Invite.create_new( invite_type='tester', commit=True, instructor=self.instructor2, email=self.user.email, course=self.course2, enroll_unit_code=enroll_unit_code3 ) self.url = reverse('ctms:shared_courses')
def enroll_unit_code(course_unit, user): enroll = EnrollUnitCode.get_code_for_user_chat( course_unit=course_unit, is_live=False, user=user, ) return enroll
def get_form_kwargs(self): kwargs = super(InvitesListView, self).get_form_kwargs() kwargs['course'] = self.get_course() kwargs['instructor'] = self.request.user.instructor # TODO: Cover this kwargs['enroll_unit_code'] = EnrollUnitCode.get_code(self.kwargs.get('courselet_pk'), give_instance=True, isTest=True) # pragma: no cover return kwargs
def get_context_data(self, **kwargs): context = super(CoursletSettingsView, self).get_context_data(**kwargs) context.update(self.kwargs) courslet = self.get_courslet() context.update({ 'u_lessons': self.get_units_by_courselet(courslet), 'course': self.get_course(), 'courslet': courslet, 'domain': 'https://{0}'.format(Site.objects.get_current().domain), 'enroll_code': EnrollUnitCode.get_code(courslet) }) return context
def test_my_courses_show_shared_courses(self, onboarding_percentage): onboarding_percentage.return_value = 100 self.course.addedBy = self.user2 self.course.save() # create shared course enroll_unit_code = EnrollUnitCode.get_code(self.courseunit, give_instance=True) shared_course = Invite.create_new(True, self.course, self.instructor2, self.user.email, 'tester', enroll_unit_code) response = self.client.get(reverse('ctms:shared_courses')) # should return shared courses self.assertIn('shared_courses', response.context) self.assertNotEqual(len(response.context['shared_courses']), 0) self.assertTrue(shared_course.course.title in response.context['shared_courses'])
def test_my_courses_show_shared_courses(self): self.course.addedBy = self.user2 self.course.save() # create shared course enroll_unit_code = EnrollUnitCode.get_code(self.courseunit, give_instance=True) shared_course = Invite.create_new( True, self.course, self.instructor2, self.user.email, 'tester', enroll_unit_code) response = self.client.get(reverse('ctms:shared_courses')) # should return shared courses self.assertIn('shared_courses', response.context) self.assertNotEqual(len(response.context['shared_courses']), 0) self.assertTrue(shared_course.course.title in response.context['shared_courses'])
def get(self, request, course_pk, pk): course = self.get_course() course_unit = course.courseunit_set.filter(id=pk).first() if course_unit: # create EnrollCode enroll = EnrollUnitCode.get_code_for_user_chat( course_unit=course_unit, is_live=False, user=request.user, ) return redirect('chat:add_units_by_chat', **{'enroll_key': enroll.enrollCode, 'course_id': course.id, 'courselet_id': course_unit.id})
def get(self, request, course_pk, pk): course = self.get_course() course_unit = course.courseunit_set.filter(id=pk).first() if course_unit: # create EnrollCode enroll = EnrollUnitCode.get_code_for_user_chat( course_unit=course_unit, is_live=False, user=request.user, is_preview=True ) return redirect('chat:preview_courselet', **{'enroll_key': enroll.enrollCode})
def test_enroll_code(enroll_unit_code, course_unit, course, unit, chat): # test hex len assert len(enroll_unit_code.enrollCode) == 32 assert EnrollUnitCode.get_code(course_unit) == enroll_unit_code.enrollCode # course unit without EnrollUnitCode new_course_unit = CourseUnit.objects.create(course=course, unit=unit, order=0, addedBy=course_unit.addedBy) assert len(EnrollUnitCode.get_code(new_course_unit)) == 32 # EnrollUnitCode with chat enroll_unit_code = EnrollUnitCode.get_code_for_user_chat( course_unit=chat.enroll_code.courseUnit, user=new_course_unit.addedBy, is_live=True, ) assert len(enroll_unit_code.enrollCode) == 32 # EnrollUnitCode without chat enroll_unit_code = EnrollUnitCode.get_code_for_user_chat( course_unit=course_unit, user=new_course_unit.addedBy, is_live=True, ) assert len(enroll_unit_code.enrollCode) == 32
def get_courselets(self, request, course): return [ { 'courselet': courselet, 'enroll_code': EnrollUnitCode.get_code(courselet), 'execrices': len(courselet.unit.get_exercises()), 'chat': Chat.objects.filter( enroll_code__courseUnit=courselet, user=request.user, state__isnull=False, is_live=False, is_preview=False, is_test=False ).first() } for courselet in course.get_course_units(True) ]
def get_context_data(self, **kwargs): context = super(Onboarding, self).get_context_data(**kwargs) users_course = Course.objects.filter(addedBy=self.request.user).last() users_courselet = CourseUnit.objects.filter( addedBy=self.request.user, course=users_course ).last() users_thread = Lesson.objects.filter(addedBy=self.request.user, kind=Lesson.ANSWER).last() introduction_course_id = get_onboarding_setting(onboarding.INTRODUCTION_COURSE_ID) introduction_courselet_id = get_onboarding_setting(onboarding.INTRODUCTION_COURSELET_ID) course = Course.objects.filter(id=introduction_course_id).first() enroll_unit_code = EnrollUnitCode.objects.filter( courseUnit=introduction_courselet_id, isLive=False, isPreview=False, isTest=False ).first() if not enroll_unit_code: logger.warning('value: ONBOARDING_INTRODUCTION_COURSELET_ID = {} for courselet - not found!'.format( introduction_courselet_id)) enroll_url = '/chat/enrollcode/{}'.format( enroll_unit_code.enrollCode or EnrollUnitCode.get_code(enroll_unit_code.courseUnit) ) if enroll_unit_code else '#' context.update(dict( introduction_course=course, users_course=users_course, users_courselet=users_courselet, users_thread=users_thread, enroll_url=enroll_url )) status = get_onboarding_status_with_settings(self.request.user.id) steps = { key: status.get(key) for key in get_onboarding_steps() } context.update(**steps) return context
def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') self.course = Course(title='test_title', addedBy=self.user) self.course.save() self.unit = Unit(title='test unit title', addedBy=self.user) self.unit.save() self.course_unit = CourseUnit(course=self.course, unit=self.unit, order=0, addedBy=self.user) self.course_unit.save() self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR) self.role.save() self.enroll = EnrollUnitCode.get_code_for_user_chat(self.course_unit, True, self.user) self.history_live_chat = Chat( user=self.user, is_live=True, enroll_code=self.enroll ) self.history_live_chat.save()
class TestCourseletViewHistoryTab(TestCase): def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') self.course = Course(title='test_title', addedBy=self.user) self.course.save() self.unit = Unit(title='test unit title', addedBy=self.user) self.unit.save() self.course_unit = CourseUnit( course=self.course, unit=self.unit, order=0, addedBy=self.user ) self.course_unit.releaseTime = timezone.now() - datetime.timedelta(days=1) self.course_unit.save() self.enroll = EnrollUnitCode(courseUnit=self.course_unit) self.enroll.save() self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR) self.role.save() self.student_role = Role(course=self.course, user=self.user, role=Role.ENROLLED) self.student_role.save() self.concept = Concept.new_concept('bad', 'idea', self.unit, self.user) self.lesson = Lesson( title='New York Test Lesson', text='brr', addedBy=self.user, kind=Lesson.ORCT_QUESTION ) self.lesson.save_root(self.concept) self.unit_lesson = UnitLesson( unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id, order=0 ) self.unit_lesson.save() self.unit_lesson_answer = UnitLesson( parent=self.unit_lesson, unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id, kind=UnitLesson.ANSWERS ) self.unit_lesson_answer.save() self.user = User.objects.create_user(username='******', password='******') call_command('fsm_deploy') def test_click_on_courslet_creates_new_chat(self): # test that there's no history yet response = self.client.get( reverse('lms:course_view', kwargs={'course_id': self.course.id}) ) self.assertEqual(response.status_code, 200) self.assertIsNotNone(list(response.context['courslets'])) self.assertEqual(response.status_code, 200) chats_count_1 = Chat.objects.all().count() # firstly call to chat:init_chat_api function with enroll_key and chat_id=0 response = self.client.get( reverse( 'chat:init_chat_api', kwargs={ 'enroll_key': self.enroll.enrollCode, 'chat_id': 0 } ), HTTP_X_REQUESTED_WITH='XMLHttpRequest' ) json_content = json.loads(response.content) chat_id = json_content['id'] response = self.client.get( reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id}) ) self.assertEqual(response.context['chat'].id, Chat.objects.all().first().id) self.assertEqual(response.status_code, 200) chats_count_2 = Chat.objects.count() self.assertNotEqual(chats_count_2, chats_count_1) response = self.client.get( reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id}) ) chats_count_3 = Chat.objects.count() response = self.client.get( reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id}) ) chats_count_4 = Chat.objects.count() self.assertEqual(response.status_code, 200) self.assertEqual(chats_count_4, chats_count_2) self.assertEqual(chats_count_3, chats_count_2) self.assertEqual(response.context['chat'].id, Chat.objects.all().first().id) chat = Chat.objects.all().first() # get chat and set state to None it means that courslet finished. chat.state = None chat.save() response = self.client.get( reverse('lms:course_view', kwargs={'course_id': self.course.id}) ) self.assertEqual(response.status_code, 200) self.assertEqual(Chat.objects.count(), chats_count_2) self.assertEqual(len(list(response.context['courslets'])), 1) def test_courslet_history(self): enroll_code = EnrollUnitCode.get_code(self.course_unit) response = self.client.get( reverse( 'chat:init_chat_api', kwargs={ 'enroll_key': self.enroll.enrollCode, 'chat_id': 0 } ), HTTP_X_REQUESTED_WITH='XMLHttpRequest' ) json_content = json.loads(response.content) chat_id = json_content['id'] response = self.client.get( reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True ) response = self.client.get( reverse('chat:history'), {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] answer = 'My Answer' response = self.client.put( next_url, data=json.dumps({"text": answer, "chat_id": chat_id}), content_type='application/json', follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] response = self.client.get( next_url, {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] self.assertIsNotNone(json_content['input']['options']) self.assertEquals(len(json_content['addMessages']), 2) # emulate chat finished - set state to None Chat.objects.filter(id=chat_id).update(state=None) response = self.client.get( reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True ) response = self.client.get( reverse('chat:history'), {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) self.assertIsNone(json_content['input']['options']) self.assertEquals(len(json_content['addMessages']), 4)
def test_courslet_history(self): enroll_code = EnrollUnitCode.get_code(self.course_unit) response = self.client.get( reverse( 'chat:init_chat_api', kwargs={ 'enroll_key': self.enroll.enrollCode, 'chat_id': 0 } ), HTTP_X_REQUESTED_WITH='XMLHttpRequest' ) json_content = json.loads(response.content) chat_id = json_content['id'] response = self.client.get( reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True ) response = self.client.get( reverse('chat:history'), {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] answer = 'My Answer' response = self.client.put( next_url, data=json.dumps({"text": answer, "chat_id": chat_id}), content_type='application/json', follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] response = self.client.get( next_url, {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] self.assertIsNotNone(json_content['input']['options']) self.assertEqual(len(json_content['addMessages']), 2) # emulate chat finished - set state to None Chat.objects.filter(id=chat_id).update(state=None) response = self.client.get( reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True ) response = self.client.get( reverse('chat:history'), {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) self.assertIsNone(json_content['input']['options']) self.assertEqual(len(json_content['addMessages']), 4)
def lti_redirect(request, lti_consumer, course_id=None, unit_id=None): """Create user and redirect to Course | Create LTIUser with all needed link to Django user | and/or UserSocialAuth. | Finally login Django user and redirect to Course :param unit_id: unit id from lunch url """ request_dict = request.session['LTI_POST'] context_id = request_dict.get('context_id') course_ref = CourseRef.objects.filter(context_id=context_id).first() user_id = request_dict.get('user_id', None) roles_from_request = request_dict.get('roles', '').split(',') roles = list( set((ROLES_MAP.get(role, Role.ENROLLED) for role in roles_from_request))) if not user_id: return render(request, 'lti/error.html', {'message': 'There is not user_id required LTI param'}) user, created = LTIUser.objects.get_or_create(user_id=user_id, lti_consumer=lti_consumer) extra_data = { k: v for (k, v) in list(request_dict.items()) if k in MOODLE_PARAMS } user.extra_data = json.dumps(extra_data) user.save() if not user.is_linked: user.create_links() user.login(request) # check user timezone and save it if not yet set or set incorrect timezone Profile.check_tz(request) if not course_id or not Course.objects.filter(id=course_id).exists(): if course_ref: course_id = course_ref.course.id elif Role.INSTRUCTOR in roles: return redirect(reverse('lti:create_courseref')) else: return render( request, 'lti/error.html', { 'message': """You are trying to access Course that does not exists but Students can not create new Courses automatically""" }) user.enroll(roles, course_id) if Role.INSTRUCTOR in roles: # NOTE: waffle allow to disable\enable features in the project through admin UI. # Here we enable\disable redirecting user to instructor UI. if waffle.switch_is_active('instructor_UI'): if not unit_id: return redirect(reverse('ctms:course_view', args=(course_id, ))) else: return redirect( reverse('ctms:courslet_view', args=(course_id, unit_id))) else: if not unit_id: return redirect(reverse('ct:course', args=(course_id, ))) else: return redirect( reverse('ct:unit_tasks', args=(course_id, unit_id))) else: course = get_object_or_404(Course, id=course_id) unit = None try: unit = Unit.objects.get(id=unit_id) course_unit = CourseUnit.objects.get(unit=unit, course=course) except Unit.DoesNotExist: # Get first CourseUnit by order if there is no Unit found course_unit = course.courseunit_set.filter( releaseTime__isnull=False, releaseTime__lt=timezone.now()).order_by('order').first() if not unit and not course_unit: return render( request, 'lti/error.html', {'message': 'There are no units to display for that Course.'}) enroll_code = EnrollUnitCode.get_code(course_unit) if not course_unit.unit.unitlesson_set.filter( order__isnull=False).exists(): return render(request, 'lti/error.html', { 'message': 'There are no Lessons to display for that Courselet.' }) params = {} params['course_id'] = course_id params['lis_result_sourcedid'] = request.POST.get( 'lis_result_sourcedid') params['lis_outcome_service_url'] = request.POST.get( 'lis_outcome_service_url') store_outcome_parameters(params, request.user, lti_consumer) if waffle.switch_is_active('chat_ui'): if not unit_id: return redirect( reverse('lms:course_view', kwargs={'course_id': course_id})) else: return redirect( reverse('chat:chat_enroll', kwargs={'enroll_key': enroll_code})) else: if not unit_id: return redirect( reverse('ct:course_student', args=(course_id, ))) else: return redirect( reverse('ct:study_unit', args=(course_id, unit_id)))
class TestCourseletViewHistoryTab(TestCase): def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') self.course = Course(title='test_title', addedBy=self.user) self.course.save() self.unit = Unit(title='test unit title', addedBy=self.user) self.unit.save() self.course_unit = CourseUnit( course=self.course, unit=self.unit, order=0, addedBy=self.user ) self.course_unit.releaseTime = timezone.now() - datetime.timedelta(days=1) self.course_unit.save() self.enroll = EnrollUnitCode(courseUnit=self.course_unit) self.enroll.save() self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR) self.role.save() self.student_role = Role(course=self.course, user=self.user, role=Role.ENROLLED) self.student_role.save() self.concept = Concept.new_concept('bad', 'idea', self.unit, self.user) self.lesson = Lesson( title='New York Test Lesson', text='brr', addedBy=self.user, kind=Lesson.ORCT_QUESTION ) self.lesson.save_root(self.concept) self.unit_lesson = UnitLesson( unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id, order=0 ) self.unit_lesson.save() self.unit_lesson_answer = UnitLesson( parent=self.unit_lesson, unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id, kind=UnitLesson.ANSWERS ) self.unit_lesson_answer.save() self.user = User.objects.create_user(username='******', password='******') call_command('fsm_deploy') def test_click_on_courslet_creates_new_chat(self): # test that there's no history yet response = self.client.get( reverse('lms:course_view', kwargs={'course_id': self.course.id}) ) self.assertEqual(response.status_code, 200) self.assertIsNotNone(list(response.context['courslets'])) self.assertEqual(response.status_code, 200) chats_count_1 = Chat.objects.all().count() # firstly call to chat:init_chat_api function with enroll_key and chat_id=0 response = self.client.get( reverse( 'chat:init_chat_api', kwargs={ 'enroll_key': self.enroll.enrollCode, 'chat_id': 0 } ), HTTP_X_REQUESTED_WITH='XMLHttpRequest' ) json_content = json.loads(response.content) chat_id = json_content['id'] response = self.client.get( reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id}) ) self.assertEqual(response.context['chat'].id, Chat.objects.all().first().id) self.assertEqual(response.status_code, 200) chats_count_2 = Chat.objects.count() self.assertNotEqual(chats_count_2, chats_count_1) response = self.client.get( reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id}) ) chats_count_3 = Chat.objects.count() response = self.client.get( reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id}) ) chats_count_4 = Chat.objects.count() self.assertEqual(response.status_code, 200) self.assertEqual(chats_count_4, chats_count_2) self.assertEqual(chats_count_3, chats_count_2) self.assertEqual(response.context['chat'].id, Chat.objects.all().first().id) chat = Chat.objects.all().first() # get chat and set state to None it means that courslet finished. chat.state = None chat.save() response = self.client.get( reverse('lms:course_view', kwargs={'course_id': self.course.id}) ) self.assertEqual(response.status_code, 200) self.assertEqual(Chat.objects.count(), chats_count_2) self.assertEqual(len(list(response.context['courslets'])), 1) def test_courslet_history(self): enroll_code = EnrollUnitCode.get_code(self.course_unit) response = self.client.get( reverse( 'chat:init_chat_api', kwargs={ 'enroll_key': self.enroll.enrollCode, 'chat_id': 0 } ), HTTP_X_REQUESTED_WITH='XMLHttpRequest' ) json_content = json.loads(response.content) chat_id = json_content['id'] response = self.client.get( reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True ) response = self.client.get( reverse('chat:history'), {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] answer = 'My Answer' response = self.client.put( next_url, data=json.dumps({"text": answer, "chat_id": chat_id}), content_type='application/json', follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] response = self.client.get( next_url, {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] self.assertIsNotNone(json_content['input']['options']) self.assertEqual(len(json_content['addMessages']), 2) # emulate chat finished - set state to None Chat.objects.filter(id=chat_id).update(state=None) response = self.client.get( reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True ) response = self.client.get( reverse('chat:history'), {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) self.assertIsNone(json_content['input']['options']) self.assertEqual(len(json_content['addMessages']), 4)
def lti_redirect(request, course_id=None, unit_id=None): """Create user and redirect to Course | Create LTIUser with all needed link to Django user | and/or UserSocialAuth. | Finally login Django user and redirect to Course :param unit_id: unit id from lunch url """ request_dict = request.session['LTI_POST'] context_id = request_dict.get('context_id') course_ref = CourseRef.objects.filter(context_id=context_id).first() consumer_name = request_dict.get('tool_consumer_info_product_family_code', 'lti') user_id = request_dict.get('user_id', None) roles_from_request = request_dict.get('roles', '').split(',') roles = list( set((ROLES_MAP.get(role, Role.ENROLLED) for role in roles_from_request))) if not user_id: return render(request, 'lti/error.html', {'message': 'There is not user_id required LTI param'}) user, created = LTIUser.objects.get_or_create( user_id=user_id, consumer=consumer_name, context_id=request_dict.get('context_id')) extra_data = { k: v for (k, v) in request_dict.iteritems() if k in MOODLE_PARAMS } user.extra_data = json.dumps(extra_data) user.save() if not user.is_linked: user.create_links() user.login(request) if not course_id or not Course.objects.filter(id=course_id).exists(): if course_ref: course_id = course_ref.course.id elif Role.INSTRUCTOR in roles: return redirect(reverse('lti:create_courseref')) else: return render( request, 'lti/error.html', { 'message': """You are trying to access Course that does not exists but Students can not create new Courses automatically""" }) user.enroll(roles, course_id) if Role.INSTRUCTOR in roles: if not unit_id: return redirect(reverse('ct:course', args=(course_id, ))) else: return redirect(reverse('ct:unit_tasks', args=(course_id, unit_id))) else: course = get_object_or_404(Course, id=course_id) unit = None try: unit = Unit.objects.get(id=unit_id) course_unit = CourseUnit.objects.get(unit=unit, course=course) except Unit.DoesNotExist: # Get first CourseUnit by order if there is no Unit found course_unit = course.courseunit_set.filter( releaseTime__isnull=False, releaseTime__lt=timezone.now()).order_by('order').first() if not unit and not course_unit: return render( request, 'lti/error.html', {'message': 'There are no units to display for that Course.'}) enroll_code = EnrollUnitCode.get_code(course_unit) if not course_unit.unit.unitlesson_set.filter( order__isnull=False).exists(): return render(request, 'lti/error.html', { 'message': 'There are no Lessons to display for that Courselet.' }) if waffle.switch_is_active('chat_ui'): return redirect( reverse('chat:chat_enroll', kwargs={'enroll_key': enroll_code})) else: if not unit_id: return redirect( reverse('ct:course_student', args=(course_id, ))) else: return redirect( reverse('ct:study_unit', args=(course_id, unit_id)))
def test_courslet_history(self): enroll_code = EnrollUnitCode.get_code(self.course_unit) response = self.client.get( reverse( 'chat:init_chat_api', kwargs={ 'enroll_key': self.enroll.enrollCode, 'chat_id': 0 } ), HTTP_X_REQUESTED_WITH='XMLHttpRequest' ) json_content = json.loads(response.content) chat_id = json_content['id'] response = self.client.get( reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True ) response = self.client.get( reverse('chat:history'), {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] answer = 'My Answer' response = self.client.put( next_url, data=json.dumps({"text": answer, "chat_id": chat_id}), content_type='application/json', follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] response = self.client.get( next_url, {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) next_url = json_content['input']['url'] self.assertIsNotNone(json_content['input']['options']) self.assertEquals(len(json_content['addMessages']), 2) # emulate chat finished - set state to None Chat.objects.filter(id=chat_id).update(state=None) response = self.client.get( reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True ) response = self.client.get( reverse('chat:history'), {'chat_id': chat_id}, follow=True ) json_content = json.loads(response.content) self.assertIsNone(json_content['input']['options']) self.assertEquals(len(json_content['addMessages']), 4)
def setUp(self): super(OnboardingTests, self).setUp() self.enroll_unit_code = EnrollUnitCode.get_code(self.courseunit) self.url = reverse('ctms:onboarding')
def test_get_redirect_url_with_invite(self): student = User.objects.create_user('student', '*****@*****.**', '123123') instructor_user = User.objects.create_user('instructor', '*****@*****.**', '123123') Instructor.objects.create(user=instructor_user, ) course = Course.objects.create(title='Test title', description='test description', access='Public', enrollCode='111', lockout='222', addedBy=instructor_user) unit = Unit.objects.create(title='Test title', addedBy=instructor_user) courseunit = CourseUnit.objects.create(unit=unit, course=course, order=0, addedBy=instructor_user, releaseTime=timezone.now()) enroll_unit_code = EnrollUnitCode.get_code(courseunit, give_instance=True) # create invite Invite.create_new(True, course, instructor_user.instructor, student.email, 'student', enroll_unit_code) # simulate student has come in chat but haven't started it yet Role.objects.create(user=student, course=course, role=Role.ENROLLED) self.assertEqual(get_redirect_url(student), reverse('lms:course_view', kwargs={'course_id': course.id })) # to lms/course from role course_2 = Course.objects.create(title='Test title2', description='test description2', access='Public', enrollCode='1112', lockout='2222', addedBy=instructor_user) unit_2 = Unit.objects.create(title='Test title 2', addedBy=instructor_user) courseunit_2 = CourseUnit.objects.create(unit=unit_2, course=course_2, order=1, addedBy=instructor_user, releaseTime=timezone.now()) enroll_unit_code_2 = EnrollUnitCode.get_code(courseunit_2, give_instance=True) Chat.objects.create(user=student, instructor=instructor_user, enroll_code=enroll_unit_code_2) self.assertEqual(get_redirect_url(student), reverse('lms:course_view', kwargs={'course_id': course_2.id })) # to lms/course from role