Пример #1
0
def course_info_update_handler(request,
                               tag=None,
                               package_id=None,
                               branch=None,
                               version_guid=None,
                               block=None,
                               provided_id=None):
    """
    restful CRUD operations on course_info updates.
    provided_id should be none if it's new (create) and index otherwise.
    GET
        json: return the course info update models
    POST
        json: create an update
    PUT or DELETE
        json: change an existing update
    """
    if 'application/json' not in request.META.get('HTTP_ACCEPT',
                                                  'application/json'):
        return HttpResponseBadRequest("Only supports json requests")

    course_location = loc_mapper().translate_locator_to_location(
        CourseLocator(package_id=package_id), get_course=True)
    updates_location = course_location.replace(category='course_info',
                                               name=block)
    if provided_id == '':
        provided_id = None

    # check that logged in user has permissions to this item (GET shouldn't require this level?)
    if not has_course_access(request.user, updates_location):
        raise PermissionDenied()

    if request.method == 'GET':
        course_updates = get_course_updates(updates_location, provided_id)
        if isinstance(course_updates, dict) and course_updates.get('error'):
            return JsonResponse(
                get_course_updates(updates_location, provided_id),
                course_updates.get('status', 400))
        else:
            return JsonResponse(
                get_course_updates(updates_location, provided_id))
    elif request.method == 'DELETE':
        try:
            return JsonResponse(
                delete_course_update(updates_location, request.json,
                                     provided_id, request.user))
        except:
            return HttpResponseBadRequest("Failed to delete",
                                          content_type="text/plain")
    # can be either and sometimes django is rewriting one to the other:
    elif request.method in ('POST', 'PUT'):
        try:
            return JsonResponse(
                update_course_updates(updates_location, request.json,
                                      provided_id, request.user))
        except:
            return HttpResponseBadRequest("Failed to save",
                                          content_type="text/plain")
Пример #2
0
def course_info_update_handler(request, tag=None, package_id=None, branch=None, version_guid=None, block=None,
                               provided_id=None):
    """
    restful CRUD operations on course_info updates.
    provided_id should be none if it's new (create) and index otherwise.
    GET
        json: return the course info update models
    POST
        json: create an update
    PUT or DELETE
        json: change an existing update
    """
    if 'application/json' not in request.META.get('HTTP_ACCEPT', 'application/json'):
        return HttpResponseBadRequest("Only supports json requests")

    course_location = loc_mapper().translate_locator_to_location(
        CourseLocator(package_id=package_id), get_course=True
    )
    updates_location = course_location.replace(category='course_info', name=block)
    print course_location
    print updates_location
    if provided_id == '':
        provided_id = None

    # check that logged in user has permissions to this item (GET shouldn't require this level?)
    if not has_course_access(request.user, updates_location):
        raise PermissionDenied()

    if request.method == 'GET':
        course_updates = get_course_updates(updates_location, provided_id)
        if isinstance(course_updates, dict) and course_updates.get('error'):
            return JsonResponse(get_course_updates(updates_location, provided_id), course_updates.get('status', 400))
        else:
            return JsonResponse(get_course_updates(updates_location, provided_id))
    elif request.method == 'DELETE':
        try:
            return JsonResponse(delete_course_update(updates_location, request.json, provided_id, request.user))
        except:
            return HttpResponseBadRequest(
                "Failed to delete",
                content_type="text/plain"
            )
    # can be either and sometimes django is rewriting one to the other:
    elif request.method in ('POST', 'PUT'):
        notice_course_update_to_student(request.json,course_location, package_id)
        try:
            return JsonResponse(update_course_updates(updates_location, request.json, provided_id, request.user))
        except:
            return HttpResponseBadRequest(
                "Failed to save",
                content_type="text/plain"
            )
Пример #3
0
def course_info(request, org, course, name, provided_id=None):
    """
    Send models and views as well as html for editing the course info to the client.

    org, course, name: Attributes of the Location for the item to edit
    """
    location = get_location_and_verify_access(request, org, course, name)

    course_module = modulestore().get_item(location)

    # get current updates
    location = Location(['i4x', org, course, 'course_info', "updates"])

    return render_to_response(
        'course_info.html', {
            'active_tab':
            'courseinfo-tab',
            'context_course':
            course_module,
            'url_base':
            "/" + org + "/" + course + "/",
            'course_updates':
            json.dumps(get_course_updates(location)),
            'handouts_location':
            Location(['i4x', org, course, 'course_info', 'handouts']).url()
        })
