Esempio n. 1
0
def update_plugin_state(course_id, plugin_id):
    """
    Change the active state of a plugin.
    """
    course = Course.query.get_or_404(course_id)

    if plugin_id not in plugins.plugin_list():
        return Iresponse.create_response({"error": "Plugin does not exist"},
                                         400)

    pids = [p.plugin for p in course.plugins]
    if plugin_id not in pids:
        cp = CoursePlugin(id=uuid.uuid4(),
                          plugin=plugin_id,
                          course_id=course_id,
                          active=request.get_json()['active'])
        course.plugins.append(cp)
        try:
            db.session.add(course)
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            print(e)
            return Iresponse.create_response(
                {
                    "status":
                    "could not process\
                                                         your request"
                }, 500)
        return Iresponse.create_response(
            {
                "status": "success",
                "active": cp.active
            }, 200)
    else:
        cp = next((p for p in course.plugins if p.plugin == plugin_id), None)
        cp.active = request.get_json()['active']
        try:
            db.session.add(course)
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            print(e)
            return Iresponse.create_response(
                {
                    "status":
                    "could not process\
                                              your request"
                }, 500)
        return Iresponse.create_response(
            {
                "status": "success",
                "active": cp.active
            }, 200)
Esempio n. 2
0
def retrieve_course_plugins(course_id):
    """
    Retrieve all plugin settins for this course. This means a list
    of all plugins and the active state for each of them.
    """
    course = Course.query.get_or_404(course_id)

    all_plugins = plugins.plugin_list()
    tmp = {}
    for p in all_plugins:
        cp = next((pl for pl in course.plugins if pl.plugin == p), None)
        tmp[p] = {}
        tmp[p]['active'] = False if not cp else cp.active
        tmp[p]['name'] = plugins.get_plugin_name(p)

    return Iresponse.create_response(tmp, 200)
Esempio n. 3
0
def update_plugin_settings(course_id, plugin_id):
    """
    Update the settings for given plugin.
    """
    course = Course.query.get_or_404(course_id)

    if plugin_id not in plugins.plugin_list():
        return Iresponse.create_response({"error": "Plugin does not exist"},
                                         400)

    cp = next((p for p in course.plugins if p.plugin == plugin_id), None)

    if cp is None:
        return Iresponse.create_response({"error": "Cannot configure plugin"},
                                         400)

    js = request.get_json()

    # We iterate over the known settings to prevent setting arbitrary
    # settings from evil requests, which could waste database space.
    if not cp.settings:
        print("cp.settings: {}".format(cp.settings))
        print("cp.settings not set. assign empty dict")
        print("cp: {}".format(cp))
        cp.settings = {}
        print("done")
    for key in cp.get_setting_values():
        print("add {} to settings".format(key))
        cp.settings[key] = js[key]
        print('done')

    try:
        db.session.commit()
        return Iresponse.create_response({"status": "success"}, 200)
    except Exception as e:
        db.session.rollback()
        print(e)
        # This should not happen so a 500 is returned. If for some reason user
        # input triggers this response, the cause of it should be detected
        # before adding to db and return a 400 instead of letting it arrive
        # here.
        return Iresponse.create_response(
            {"status": "could not process your\
                request"}, 500)
Esempio n. 4
0
def activate_plugin(label_id):
    """
    Create a record in the database to activate this plugin.
    """
    label = Label.query.get_or_404(label_id)
    plugin_id = request.get_json()['plugin_id']
    if plugin_id not in plugins.plugin_list():
        return Iresponse.create_response({"error": "Plugin does not exist"},
                                         404)
    lp = LabelPlugin()
    lp.id = uuid.uuid4()
    lp.plugin = plugin_id
    lp.label = label
    lp.assignment_id = ''
    if database.addItemSafelyToDB(lp):
        return Iresponse.create_response({"status": "success"}, 200)
    else:
        return Iresponse.create_response({"error": "Something went wrong"},
                                         500)
Esempio n. 5
0
def get_plugin_configurations(course_id, plugin_id):
    """
    Returns the configuration options for this plugin.
    """
    course = Course.query.get_or_404(course_id)

    if plugin_id not in plugins.plugin_list():
        return Iresponse.create_response({"error": "Plugin does not exist"},
                                         404)

    pids = [p.plugin for p in course.plugins]
    if plugin_id not in pids:
        return Iresponse.create_response(
            {"error": "Plugin not available for\
                this course"}, 403)

    cp = next((p for p in course.plugins if p.plugin == plugin_id), None)
    if cp:
        return Iresponse.create_response(cp.get_settings(), 200)
    else:
        return Iresponse.create_response({"error": "Not found"}, 404)