Пример #1
0
 def test_get_own_targets_as_user(self):
     """
     """
     self.assertTrue(
         len(TOD_Implementation.get_own_targets_as_user(self.ltiprofile1)) == 2
     )  # noqa
     self.assertTrue(
         len(TOD_Implementation.get_own_targets_as_user(self.ltiprofile2)) == 0
     )  # noqa
Пример #2
0
 def test_get_own_targets_from_course(self):
     """
     """
     self.assertTrue(
         len(TOD_Implementation.get_own_targets_from_course("fake_course")) == 2
     )  # noqa
     self.assertRaises(
         ObjectDoesNotExist,
         TOD_Implementation.get_own_targets_from_course,
         "fake_course2",
     )  # noqa
     course2 = LTICourse.create_course("fake_course2", self.ltiprofile1)
     self.assertTrue(
         len(TOD_Implementation.get_own_targets_from_course("fake_course2")) == 0
     )  # noqa
Пример #3
0
    def setUp(self):
        """
        """
        self.sampleImplementation = TOD_Implementation()
        self.user1, self.ltiprofile1 = self.createFakeUser(
            "fakeusername1", "fakeuserid1"
        )
        self.user2, self.ltiprofile2 = self.createFakeUser(
            "fakeusername2", "fakeuserid2"
        )

        self.samplecourse = LTICourse.create_course("fake_course", self.ltiprofile1)
        self.samplecourse.course_name = "Fake Course"
        self.samplecourse.save()
        self.target1 = TargetObject(
            target_title="TObj1",
            target_author="Test Author",
            target_content="Fake Content",
            target_citation="Fake Citation",
            target_type="tx",
        )
        self.target1.save()
        self.target1.target_courses.add(self.samplecourse)
        self.target2 = TargetObject(
            target_title="TObj2",
            target_author="Test Author",
            target_content="Fake Content2",
            target_citation="Fake Citation2",
            target_type="tx",
        )
        self.target2.save()
        self.target2.target_courses.add(self.samplecourse)
Пример #4
0
 def test_get_dict_of_files_from_courses(self):
     """
     """
     dict_of_targets = TOD_Implementation.get_dict_of_files_from_courses(
         LTICourse.get_all_courses()
     )
     self.assertIsInstance(dict_of_targets, dict)
     self.assertTrue(len(dict_of_targets["Fake Course"]) == 2)
     self.assertTrue(self.target1 in dict_of_targets["Fake Course"])
     self.assertTrue(self.target2 in dict_of_targets["Fake Course"])
Пример #5
0
def course_admin_hub(request):
    """
    The index view for both students and instructors. Without the 'is_instructor' flag,
    students are directed to a version of admin_hub with reduced privileges
    """
    is_instructor = request.LTI["is_staff"]
    courses_for_user = LTICourse.objects.filter(
        course_id=request.LTI["hx_context_id"])
    files_in_courses = TOD_Implementation.get_dict_of_files_from_courses(
        courses_for_user)

    logger.debug("course_admin_hub view")
    try:
        config = LTIResourceLinkConfig.objects.get(
            resource_link_id=request.LTI["resource_link_id"])
        collection_id = config.assignment_target.assignment.assignment_id
        object_id = config.assignment_target.target_object_id
        to = TargetObject.objects.get(pk=object_id)
        starter_object = to.target_title

    except:
        object_id = None
        collection_id = None
        to = None
        starter_object = None

    logger.debug(
        "resource_link_config object_id=%s collection_id=%s target_object=%s" %
        (object_id, collection_id, to))

    debug = files_in_courses
    return render(
        request,
        "hx_lti_initializer/admin_hub.html",
        {
            "username": request.LTI["hx_user_name"],
            "is_instructor": request.LTI["is_staff"],
            "courses": courses_for_user,
            "files": files_in_courses,
            "org": settings.ORGANIZATION,
            "debug": debug,
            "starter_object": starter_object,
            "starter_object_id": object_id,
            "starter_collection_id": collection_id,
        },
    )
