Example #1
0
    def data(self):
        """
        Uses the CourseTeamSerializer to create a serialized course_team object.
        Adds in additional text and pk fields.
        Removes membership relation.

        Returns serialized object with additional search fields.
        """
        # Django Rest Framework v3.1 requires that we pass the request to the serializer
        # so it can construct hyperlinks.  To avoid changing the interface of this object,
        # we retrieve the request from the request cache.
        context = {"request": get_request_or_stub()}

        serialized_course_team = CourseTeamSerializer(self.course_team,
                                                      context=context).data

        # Save the primary key so we can load the full objects easily after we search
        serialized_course_team['pk'] = self.course_team.pk
        # Don't save the membership relations in elasticsearch
        serialized_course_team.pop('membership', None)

        # add generally searchable content
        serialized_course_team['content'] = {'text': self.content_text()}

        return serialized_course_team
Example #2
0
    def data(self):
        """
        Uses the CourseTeamSerializer to create a serialized course_team object.
        Adds in additional text and pk fields.
        Removes membership relation.

        Returns serialized object with additional search fields.
        """
        # Django Rest Framework v3.1 requires that we pass the request to the serializer
        # so it can construct hyperlinks.  To avoid changing the interface of this object,
        # we retrieve the request from the request cache.
        context = {
            "request": get_request_or_stub()
        }

        serialized_course_team = CourseTeamSerializer(self.course_team, context=context).data

        # Save the primary key so we can load the full objects easily after we search
        serialized_course_team['pk'] = self.course_team.pk
        # Don't save the membership relations in elasticsearch
        serialized_course_team.pop('membership', None)

        # add generally searchable content
        serialized_course_team['content'] = {
            'text': self.content_text()
        }

        return serialized_course_team
Example #3
0
def _get_course_module(course_descriptor, user):
    """ Gets course module that takes into account user state and permissions """
    # Adding courseware imports here to insulate other apps (e.g. schedules) to
    # avoid import errors.
    from lms.djangoapps.courseware.model_data import FieldDataCache
    from lms.djangoapps.courseware.module_render import get_module_for_descriptor

    # Fake a request to fool parts of the courseware that want to inspect it.
    request = get_request_or_stub()
    request.user = user

    # Now evil modulestore magic to inflate our descriptor with user state and
    # permissions checks.
    field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
        course_descriptor.id,
        user,
        course_descriptor,
        depth=1,
        read_only=True,
    )
    course_module = get_module_for_descriptor(
        user,
        request,
        course_descriptor,
        field_data_cache,
        course_descriptor.id,
        course=course_descriptor,
    )
    if not course_module:
        raise CourseUpdateDoesNotExist('Course module {} not found'.format(
            course_descriptor.id))
    return course_module
Example #4
0
def _get_course_module(course_descriptor, user):
    # Adding courseware imports here to insulate other apps (e.g. schedules) to
    # avoid import errors.
    from lms.djangoapps.courseware.model_data import FieldDataCache
    from lms.djangoapps.courseware.module_render import get_module_for_descriptor

    # Fake a request to fool parts of the courseware that want to inspect it.
    request = get_request_or_stub()
    request.user = user

    # Now evil modulestore magic to inflate our descriptor with user state and
    # permissions checks.
    field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
        course_descriptor.id,
        user,
        course_descriptor,
        depth=1,
        read_only=True,
    )
    return get_module_for_descriptor(
        user,
        request,
        course_descriptor,
        field_data_cache,
        course_descriptor.id,
        course=course_descriptor,
    )
Example #5
0
 def test_get_request_or_stub(self):
     """
     Outside the context of the request, we should still get a request
     that allows us to build an absolute URI.
     """
     stub = get_request_or_stub()
     expected_url = "http://{site_name}/foobar".format(site_name=settings.SITE_NAME)
     self.assertEqual(stub.build_absolute_uri("foobar"), expected_url)