Пример #4
0
def course_info_updates(request, org, course, provided_id=None):
    """
    restful CRUD operations on course_info updates.

    org, course: Attributes of the Location for the item to edit
    provided_id should be none if it's new (create) and a composite of the update db id + index otherwise.
    """
    # ??? No way to check for access permission afaik
    # get current updates
    location = ['i4x', org, course, 'course_info', "updates"]

    # Hmmm, provided_id is coming as empty string on create whereas I believe it used to be None :-(
    # Possibly due to my removing the seemingly redundant pattern in urls.py
    if provided_id == '':
        provided_id = None

    # check that logged in user has permissions to this item
    if not has_access(request.user, location):
        raise PermissionDenied()

    if request.method == 'GET':
        return JsonResponse(get_course_updates(location))
    elif request.method == 'DELETE':
        try:
            return JsonResponse(delete_course_update(location, request.POST, provided_id))
        except:
            return HttpResponseBadRequest("Failed to delete",
                                          content_type="text/plain")
    elif request.method == 'POST':
        try:
            return JsonResponse(update_course_updates(location, request.POST, provided_id))
        except:
            return HttpResponseBadRequest("Failed to save",
                                          content_type="text/plain")
Пример #5
0
 def get_course_info_by_action(location, action):
     """
     return data depend on action
     """
     if action == "updates":
         return get_course_updates(location, None)
     elif action == "handouts":
         module = get_modulestore(location).get_item(location)
         return {"data": getattr(module, 'data', '')}
     else:
         return {"error": 20000, "errmsg": "some error occur!"}
Пример #6
0
 def get_course_info_by_action(location, action):
     """
     return data depend on action
     """
     if action == "updates":
         return get_course_updates(location, None)
     elif action == "handouts":
         module = get_modulestore(location).get_item(location)
         return {"data": getattr(module, 'data', '')}
     else:
         return {"error": 20000, "errmsg": "some error occur!"}
Пример #7
0
def mobi_notice(request):
    if "application/json" not in request.META.get('HTTP_ACCEPT', 'application/json'):
        return HttpResponseBadRequest("Only support json requests")

    if "course_id" not in request.POST:
        return JsonResponse({"errcode": 20000, "errmsg": "params error"})
    try:
        course_locator = BlockUsageLocator(package_id=request.POST["course_id"], branch='draft', version_guid=None, block_id='updates')
        location = loc_mapper().translate_locator_to_location(course_locator)
        return JsonResponse(get_course_updates(location, None))
    except:
        return HttpResponseBadRequest("Fail to get data")
Пример #8
0
def course_info_update_handler(request, course_key_string, provided_id=None):
    """
    restful CRUD operations on course_info updates.
    provided_id should be none if it's new (create) and index otherwise.
    GET
        json: return the course info update models
    POST
        json: create an update
    PUT or DELETE
        json: change an existing update
    """
    if 'application/json' not in request.META.get('HTTP_ACCEPT',
                                                  'application/json'):
        return HttpResponseBadRequest("Only supports json requests")

    course_key = CourseKey.from_string(course_key_string)
    usage_key = course_key.make_usage_key('course_info', 'updates')
    if provided_id == '':
        provided_id = None

    # check that logged in user has permissions to this item (GET shouldn't require this level?)
    if not has_course_access(request.user, usage_key.course_key):
        raise PermissionDenied()

    if request.method == 'GET':
        course_updates = get_course_updates(usage_key, provided_id,
                                            request.user.id)
        if isinstance(course_updates, dict) and course_updates.get('error'):
            return JsonResponse(course_updates,
                                course_updates.get('status', 400))
        else:
            return JsonResponse(course_updates)
    elif request.method == 'DELETE':
        try:
            return JsonResponse(
                delete_course_update(usage_key, request.json, provided_id,
                                     request.user))
        except:
            return HttpResponseBadRequest("Failed to delete",
                                          content_type="text/plain")
    # can be either and sometimes django is rewriting one to the other:
    elif request.method in ('POST', 'PUT'):
        try:
            return JsonResponse(
                update_course_updates(usage_key, request.json, provided_id,
                                      request.user))
        except:
            return HttpResponseBadRequest("Failed to save",
                                          content_type="text/plain")