Пример #6
0
def course_admin_hub(request):
    """
    The index view for both students and instructors. Without the 'is_instructor' flag,
    students are directed to a version of admin_hub with reduced privileges
    """
    is_instructor = request.LTI['is_staff']
    courses_for_user = LTICourse.objects.filter(
        course_id=request.LTI['hx_context_id'])
    files_in_courses = TOD_Implementation.get_dict_of_files_from_courses(
        courses_for_user)

    logger.debug("course_admin_hub view")
    try:
        config = LTIResourceLinkConfig.objects.get(
            resource_link_id=request.LTI['resource_link_id'])
        object_id = int(config.object_id)
        collection_id = config.collection_id
        to = TargetObject.objects.get(pk=object_id)
        starter_object = to.target_title

    except:
        object_id = None
        collection_id = None
        to = None
        starter_object = None

    logger.debug(
        "resource_link_config object_id=%s collection_id=%s target_object=%s" %
        (object_id, collection_id, to))

    debug = files_in_courses
    return render(
        request, 'hx_lti_initializer/admin_hub.html', {
            'username': request.LTI['hx_user_name'],
            'is_instructor': request.LTI['is_staff'],
            'courses': courses_for_user,
            'files': files_in_courses,
            'org': settings.ORGANIZATION,
            'debug': debug,
            'starter_object': starter_object,
            'starter_object_id': object_id,
            'starter_collection_id': collection_id,
        })
Пример #7
0
def course_admin_hub(request):
    """
    """
    lti_profile = LTIProfile.objects.get(user=request.user)
    courses_for_user = LTICourse.objects.filter(course_admins=lti_profile.id)
    files_in_courses = TOD_Implementation.get_dict_of_files_from_courses(lti_profile, courses_for_user)

    return render(
        request,
        "hx_lti_initializer/testpage2.html",
        {
            "user": request.user,
            "email": request.user.email,
            "user_id": request.user.get_username(),
            "roles": lti_profile.roles,
            "courses": courses_for_user,
            "files": files_in_courses,
        },
    )