Example #6
0
 def should_expire_access_token(cls, application):
     set_token_expired = not ENFORCE_JWT_SCOPES.is_enabled()
     jwt_not_requested = get_request_or_stub().POST.get('token_type',
                                                        '').lower() != 'jwt'
     restricted_application = cls.objects.filter(
         application=application).exists()
     return restricted_application and (jwt_not_requested
                                        or set_token_expired)
 def test_get_request_or_stub(self):
     """
     Outside the context of the request, we should still get a request
     that allows us to build an absolute URI.
     """
     stub = get_request_or_stub()
     expected_url = f"http://{settings.SITE_NAME}/foobar"
     assert stub.build_absolute_uri('foobar') == expected_url
 def test_get_request_or_stub(self):
     """
     Outside the context of the request, we should still get a request
     that allows us to build an absolute URI.
     """
     stub = get_request_or_stub()
     expected_url = "http://{site_name}/foobar".format(site_name=settings.SITE_NAME)
     self.assertEqual(stub.build_absolute_uri("foobar"), expected_url)
Example #9
0
def _get_course_module(course_descriptor, user):
    # Fake a request to fool parts of the courseware that want to inspect it.
    request = get_request_or_stub()
    request.user = user

    # Now evil modulestore magic to inflate our descriptor with user state and
    # permissions checks.
    field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
        course_descriptor.id, user, course_descriptor, depth=1, read_only=True,
    )
    return get_module_for_descriptor(
        user, request, course_descriptor, field_data_cache, course_descriptor.id, course=course_descriptor,
    )
def _get_course_module(course_descriptor, user):
    # Fake a request to fool parts of the courseware that want to inspect it.
    request = get_request_or_stub()
    request.user = user

    # Now evil modulestore magic to inflate our descriptor with user state and
    # permissions checks.
    field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
        course_descriptor.id, user, course_descriptor, depth=1, read_only=True,
    )
    return get_module_for_descriptor(
        user, request, course_descriptor, field_data_cache, course_descriptor.id, course=course_descriptor,
    )
Example #11
0
def emulate_http_request(site=None, user=None, middleware_classes=None):
    """
    Generate a fake HTTP request and run selected middleware on it.

    This is used to enable features that assume they are running as part of an HTTP request handler. Many of these
    features retrieve the "current" request from a thread local managed by crum. They will make a call like
    crum.get_current_request() or something similar.

    Since some tasks are kicked off by a management commands (which does not have an HTTP request) and then executed
    in celery workers there is no "current HTTP request". Instead we just populate the global state that is most
    commonly used on request objects.

    Arguments:
        site (Site): The site that this request should emulate. Defaults to None.
        user (User): The user that initiated this fake request. Defaults to None
        middleware_classes (list): A list of classes that implement Django's middleware interface.
            Defaults to [CurrentRequestUserMiddleware, CurrentSiteThemeMiddleware] if None.
    """
    request = get_request_or_stub()
    request.site = site
    request.user = user

    # TODO: define the default middleware_classes in settings.py
    middleware_classes = middleware_classes or [
        CurrentRequestUserMiddleware,
        CurrentSiteThemeMiddleware,
    ]
    middleware_instances = [klass() for klass in middleware_classes]
    response = HttpResponse()

    for middleware in middleware_instances:
        _run_method_if_implemented(middleware, 'process_request', request)

    try:
        yield
    except Exception as exc:
        for middleware in reversed(middleware_instances):
            _run_method_if_implemented(middleware, 'process_exception',
                                       request, exc)
        raise
    else:
        for middleware in reversed(middleware_instances):
            _run_method_if_implemented(middleware, 'process_response', request,
                                       response)