Пример #9
0
def course_info_update_handler(request, course_key_string, provided_id=None):
    """
    restful CRUD operations on course_info updates.
    provided_id should be none if it's new (create) and index otherwise.
    GET
        json: return the course info update models
    POST
        json: create an update
    PUT or DELETE
        json: change an existing update
    """
    if 'application/json' not in request.META.get('HTTP_ACCEPT', 'application/json'):
        return HttpResponseBadRequest("Only supports json requests")

    course_key = CourseKey.from_string(course_key_string)
    usage_key = course_key.make_usage_key('course_info', 'updates')
    if provided_id == '':
        provided_id = None

    # check that logged in user has permissions to this item (GET shouldn't require this level?)
    if not has_course_access(request.user, usage_key.course_key):
        raise PermissionDenied()

    if request.method == 'GET':
        course_updates = get_course_updates(usage_key, provided_id)
        if isinstance(course_updates, dict) and course_updates.get('error'):
            return JsonResponse(course_updates, course_updates.get('status', 400))
        else:
            return JsonResponse(course_updates)
    elif request.method == 'DELETE':
        try:
            return JsonResponse(delete_course_update(usage_key, request.json, provided_id, request.user))
        except:
            return HttpResponseBadRequest(
                "Failed to delete",
                content_type="text/plain"
            )
    # can be either and sometimes django is rewriting one to the other:
    elif request.method in ('POST', 'PUT'):
        try:
            return JsonResponse(update_course_updates(usage_key, request.json, provided_id, request.user))
        except:
            return HttpResponseBadRequest(
                "Failed to save",
                content_type="text/plain"
            )
Пример #10
0
def mobi_notice(request):
    if "application/json" not in request.META.get('HTTP_ACCEPT',
                                                  'application/json'):
        return HttpResponseBadRequest("Only support json requests")

    if "course_id" not in request.POST:
        return JsonResponse({"errcode": 20000, "errmsg": "params error"})
    try:
        course_locator = BlockUsageLocator(
            package_id=request.POST["course_id"],
            branch='draft',
            version_guid=None,
            block_id='updates')
        location = loc_mapper().translate_locator_to_location(course_locator)
        return JsonResponse(get_course_updates(location, None))
    except:
        return HttpResponseBadRequest("Fail to get data")
Пример #11
0
def course_info(request, org, course, name, provided_id=None):
    """
    Send models and views as well as html for editing the course info to the
    client.

    org, course, name: Attributes of the Location for the item to edit
    """
    location = get_location_and_verify_access(request, org, course, name)

    course_module = modulestore().get_item(location)

    # get current updates
    location = Location(['i4x', org, course, 'course_info', "updates"])

    return render_to_response('course_info.html', {
        'context_course': course_module,
        'url_base': "/" + org + "/" + course + "/",
        'course_updates': json.dumps(get_course_updates(location)),
        'handouts_location': Location(['i4x', org, course, 'course_info', 'handouts']).url() })
Пример #12
0
def course_info_update_handler(
    request, tag=None, course_id=None, branch=None, version_guid=None, block=None, provided_id=None
    ):
    """
    restful CRUD operations on course_info updates.
    provided_id should be none if it's new (create) and index otherwise.
    GET
        json: return the course info update models
    POST
        json: create an update
    PUT or DELETE
        json: change an existing update
    """
    if 'application/json' not in request.META.get('HTTP_ACCEPT', 'application/json'):
        return HttpResponseBadRequest("Only supports json requests")
    updates_locator = BlockUsageLocator(course_id=course_id, branch=branch, version_guid=version_guid, usage_id=block)
    updates_location = loc_mapper().translate_locator_to_location(updates_locator)
    if provided_id == '':
        provided_id = None

    # check that logged in user has permissions to this item (GET shouldn't require this level?)
    if not has_access(request.user, updates_location):
        raise PermissionDenied()

    if request.method == 'GET':
        return JsonResponse(get_course_updates(updates_location, provided_id))
    elif request.method == 'DELETE':
        try:
            return JsonResponse(delete_course_update(updates_location, request.json, provided_id))
        except:
            return HttpResponseBadRequest(
                "Failed to delete",
                content_type="text/plain"
            )
    # can be either and sometimes django is rewriting one to the other:
    elif request.method in ('POST', 'PUT'):
        try:
            return JsonResponse(update_course_updates(updates_location, request.json, provided_id))
        except:
            return HttpResponseBadRequest(
                "Failed to save",
                content_type="text/plain"
            )