Пример #8
0
def launch_lti(request):
    """
    Gets a request from an LTI consumer.
    Passes along information to render a welcome screen to the user.
    """
    
    validate_request(request)
    tool_provider = initialize_lti_tool_provider(request)
    
    # collect anonymous_id and consumer key in order to fetch LTIProfile
    # if it exists, we initialize the tool otherwise, we create a new user
    consumer_key_requested = request.POST['oauth_consumer_key']
    anon_id = '%s:%s' % (consumer_key_requested, get_lti_value('user_id', tool_provider))
    debug_printer('DEBUG - Found anonymous ID in request: %s' % anon_id)
    
    course = get_lti_value(settings.LTI_COURSE_ID, tool_provider)
    collection = get_lti_value(settings.LTI_COLLECTION_ID, tool_provider)
    object = get_lti_value(settings.LTI_OBJECT_ID, tool_provider)
    
    debug_printer('DEBUG - Found course being accessed: %s' % course)
    
    roles = get_lti_value(settings.LTI_ROLES, tool_provider)
    if "Student" in roles:
        targ_obj = TargetObject.objects.get(pk=object)
        return render(request, '%s/detail.html' % targ_obj.target_type, {
            'email': get_lti_value('lis_person_contact_email_primary', tool_provider),
            'user_id': get_lti_value('lis_person_sourcedid', tool_provider),
            'roles': roles,
            'collection': collection,
            'course': course,
            'object': object,
            'target_object': targ_obj,
            'token': retrieve_token(get_lti_value('lis_person_contact_email_primary', tool_provider), ''),
        })
    
    try:
        # try to get the profile via the anon id
        lti_profile = LTIProfile.objects.get(anon_id=anon_id)
        debug_printer('DEBUG - LTI Profile was found via anonymous id.')
    
    except LTIProfile.DoesNotExist:
        debug_printer('DEBUG - LTI Profile not found. New User to be created.')
        
        # gather the necessary data from the LTI initialization request
        email = get_lti_value('lis_person_contact_email_primary', tool_provider)
        lti_username = get_lti_value('lis_person_sourcedid', tool_provider)
        
        # checks to see if email and username were not passed in
        # cannot create a user without them
        if not email or not lti_username:
            debug_printer('DEBUG - Email and/or user_id not found in post.')
            raise PermissionDenied()
        
        # checks to see if roles were passed in. Defaults to student role.
        if settings.ALL_ROLES:
            all_user_roles = []
            
            if not roles:
                debug_printer('DEBUG - ALL_ROLES is set but user was not passed in any roles via the request. Defaults to student.')
                all_user_roles += "Student"
            
            else:
                # makes sure that roles is a list and not just a string
                if not isinstance(roles, list):
                    roles = [roles]
                all_user_roles += roles
            
            # checks to make sure that role is actually allowed/expected
            role_allowed = False
            for role_type in settings.ALL_ROLES:
                for user_role in roles:
                    if role_type.lower() == user_role.lower():
                        role_allowed = True
            
            # if role is not allowed then denied (problem with platform)
            # if role is missing, default to Student (problem with request)
            if not role_allowed:
                debug_printer('DEBUG - User does not have an acceptable role. Check with platform.')
                raise PermissionedDenied()
            else:
                debug_printer('DEBUG - User had an acceptable role: %s' % all_user_roles)
        
        user, lti_profile = create_new_user(lti_username, email, roles)
    
    # now it's time to deal with the course_id it does not associate
    # with users as they can flow in and out in a MOOC
    try:
        debug_printer('DEBUG - Course was found %s' % course)
        course_object = LTICourse.get_course_by_id(course)
    
    except LTICourse.DoesNotExist:
        # this should only happen if an instructor is trying to access the 
        # tool from a different course
        debug_printer('DEBUG - Course %s was NOT found. Will be created.' %course)
        message_error = "Sorry, the course you are trying to reach does not exist."
        messages.error(request, message_error)
        if 'Administrator' in roles:
            # if the user is an administrator, the missing course is created
            # otherwise, it will just display an error message
            message_error = "Because you are an instructor, a course has been created for you, edit it below to add a proper name."
            messages.warning(request, message_error)
            course_object = LTICourse.create_course(course, lti_profile)
    
    # get all the courses the user is a part of
    courses_for_user = LTICourse.get_courses_of_user(lti_profile, course_object)
    
    # then gets all the files associated with the courses 
    files_in_courses = TOD_Implementation.get_dict_of_files_from_courses(lti_profile, courses_for_user)
    
    # logs the user in
    lti_profile.user.backend = 'django.contrib.auth.backends.ModelBackend'
    login(request, lti_profile.user)
    
    # then renders the page using the template
    return render(request, 'hx_lti_initializer/testpage2.html', {'user': lti_profile.user, 'email': lti_profile.user.email, 'user_id': lti_profile.user.get_username(), 'roles': lti_profile.roles, 'courses': courses_for_user, 'files': files_in_courses})