Example #12
0
def emulate_http_request(site=None, user=None, middleware_classes=None):
    """
    Generate a fake HTTP request and run selected middleware on it.

    This is used to enable features that assume they are running as part of an HTTP request handler. Many of these
    features retrieve the "current" request from a thread local managed by crum. They will make a call like
    crum.get_current_request() or something similar.

    Since some tasks are kicked off by a management commands (which does not have an HTTP request) and then executed
    in celery workers there is no "current HTTP request". Instead we just populate the global state that is most
    commonly used on request objects.

    Arguments:
        site (Site): The site that this request should emulate. Defaults to None.
        user (User): The user that initiated this fake request. Defaults to None
        middleware_classes (list): A list of classes that implement Django's middleware interface.
            Defaults to [CurrentRequestUserMiddleware, CurrentSiteThemeMiddleware] if None.
    """
    request = get_request_or_stub()
    request.site = site
    request.user = user

    # TODO: define the default middleware_classes in settings.py
    middleware_classes = middleware_classes or [
        CurrentRequestUserMiddleware,
        CurrentSiteThemeMiddleware,
    ]
    middleware_instances = [klass() for klass in middleware_classes]
    response = HttpResponse()

    for middleware in middleware_instances:
        _run_method_if_implemented(middleware, 'process_request', request)

    try:
        yield
    except Exception as exc:
        for middleware in reversed(middleware_instances):
            _run_method_if_implemented(middleware, 'process_exception', request, exc)
        raise
    else:
        for middleware in reversed(middleware_instances):
            _run_method_if_implemented(middleware, 'process_response', request, response)
Example #13
0
def complete_student_attempt_task(user_identifier: str, content_id: str) -> None:
    """
    Marks all completable children of content_id as complete for the user

    Submits all completable xblocks inside of the content_id block to the
    Completion Service to mark them as complete. One use case of this function is
    for special exams (timed/proctored) where regardless of submission status on
    individual problems, we want to mark the entire exam as complete when the exam
    is finished.

    params:
        user_identifier (str): username or email of a user
        content_id (str): the block key for a piece of content
    """
    err_msg_prefix = (
        'Error occurred while attempting to complete student attempt for user '
        f'{user_identifier} for content_id {content_id}. '
    )
    err_msg = None
    try:
        user = get_user_by_username_or_email(user_identifier)
        block_key = UsageKey.from_string(content_id)
        root_descriptor = modulestore().get_item(block_key)
    except ObjectDoesNotExist:
        err_msg = err_msg_prefix + 'User does not exist!'
    except InvalidKeyError:
        err_msg = err_msg_prefix + 'Invalid content_id!'
    except ItemNotFoundError:
        err_msg = err_msg_prefix + 'Block not found in the modulestore!'
    if err_msg:
        log.error(err_msg)
        return

    # This logic has been copied over from openedx/core/djangoapps/schedules/content_highlights.py
    # in the _get_course_module function.
    # I'm not sure if this is an anti-pattern or not, so if you can avoid re-copying this, please do.
    # We are using it here because we ran into issues with the User service being undefined when we
    # encountered a split_test xblock.

    # Fake a request to fool parts of the courseware that want to inspect it.
    request = get_request_or_stub()
    request.user = user

    # Now evil modulestore magic to inflate our descriptor with user state and
    # permissions checks.
    field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
        root_descriptor.course_id, user, root_descriptor, read_only=True,
    )
    root_module = get_module_for_descriptor(
        user, request, root_descriptor, field_data_cache, root_descriptor.course_id,
    )
    if not root_module:
        err_msg = err_msg_prefix + 'Module unable to be created from descriptor!'
        log.error(err_msg)
        return

    def _submit_completions(block, user):
        """
        Recursively submits the children for completion to the Completion Service
        """
        mode = XBlockCompletionMode.get_mode(block)
        if mode == XBlockCompletionMode.COMPLETABLE:
            block.runtime.publish(block, 'completion', {'completion': 1.0, 'user_id': user.id})
        elif mode == XBlockCompletionMode.AGGREGATOR:
            # I know this looks weird, but at the time of writing at least, there isn't a good
            # single way to get the children assigned for a partcular user. Some blocks define the
            # child descriptors method, but others don't and with blocks like Randomized Content
            # (Library Content), the get_children method returns all children and not just assigned
            # children. So this is our way around situations like that. See also Split Test Module
            # for another use case where user state has to be taken into account via get_child_descriptors
            block_children = ((hasattr(block, 'get_child_descriptors') and block.get_child_descriptors())
                              or (hasattr(block, 'get_children') and block.get_children())
                              or [])
            for child in block_children:
                _submit_completions(child, user)

    _submit_completions(root_module, user)