Пример #13
0
def course_info_updates(request, org, course, provided_id=None):
    """
    restful CRUD operations on course_info updates.

    org, course: Attributes of the Location for the item to edit
    provided_id should be none if it's new (create) and a composite of the update db id + index otherwise.
    """
    # ??? No way to check for access permission afaik
    # get current updates
    location = ['i4x', org, course, 'course_info', "updates"]

    # Hmmm, provided_id is coming as empty string on create whereas I believe it used to be None :-(
    # Possibly due to my removing the seemingly redundant pattern in urls.py
    if provided_id == '':
        provided_id = None

    # check that logged in user has permissions to this item
    if not has_access(request.user, location):
        raise PermissionDenied()

    real_method = get_request_method(request)

    if request.method == 'GET':
        return HttpResponse(json.dumps(get_course_updates(location)),
                            mimetype="application/json")
    elif real_method == 'DELETE':
        try:
            return HttpResponse(json.dumps(
                delete_course_update(location, request.POST, provided_id)),
                                mimetype="application/json")
        except:
            return HttpResponseBadRequest("Failed to delete",
                                          content_type="text/plain")
    elif request.method == 'POST':
        try:
            return HttpResponse(json.dumps(
                update_course_updates(location, request.POST, provided_id)),
                                mimetype="application/json")
        except:
            return HttpResponseBadRequest("Failed to save",
                                          content_type="text/plain")
Пример #14
0
def course_info_updates(request, org, course, provided_id=None):
    """
    restful CRUD operations on course_info updates.

    org, course: Attributes of the Location for the item to edit
    provided_id should be none if it's new (create) and a composite of the
    update db id + index otherwise.
    """
    # ??? No way to check for access permission afaik
    # get current updates
    location = ['i4x', org, course, 'course_info', "updates"]

    if provided_id == '':
        provided_id = None

    # check that logged in user has permissions to this item
    if not has_access(request.user, location):
        raise PermissionDenied()

    if request.method == 'GET':
        return JsonResponse(get_course_updates(location))
    elif request.method == 'DELETE':
        try:
            return JsonResponse(delete_course_update(location, request.POST, provided_id))
        except:
            return HttpResponseBadRequest(
                "Failed to delete",
                content_type="text/plain"
            )
    # can be either and sometimes django is rewriting one to the other:
    elif request.method in ('POST', 'PUT'): 
        try:
            return JsonResponse(update_course_updates(location, request.POST, provided_id))
        except:
            return HttpResponseBadRequest(
                "Failed to save",
                content_type="text/plain"
            )
Пример #15
0
def course_info_updates(request, org, course, provided_id=None):
    """
    restful CRUD operations on course_info updates.

    org, course: Attributes of the Location for the item to edit
    provided_id should be none if it's new (create) and a composite of the
    update db id + index otherwise.
    """
    # ??? No way to check for access permission afaik
    # get current updates
    location = ['i4x', org, course, 'course_info', "updates"]

    if provided_id == '':
        provided_id = None

    # check that logged in user has permissions to this item
    if not has_access(request.user, location):
        raise PermissionDenied()

    if request.method == 'GET':
        return JsonResponse(get_course_updates(location))
    elif request.method == 'DELETE':
        try:
            return JsonResponse(delete_course_update(location, request.POST, provided_id))
        except:
            return HttpResponseBadRequest(
                "Failed to delete",
                content_type="text/plain"
            )
    # can be either and sometimes django is rewriting one to the other:
    elif request.method in ('POST', 'PUT'): 
        try:
            return JsonResponse(update_course_updates(location, request.POST, provided_id))
        except:
            return HttpResponseBadRequest(
                "Failed to save",
                content_type="text/plain"
            )
Пример #16
0
 def _updates_validation(self, course, request):
     updates_usage_key = course.id.make_usage_key('course_info', 'updates')
     updates = get_course_updates(updates_usage_key, provided_id=None, user_id=request.user.id)
     return dict(
         has_update=len(updates) > 0,
     )