Пример #9
0
def launch_lti(request):
    """
    Gets a request from an LTI consumer.
    Passes along information to render a welcome screen to the user.
    """

    validate_request(request)
    tool_provider = initialize_lti_tool_provider(request)

    # collect anonymous_id and consumer key in order to fetch LTIProfile
    # if it exists, we initialize the tool otherwise, we create a new user
    consumer_key_requested = request.POST['oauth_consumer_key']
    anon_id = '%s:%s' % (consumer_key_requested,
                         get_lti_value('user_id', tool_provider))
    debug_printer('DEBUG - Found anonymous ID in request: %s' % anon_id)

    course = get_lti_value(settings.LTI_COURSE_ID, tool_provider)
    collection = get_lti_value(settings.LTI_COLLECTION_ID, tool_provider)
    object = get_lti_value(settings.LTI_OBJECT_ID, tool_provider)

    debug_printer('DEBUG - Found course being accessed: %s' % course)

    roles = get_lti_value(settings.LTI_ROLES, tool_provider)
    if "Student" in roles:
        targ_obj = TargetObject.objects.get(pk=object)
        return render(
            request, '%s/detail.html' % targ_obj.target_type, {
                'email':
                get_lti_value('lis_person_contact_email_primary',
                              tool_provider),
                'user_id':
                get_lti_value('lis_person_sourcedid', tool_provider),
                'roles':
                roles,
                'collection':
                collection,
                'course':
                course,
                'object':
                object,
                'target_object':
                targ_obj,
                'token':
                retrieve_token(
                    get_lti_value('lis_person_contact_email_primary',
                                  tool_provider), ''),
            })

    try:
        # try to get the profile via the anon id
        lti_profile = LTIProfile.objects.get(anon_id=anon_id)
        debug_printer('DEBUG - LTI Profile was found via anonymous id.')

    except LTIProfile.DoesNotExist:
        debug_printer('DEBUG - LTI Profile not found. New User to be created.')

        # gather the necessary data from the LTI initialization request
        email = get_lti_value('lis_person_contact_email_primary',
                              tool_provider)
        lti_username = get_lti_value('lis_person_sourcedid', tool_provider)

        # checks to see if email and username were not passed in
        # cannot create a user without them
        if not email or not lti_username:
            debug_printer('DEBUG - Email and/or user_id not found in post.')
            raise PermissionDenied()

        # checks to see if roles were passed in. Defaults to student role.
        if settings.ALL_ROLES:
            all_user_roles = []

            if not roles:
                debug_printer(
                    'DEBUG - ALL_ROLES is set but user was not passed in any roles via the request. Defaults to student.'
                )
                all_user_roles += "Student"

            else:
                # makes sure that roles is a list and not just a string
                if not isinstance(roles, list):
                    roles = [roles]
                all_user_roles += roles

            # checks to make sure that role is actually allowed/expected
            role_allowed = False
            for role_type in settings.ALL_ROLES:
                for user_role in roles:
                    if role_type.lower() == user_role.lower():
                        role_allowed = True

            # if role is not allowed then denied (problem with platform)
            # if role is missing, default to Student (problem with request)
            if not role_allowed:
                debug_printer(
                    'DEBUG - User does not have an acceptable role. Check with platform.'
                )
                raise PermissionedDenied()
            else:
                debug_printer('DEBUG - User had an acceptable role: %s' %
                              all_user_roles)

        user, lti_profile = create_new_user(lti_username, email, roles)

    # now it's time to deal with the course_id it does not associate
    # with users as they can flow in and out in a MOOC
    try:
        debug_printer('DEBUG - Course was found %s' % course)
        course_object = LTICourse.get_course_by_id(course)

    except LTICourse.DoesNotExist:
        # this should only happen if an instructor is trying to access the
        # tool from a different course
        debug_printer('DEBUG - Course %s was NOT found. Will be created.' %
                      course)
        message_error = "Sorry, the course you are trying to reach does not exist."
        messages.error(request, message_error)
        if 'Administrator' in roles:
            # if the user is an administrator, the missing course is created
            # otherwise, it will just display an error message
            message_error = "Because you are an instructor, a course has been created for you, edit it below to add a proper name."
            messages.warning(request, message_error)
            course_object = LTICourse.create_course(course, lti_profile)

    # get all the courses the user is a part of
    courses_for_user = LTICourse.get_courses_of_user(lti_profile,
                                                     course_object)

    # then gets all the files associated with the courses
    files_in_courses = TOD_Implementation.get_dict_of_files_from_courses(
        lti_profile, courses_for_user)

    # logs the user in
    lti_profile.user.backend = 'django.contrib.auth.backends.ModelBackend'
    login(request, lti_profile.user)

    # then renders the page using the template
    return render(
        request, 'hx_lti_initializer/testpage2.html', {
            'user': lti_profile.user,
            'email': lti_profile.user.email,
            'user_id': lti_profile.user.get_username(),
            'roles': lti_profile.roles,
            'courses': courses_for_user,
            'files': files_in_courses
        })