Example #14
0
 def should_expire_access_token(cls, application):
     jwt_not_requested = get_request_or_stub().POST.get('token_type', '').lower() != 'jwt'
     restricted_application = cls.objects.filter(application=application).exists()
     return restricted_application and jwt_not_requested
Example #15
0
 def should_expire_access_token(cls, application):
     set_token_expired = not ENFORCE_JWT_SCOPES.is_enabled()
     jwt_not_requested = get_request_or_stub().POST.get('token_type', '').lower() != 'jwt'
     restricted_application = cls.objects.filter(application=application).exists()
     return restricted_application and (jwt_not_requested or set_token_expired)
Example #16
0
def export_course_access_report(self,course_id,filename,admin_user_id,obj_id):
    log.info("celeru task runnnnnnnnnnnnnnnn")
    course_report_obj = CourseReportLink.objects.get(id=obj_id)
    course_report_obj.status = "inprogress"
    course_report_obj.save()
    course_key = CourseKey.from_string(course_id)
    course_enrollment_user_ids = CourseEnrollment.objects.filter(course_id=course_key,is_active=True).values('user_id')
    workbook = xlsxwriter.Workbook(settings.MEDIA_ROOT + filename)
    worksheet = workbook.add_worksheet()
    row = 0
    col = 0
    columns_list = ["Email"]
    admin_user = User.objects.get(id=admin_user_id)
    request = get_request_or_stub()
    course_block_tree = get_course_outline_block_tree_custom(request,course_id,admin_user)
    for section in course_block_tree.get("children", list()):
        section_name = section.get("display_name")
        for subsection in section.get("children", list()):
            subsection_name = subsection.get("display_name")
            for block in subsection.get("children", list()):
                columns_list.append(block.get("display_name"))
    for column in columns_list:
        worksheet.write(0, col, column)
        col += 1
    row +=1
    for course_enrollment_user_id in course_enrollment_user_ids:
        user = User.objects.filter(id=course_enrollment_user_id['user_id'])
        if user:
            user = user[0]
            course_block_tree = get_course_outline_block_tree_custom(request,course_id,user)
            unit_completed_list = []
            unit_completed_list.append(user.email)
            for section in course_block_tree.get("children", list()):
                section_name = section.get("display_name")
                for subsection in section.get("children", list()):
                    subsection_name = subsection.get("display_name")
                    for block in subsection.get("children", list()):
                        if  block.get("complete"):
                            unit_completed_list.append("completed")
                        else:
                            unit_completed_list.append("not completed")
            col = 0
            for unit in unit_completed_list:
                worksheet.write(row,col,unit)
                col +=1
            row +=1
    workbook.close()
    with open(settings.MEDIA_ROOT +filename, 'rb') as model_excel:
        access_file = model_excel
        bucket_name = BUCKET_NAME +'/'+ 'course_access_report/'
        conn = boto.connect_s3(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)
        bucket = conn.get_bucket(BUCKET_NAME)
        imagedate = datetime.date.today()
        imagedate = imagedate.strftime('%d_%m_%Y')
        fs = FileSystemStorage()
        filename = fs.save(workbook.filename, access_file)
        uploaded_file_url = fs.url(filename)
        k = Key(bucket)
        course_overview = CourseOverview.objects.get(id=course_key)
        course_name = course_overview.display_name
        k.key = course_name+'_new_'+imagedate+'_'+workbook.filename
        k.set_contents_from_filename(workbook.filename)
        k.make_public()
        imageurl = 'https://s3-ap-southeast-1.amazonaws.com/docmode-org-uploads/'+course_name+'_new_'+imagedate+'_'+workbook.filename
        course_report_obj.status = "success"
        course_report_obj.file_path = imageurl
        course_report_obj.save()
        course_access_obj = CourseAccessReportLink(course_id=course_id,file_path=imageurl,file_name=workbook.filename)
        course_access_obj.save()