Пример #1
0
def lms_admin_scorm(laboratory_identifier):
    db_lms = db_session.query(LMS).filter_by(lms_login=session['lms']).first()
    if db_lms is None:
        flash("Error: LMS does not exist.")
        return render_template("lms_admin/scorm_errors.html")

    permission_on_lab = db_session.query(PermissionOnLaboratory).filter_by(
        lms=db_lms, local_identifier=laboratory_identifier).first()
    if permission_on_lab is None:
        flash("Error: LMS does not have permission on that laboratory. ")
        return render_template("lms_admin/scorm_errors.html")

    lms_path = urlparse.urlparse(db_lms.url).path or '/'
    extension = '/'
    if 'lms4labs/' in lms_path:
        extension = lms_path[lms_path.rfind('lms4labs/lms/list') +
                             len('lms4labs/lms/list'):]
        lms_path = lms_path[:lms_path.rfind('lms4labs/')]

    content = get_scorm_object(False, laboratory_identifier, lms_path,
                               extension)
    return Response(content,
                    headers={
                        'Content-Type':
                        'application/zip',
                        'Content-Disposition':
                        'attachment; filename=scorm_%s.zip' %
                        laboratory_identifier
                    })
Пример #2
0
def lms_admin_scorms():
    db_lms = db_session.query(LMS).filter_by(lms_login = session['lms']).first()
    if db_lms is None:
        flash("Error: LMS does not exist")
        return render_template("lms_admin/scorm_errors.html")
    permission_on_labs = db_session.query(PermissionOnLaboratory).filter_by(lms = db_lms).all()
    return render_template("lms_admin/scorms.html", permissions = permission_on_labs)
Пример #3
0
def lms_admin_courses_permissions(course_id):
    db_lms = db_session.query(LMS).filter_by(lms_login = session['lms']).first()
    course = db_session.query(Course).filter_by(id = course_id, lms = db_lms).first()

    if course is None:
        return render_template("lms_admin/course_errors.html")

    granted_permission_ids = [ permission.permission_on_lab_id for permission in course.permissions ]

    if request.method == 'POST':
        if request.form.get('action','').startswith('revoke-'):
            try:
                permission_on_lab_id = int(request.form['action'][len('revoke-'):])
            except:
                flash("Error parsing permission on lab identifier")
                return render_template("lms_admin/course_errors.html")

            permission_on_course = db_session.query(PermissionOnCourse).filter_by(course = course, permission_on_lab_id = permission_on_lab_id).first()
            if permission_on_course is not None:
                db_session.delete(permission_on_course)
                db_session.commit()
                
            return redirect(url_for('lms_admin_courses_permissions', course_id = course_id))

    return render_template("lms_admin/courses_permissions.html", permissions = db_lms.permissions, course = course, granted_permission_ids = granted_permission_ids)
Пример #4
0
def admin_rlms_rlms_lab_edit_permissions(rlmstype, rlmsversion, id, lab_id):
    template_variables = {}

    lab, rlms = get_lab_and_lms(rlmstype, rlmsversion, id, lab_id)
    if lab is None or rlms is None:
        return render_template("labmanager_admin/rlms_errors.html")

    if request.method == 'POST':
        if request.form.get('action','').startswith('revoke-'):
            lms_login = request.form['action'][len('revoke-'):]
            lms = db_session.query(LMS).filter_by(lms_login = lms_login).first()
            if lms is None:
                return render_template("labmanager_admin/rlms_errors.html")
           
            permission = db_session.query(PermissionOnLaboratory).filter_by(laboratory_id = lab_id, lms_id = lms.id).first()
            if permission is not None:
                db_session.delete(permission)
                db_session.commit()

    granted_lms_ids = [ perm.lms_id for perm in lab.permissions ]

    lmss = db_session.query(LMS).all()

    template_variables['granted_lms_ids'] = granted_lms_ids
    template_variables['type_name']       = rlmstype
    template_variables['version']         = rlmsversion
    template_variables['rlms_name']       = rlms.name
    template_variables['rlms_id']         = id
    template_variables['lab_name']        = lab.name
    template_variables['lab_id']          = lab_id
    template_variables['lmss']            = lmss

    return render_template("labmanager_admin/rlms_rlms_lab_edit_permissions.html", **template_variables)
Пример #5
0
def lms_admin_scorms():
    db_lms = db_session.query(LMS).filter_by(lms_login=session['lms']).first()
    if db_lms is None:
        flash("Error: LMS does not exist")
        return render_template("lms_admin/scorm_errors.html")
    permission_on_labs = db_session.query(PermissionOnLaboratory).filter_by(
        lms=db_lms).all()
    return render_template("lms_admin/scorms.html",
                           permissions=permission_on_labs)
Пример #6
0
def lms_admin_external_courses():
    q     = request.args.get('q','')
    try:
        start = int(request.args.get('start','0'))
    except:
        start = 0
    db_lms = db_session.query(LMS).filter_by(lms_login = session['lms']).first()
    user     = db_lms.labmanager_login
    password = db_lms.labmanager_password
    url = "%s?q=%s&start=%s" % (db_lms.url, q, start)

    VISIBLE_PAGES = 10
    results = retrieve_courses(url, user, password)
    if isinstance(results, basestring):
        return "Invalid JSON provided or could not connect to the LMS. Look at the logs for more information"

    try:
        courses_data = results['courses']
        courses = [ (course['id'], course['name']) for course in courses_data ]
        course_dict = dict(courses)
        number   = int(results['number'])
        per_page = int(results['per-page'])
        number_of_pages = ((number - 1) / per_page ) + 1
        current_page    = ((start - 1)  / per_page ) + 1

        THEORICAL_BEFORE_PAGES = VISIBLE_PAGES / 2
        if current_page < THEORICAL_BEFORE_PAGES:
            BEFORE_PAGES = current_page
            AFTER_PAGES  = VISIBLE_PAGES - current_page
        else:
            BEFORE_PAGES = THEORICAL_BEFORE_PAGES
            AFTER_PAGES  = BEFORE_PAGES

        min_page = (start/VISIBLE_PAGES - BEFORE_PAGES)
        max_page = (start/VISIBLE_PAGES + AFTER_PAGES)
        if max_page >= number_of_pages:
            max_page = number_of_pages
        if min_page <= -1:
            min_page = 0
        current_pages   = range(min_page, max_page)
    except:
        traceback.print_exc()
        return "Malformed data retrieved. Look at the logs for more information"

    existing_courses = db_session.query(Course).filter(Course.course_id.in_(course_dict.keys()), Course.lms == db_lms).all()
    existing_course_ids = [ existing_course.course_id for existing_course in existing_courses ]

    if request.method == 'POST':
        for course_id in request.form:
            if course_id != 'action' and course_id in course_dict and course_id not in existing_course_ids:
                db_course = Course(db_lms, course_id, course_dict[course_id])
                db_session.add(db_course)
        db_session.commit()
        return redirect(url_for('lms_admin_courses'))

    return render_template("lms_admin/courses_external.html", courses = courses, existing_course_ids = existing_course_ids, q = q, current_page = current_page, number = number, current_pages = current_pages, per_page = per_page, start = start)
Пример #7
0
def lms_admin_courses_permissions_edit(course_id, permission_on_lab_id):
    db_lms = db_session.query(LMS).filter_by(lms_login=session['lms']).first()
    course = db_session.query(Course).filter_by(id=course_id,
                                                lms=db_lms).first()
    permission_on_lab = db_session.query(PermissionOnLaboratory).filter_by(
        id=permission_on_lab_id, lms=db_lms).first()

    if course is None or permission_on_lab is None:
        return render_template("lms_admin/course_errors.html")

    lab = permission_on_lab.laboratory
    db_rlms = lab.rlms
    db_rlms_version = db_rlms.rlms_version
    db_rlms_type = db_rlms_version.rlms_type
    rlmstype = db_rlms_type.name
    rlmsversion = db_rlms_version.version

    permission = db_session.query(PermissionOnCourse).filter_by(
        permission_on_lab=permission_on_lab, course=course).first()

    PermissionsForm = get_permissions_form_class(rlmstype, rlmsversion)
    form = PermissionsForm()
    if form.validate_on_submit():
        configuration_dict = {}
        for field in form.get_field_names():
            data = getattr(form, field).data
            if data != '':
                configuration_dict[field] = data

        configuration = json.dumps(configuration_dict)

        if permission is None:  # Not yet granted: add it
            permission = PermissionOnCourse(
                permission_on_lab=permission_on_lab,
                course=course,
                configuration=configuration)
            db_session.add(permission)
        else:  # Already granted: edit it
            permission.configuration = configuration
        db_session.commit()
        return redirect(
            url_for('lms_admin_courses_permissions', course_id=course_id))

    if permission is not None:
        configuration_dict = json.loads(permission.configuration or '{}')
        for field in configuration_dict:
            if hasattr(form, field):
                getattr(form, field).data = configuration_dict.get(field, '')

    return render_template("lms_admin/courses_permissions_add.html",
                           course=course,
                           form=form,
                           lab=lab)
Пример #8
0
def _add_or_edit_lms(id):
    form = AddLmsForm(id is None)

    if form.validate_on_submit():
        if id is None:
            new_lms = LMS(name=form.name.data,
                          url=form.url.data,
                          lms_login=form.lms_login.data,
                          lms_password=hash_password(form.lms_password.data),
                          labmanager_login=form.labmanager_login.data,
                          labmanager_password=form.labmanager_password.data)
            db_session.add(new_lms)
        else:
            lms = db_session.query(LMS).filter_by(id=id).first()
            if lms is None:
                return render_template("labmanager_admin/lms_errors.html")

            lms.url = form.url.data
            lms.name = form.name.data
            lms.lms_login = form.lms_login.data
            lms.labmanager_login = form.labmanager_login.data
            if form.lms_password.data:
                lms.lms_password = hash_password(form.lms_password.data)
            if form.labmanager_password.data:
                lms.labmanager_password = form.labmanager_password.data

        db_session.commit()
        return redirect(
            url_for('admin_lms_edit', lms_login=form.lms_login.data))

    if id is not None:
        lms = db_session.query(LMS).filter_by(id=id).first()
        if lms is None:
            return render_template("labmanager_admin/lms_errors.html")

        name = lms.name

        form.url.data = lms.url
        form.name.data = lms.name
        form.lms_login.data = lms.lms_login
        form.labmanager_login.data = lms.labmanager_login

        courses = retrieve_courses(lms.url, lms.labmanager_login,
                                   lms.labmanager_password)
        if isinstance(courses, basestring):
            # There was an error, show it
            flash(courses)
    else:
        name = None

    return render_template("labmanager_admin/lms_add.html",
                           form=form,
                           name=name)
Пример #9
0
def _add_or_edit_rlms(rlmstype, rlmsversion, id):
    if not is_supported(rlmstype, rlmsversion):
        return "Not supported"

    rlms_version = _get_rlms_version(rlmstype, rlmsversion)
    if rlms_version is None:
        return render_template("labmanager_admin/rlms_errors.html")

    if id is not None:
        rlms = db_session.query(RLMS).filter_by(id = id).first()
        if rlms is None or rlms.rlms_version != rlms_version:
            return render_template("labmanager_admin/rlms_errors.html")


    AddForm = get_form_class(rlmstype, rlmsversion)
    form = AddForm(id is None)

    if form.validate_on_submit():
        configuration_dict = {}
        for field in form.get_field_names():
            if field not in ('location', 'name'):
                configuration_dict[field] = getattr(form, field).data

        configuration = json.dumps(configuration_dict)
        
        if id is None:
            new_rlms = RLMS(name = form.name.data, location = form.location.data, rlms_version = rlms_version, configuration = configuration)
            db_session.add(new_rlms)
        else:
            rlms = db_session.query(RLMS).filter_by(id = id).first()
            if rlms is None:
                return render_template("labmanager_admin/rlms_errors.html")
            rlms.name          = form.name.data
            rlms.location      = form.location.data
            rlms.configuration = AddForm.process_configuration(rlms.configuration, configuration)

        db_session.commit()
        return redirect(url_for('admin_rlms_rlms', rlmstype = rlmstype, rlmsversion = rlmsversion))

    if id is not None:
        rlms = db_session.query(RLMS).filter_by(id = id).first()
        if rlms is None:
            return render_template("labmanager_admin/rlms_errors.html")

        form.name.data     = rlms.name
        form.location.data = rlms.location
        if rlms.configuration is not None and rlms.configuration != '':
            configuration = json.loads(rlms.configuration)
            for key in configuration:
                getattr(form, key).data = configuration[key]

    return render_template("labmanager_admin/rlms_rlms_add.html", rlmss = rlms_version.rlms, name = rlms_version.rlms_type.name, version = rlms_version.version, form = form)
Пример #10
0
def _add_or_edit_lms(id):
    form = AddLmsForm(id is None)

    if form.validate_on_submit():
        if id is None:
            new_lms = LMS(name = form.name.data, url = form.url.data, 
                            lms_login           = form.lms_login.data, 
                            lms_password        = hash_password(form.lms_password.data), 
                            labmanager_login    = form.labmanager_login.data, 
                            labmanager_password = form.labmanager_password.data)
            db_session.add(new_lms)
        else:
            lms = db_session.query(LMS).filter_by(id = id).first()
            if lms is None:
                return render_template("labmanager_admin/lms_errors.html")


            lms.url               = form.url.data
            lms.name              = form.name.data
            lms.lms_login         = form.lms_login.data
            lms.labmanager_login  = form.labmanager_login.data
            if form.lms_password.data:
                lms.lms_password        = hash_password(form.lms_password.data)
            if form.labmanager_password.data:
                lms.labmanager_password = form.labmanager_password.data

        db_session.commit()
        return redirect(url_for('admin_lms_edit', lms_login = form.lms_login.data))
    
    if id is not None:
        lms = db_session.query(LMS).filter_by(id = id).first()
        if lms is None:
            return render_template("labmanager_admin/lms_errors.html")

        name = lms.name

        form.url.data              = lms.url
        form.name.data             = lms.name
        form.lms_login.data        = lms.lms_login
        form.labmanager_login.data = lms.labmanager_login

        courses = retrieve_courses(lms.url, lms.labmanager_login, lms.labmanager_password)
        if isinstance(courses, basestring):
            # There was an error, show it
            flash(courses)
    else:
        name = None

    return render_template("labmanager_admin/lms_add.html", form = form, name = name)
Пример #11
0
def lms_admin_authenticate_scorm():
    db_lms = db_session.query(LMS).filter_by(lms_login = session['lms']).first()
    if db_lms is None:
        flash("Error: LMS does not exist.")
        return render_template("lms_admin/scorm_errors.html")

    return get_authentication_scorm(db_lms.url)
Пример #12
0
def lms_admin_authenticate_scorm():
    db_lms = db_session.query(LMS).filter_by(lms_login=session['lms']).first()
    if db_lms is None:
        flash("Error: LMS does not exist.")
        return render_template("lms_admin/scorm_errors.html")

    return get_authentication_scorm(db_lms.url)
Пример #13
0
def admin_users():
    if request.method == 'POST' and request.form.get(
            'action', '').lower().startswith('add'):
        return redirect(url_for('admin_user_add'))

    users = db_session.query(LabManagerUser).all()
    return render_template("labmanager_admin/user.html", users=users)
Пример #14
0
def _get_rlms_version(rlmstype, rlmsversion):
    rlms_type = db_session.query(RLMSType).filter_by(name = rlmstype).first()
    if rlms_type is not None:
        rlms_version = ([ version for version in rlms_type.versions if version.version == rlmsversion ] or [None])[0]
        if rlms_version is not None:
            return rlms_version
    return None
Пример #15
0
def admin_login():
    login_error = False

    if request.method == 'POST':
        login = request.form['username']
        password = request.form['password']

        user = db_session.query(LabManagerUser).filter_by(
            login=login, password=hash_password(password)).first()

        if user is not None:
            session['logged_in'] = True
            session['session_type'] = 'labmanager_admin'
            session['user_id'] = user.id
            session['user_name'] = user.name
            session['login'] = login

            next = request.args.get('next')
            if next is not None and next.startswith(
                    app.config.get('URL_ROOT',
                                   request.url_root)) and next != '':
                return redirect(next)
            return redirect(url_for('admin_index'))

        login_error = True

    return render_template("labmanager_admin/login.html",
                           login_error=login_error,
                           next=request.args.get('next', ''))
Пример #16
0
def admin_rlms():
    types = db_session.query(RLMSType).all()
    retrieved_types = set( (retrieved_type.name for retrieved_type in types) )

    if request.method == 'POST' and request.form.get('action','').lower().startswith('add'):
        for supported_type in get_supported_types():
            if supported_type not in retrieved_types:
                new_type = RLMSType(supported_type)
                db_session.add(new_type)
        db_session.commit()    
        types = db_session.query(RLMSType).all()
        retrieved_types = set( (retrieved_type.name for retrieved_type in types) )

    any_supported_missing = any([ supported_type not in retrieved_types for supported_type in get_supported_types()])

    return render_template("labmanager_admin/rlms_types.html", types = types, supported = get_supported_types(), any_supported_missing = any_supported_missing)
Пример #17
0
def admin_rlms_versions(rlmstype):
    rlms_type = db_session.query(RLMSType).filter_by(name=rlmstype).first()
    if rlms_type is not None:
        versions = rlms_type.versions

        retrieved_versions = set(
            (retrieved_version.version for retrieved_version in versions))

        if request.method == 'POST' and request.form.get(
                'action', '').lower().startswith('add'):
            for supported_version in get_supported_versions(rlmstype):
                if supported_version not in retrieved_versions:
                    new_version = RLMSTypeVersion(rlms_type, supported_version)
                    db_session.add(new_version)
            db_session.commit()

            versions = rlms_type.versions
            retrieved_versions = set(
                (retrieved_version.version for retrieved_version in versions))

        any_supported_missing = any([
            supported_version not in retrieved_versions
            for supported_version in get_supported_versions(rlmstype)
        ])

        return render_template("labmanager_admin/rlms_versions.html",
                               versions=versions,
                               name=rlms_type.name,
                               supported=get_supported_versions(rlmstype),
                               any_supported_missing=any_supported_missing)

    return render_template("labmanager_admin/rlms_errors.html")
Пример #18
0
def admin_lms():
    if request.method == 'POST' and request.form.get(
            'action', '').lower().startswith('add'):
        return redirect(url_for('admin_lms_add'))

    lmss = db_session.query(LMS).all()
    return render_template("labmanager_admin/lms.html", lmss=lmss)
Пример #19
0
def admin_rlms_rlms_list_external(rlmstype, rlmsversion, id):
    rlms = db_session.query(RLMS).filter_by(id = id).first()
    if rlms is None or rlms.rlms_version.version != rlmsversion or rlms.rlms_version.rlms_type.name != rlmstype:
        return render_template("labmanager_admin/rlms_errors.html")

    existing_laboratory_ids = [ laboratory.laboratory_id for laboratory in rlms.laboratories ]

    ManagerClass          = get_manager_class(rlmstype, rlmsversion)
    manager_class         = ManagerClass(rlms.configuration)
    try:
        available_laboratories = manager_class.get_laboratories()
    except:
        traceback.print_exc()
        flash("There was an error retrieving laboratories. Check the trace")
        return render_template("labmanager_admin/rlms_errors.html")

    available_laboratory_ids = [ lab.laboratory_id for lab in available_laboratories ]

    if request.method == 'POST':
        if request.form.get('action','') == 'add':
            for laboratory_id in request.form:
                if laboratory_id != 'action' and laboratory_id in available_laboratory_ids and laboratory_id not in existing_laboratory_ids:
                    new_lab = Laboratory(laboratory_id, laboratory_id, rlms)
                    db_session.add(new_lab)
            db_session.commit()
            return redirect(url_for('admin_rlms_rlms_list', rlmstype = rlmstype, rlmsversion = rlmsversion, id = id))

    return render_template("labmanager_admin/rlms_rlms_list_external.html", available_laboratories = available_laboratories, type_name = rlmstype, version = rlmsversion, rlms_name = rlms.name, existing_laboratory_ids = existing_laboratory_ids)
Пример #20
0
def get_lab_and_lms(rlmstype, rlmsversion, id, lab_id):
    lab = db_session.query(Laboratory).filter_by(id=lab_id).first()
    if lab is None:
        return None, None

    rlms = lab.rlms
    if rlms is None or rlms.id != id or rlms.rlms_version.version != rlmsversion or rlms.rlms_version.rlms_type.name != rlmstype:
        return None, None
    return lab, rlms
Пример #21
0
        def decorated(*args, **kwargs):
            if request.method == "POST" and request.form.get("action", "") == "delete":
                for current_id in request.form:
                    element = db_session.query(table).filter_by(id=current_id).first()
                    if element is not None:
                        db_session.delete(element)
                db_session.commit()

            return f(*args, **kwargs)
Пример #22
0
def get_lab_and_lms(rlmstype, rlmsversion, id, lab_id):
    lab  = db_session.query(Laboratory).filter_by(id = lab_id).first()
    if lab is None:
        return None, None

    rlms = lab.rlms
    if rlms is None or rlms.id != id or rlms.rlms_version.version != rlmsversion or rlms.rlms_version.rlms_type.name != rlmstype:
        return None, None
    return lab, rlms
Пример #23
0
def lms_admin_courses_permissions_edit(course_id, permission_on_lab_id):
    db_lms = db_session.query(LMS).filter_by(lms_login = session['lms']).first()
    course = db_session.query(Course).filter_by(id = course_id, lms = db_lms).first()
    permission_on_lab = db_session.query(PermissionOnLaboratory).filter_by(id = permission_on_lab_id, lms = db_lms).first()

    if course is None or permission_on_lab is None:
        return render_template("lms_admin/course_errors.html")

    lab             = permission_on_lab.laboratory
    db_rlms         = lab.rlms
    db_rlms_version = db_rlms.rlms_version
    db_rlms_type    = db_rlms_version.rlms_type
    rlmstype        = db_rlms_type.name
    rlmsversion     = db_rlms_version.version

    permission = db_session.query(PermissionOnCourse).filter_by(permission_on_lab = permission_on_lab, course = course).first()

    PermissionsForm = get_permissions_form_class(rlmstype, rlmsversion)
    form = PermissionsForm()
    if form.validate_on_submit():
        configuration_dict = {}
        for field in form.get_field_names():
            data = getattr(form, field).data
            if data != '':
                configuration_dict[field] = data

        configuration = json.dumps(configuration_dict)

        if permission is None: # Not yet granted: add it            
            permission = PermissionOnCourse(permission_on_lab = permission_on_lab, course = course, configuration = configuration)
            db_session.add(permission)
        else: # Already granted: edit it
            permission.configuration    = configuration
        db_session.commit()
        return redirect(url_for('lms_admin_courses_permissions', course_id = course_id))

    if permission is not None:
        configuration_dict = json.loads(permission.configuration or '{}')
        for field in configuration_dict:
            if hasattr(form, field):
                getattr(form, field).data = configuration_dict.get(field,'')

    return render_template("lms_admin/courses_permissions_add.html", course = course, form = form, lab = lab)
Пример #24
0
def _get_rlms_version(rlmstype, rlmsversion):
    rlms_type = db_session.query(RLMSType).filter_by(name=rlmstype).first()
    if rlms_type is not None:
        rlms_version = ([
            version
            for version in rlms_type.versions if version.version == rlmsversion
        ] or [None])[0]
        if rlms_version is not None:
            return rlms_version
    return None
Пример #25
0
def lms_admin_scorm(laboratory_identifier):
    db_lms = db_session.query(LMS).filter_by(lms_login = session['lms']).first()
    if db_lms is None:
        flash("Error: LMS does not exist.")
        return render_template("lms_admin/scorm_errors.html")

    permission_on_lab = db_session.query(PermissionOnLaboratory).filter_by(lms = db_lms, local_identifier = laboratory_identifier).first()
    if permission_on_lab is None:
        flash("Error: LMS does not have permission on that laboratory. ")
        return render_template("lms_admin/scorm_errors.html")

    lms_path = urlparse.urlparse(db_lms.url).path or '/'
    extension = '/'
    if 'lms4labs/' in lms_path:
        extension = lms_path[lms_path.rfind('lms4labs/lms/list') + len('lms4labs/lms/list'):]
        lms_path  = lms_path[:lms_path.rfind('lms4labs/')]

    content = get_scorm_object(False, laboratory_identifier, lms_path, extension)
    return Response(content, headers = {'Content-Type' : 'application/zip', 'Content-Disposition' : 'attachment; filename=scorm_%s.zip' % laboratory_identifier})
Пример #26
0
def _add_or_edit_user(id):
    form = AddUserForm(id is None)

    if form.validate_on_submit():
        if id is None:
            new_user = LabManagerUser(login=form.login.data,
                                      name=form.name.data,
                                      password=hash_password(
                                          form.password.data))
            db_session.add(new_user)
        else:
            user = db_session.query(LabManagerUser).filter_by(id=id).first()
            if user is None:
                return render_template("labmanager_admin/user_errors.html")

            if user.login == session.get('login'):
                session['login'] = form.login.data
                session['user_name'] = form.name.data

            user.name = form.name.data
            user.login = form.login.data
            if form.password.data:
                user.password = hash_password(form.password.data)

        db_session.commit()
        return redirect(url_for('admin_users'))

    if id is not None:
        user = db_session.query(LabManagerUser).filter_by(id=id).first()
        if user is None:
            return render_template("labmanager_admin/user_errors.html")

        name = user.name

        form.name.data = user.name
        form.login.data = user.login
    else:
        name = None

    return render_template("labmanager_admin/user_add.html",
                           form=form,
                           name=name)
Пример #27
0
        def decorated(*args, **kwargs):
            if request.method == 'POST' and request.form.get('action',
                                                             '') == 'delete':
                for current_id in request.form:
                    element = db_session.query(table).filter_by(
                        id=current_id).first()
                    if element is not None:
                        db_session.delete(element)
                db_session.commit()

            return f(*args, **kwargs)
Пример #28
0
def _add_or_edit_user(id):
    form = AddUserForm(id is None)

    if form.validate_on_submit():
        if id is None:
            new_user = LabManagerUser(login = form.login.data, name = form.name.data, 
                                    password = hash_password(form.password.data))
            db_session.add(new_user)
        else:
            user = db_session.query(LabManagerUser).filter_by(id = id).first()
            if user is None:
                return render_template("labmanager_admin/user_errors.html")

            if user.login == session.get('login'):
                session['login']     = form.login.data
                session['user_name'] = form.name.data

            user.name  = form.name.data
            user.login = form.login.data
            if form.password.data:
                user.password = hash_password(form.password.data)

        db_session.commit()
        return redirect(url_for('admin_users'))
    
    if id is not None:
        user = db_session.query(LabManagerUser).filter_by(id = id).first()
        if user is None:
            return render_template("labmanager_admin/user_errors.html")

        name = user.name

        form.name.data  = user.name
        form.login.data = user.login
    else:
        name = None

    return render_template("labmanager_admin/user_add.html", form = form, name = name)
Пример #29
0
def admin_rlms_rlms_lab_edit_permissions(rlmstype, rlmsversion, id, lab_id):
    template_variables = {}

    lab, rlms = get_lab_and_lms(rlmstype, rlmsversion, id, lab_id)
    if lab is None or rlms is None:
        return render_template("labmanager_admin/rlms_errors.html")

    if request.method == 'POST':
        if request.form.get('action', '').startswith('revoke-'):
            lms_login = request.form['action'][len('revoke-'):]
            lms = db_session.query(LMS).filter_by(lms_login=lms_login).first()
            if lms is None:
                return render_template("labmanager_admin/rlms_errors.html")

            permission = db_session.query(PermissionOnLaboratory).filter_by(
                laboratory_id=lab_id, lms_id=lms.id).first()
            if permission is not None:
                db_session.delete(permission)
                db_session.commit()

    granted_lms_ids = [perm.lms_id for perm in lab.permissions]

    lmss = db_session.query(LMS).all()

    template_variables['granted_lms_ids'] = granted_lms_ids
    template_variables['type_name'] = rlmstype
    template_variables['version'] = rlmsversion
    template_variables['rlms_name'] = rlms.name
    template_variables['rlms_id'] = id
    template_variables['lab_name'] = lab.name
    template_variables['lab_id'] = lab_id
    template_variables['lmss'] = lmss

    return render_template(
        "labmanager_admin/rlms_rlms_lab_edit_permissions.html",
        **template_variables)
Пример #30
0
def lms_admin_courses_permissions(course_id):
    db_lms = db_session.query(LMS).filter_by(lms_login=session['lms']).first()
    course = db_session.query(Course).filter_by(id=course_id,
                                                lms=db_lms).first()

    if course is None:
        return render_template("lms_admin/course_errors.html")

    granted_permission_ids = [
        permission.permission_on_lab_id for permission in course.permissions
    ]

    if request.method == 'POST':
        if request.form.get('action', '').startswith('revoke-'):
            try:
                permission_on_lab_id = int(
                    request.form['action'][len('revoke-'):])
            except:
                flash("Error parsing permission on lab identifier")
                return render_template("lms_admin/course_errors.html")

            permission_on_course = db_session.query(
                PermissionOnCourse).filter_by(
                    course=course,
                    permission_on_lab_id=permission_on_lab_id).first()
            if permission_on_course is not None:
                db_session.delete(permission_on_course)
                db_session.commit()

            return redirect(
                url_for('lms_admin_courses_permissions', course_id=course_id))

    return render_template("lms_admin/courses_permissions.html",
                           permissions=db_lms.permissions,
                           course=course,
                           granted_permission_ids=granted_permission_ids)
Пример #31
0
def admin_rlms():
    types = db_session.query(RLMSType).all()
    retrieved_types = set((retrieved_type.name for retrieved_type in types))

    if request.method == 'POST' and request.form.get(
            'action', '').lower().startswith('add'):
        for supported_type in get_supported_types():
            if supported_type not in retrieved_types:
                new_type = RLMSType(supported_type)
                db_session.add(new_type)
        db_session.commit()
        types = db_session.query(RLMSType).all()
        retrieved_types = set(
            (retrieved_type.name for retrieved_type in types))

    any_supported_missing = any([
        supported_type not in retrieved_types
        for supported_type in get_supported_types()
    ])

    return render_template("labmanager_admin/rlms_types.html",
                           types=types,
                           supported=get_supported_types(),
                           any_supported_missing=any_supported_missing)
Пример #32
0
def admin_rlms_rlms_list_external(rlmstype, rlmsversion, id):
    rlms = db_session.query(RLMS).filter_by(id=id).first()
    if rlms is None or rlms.rlms_version.version != rlmsversion or rlms.rlms_version.rlms_type.name != rlmstype:
        return render_template("labmanager_admin/rlms_errors.html")

    existing_laboratory_ids = [
        laboratory.laboratory_id for laboratory in rlms.laboratories
    ]

    ManagerClass = get_manager_class(rlmstype, rlmsversion)
    manager_class = ManagerClass(rlms.configuration)
    try:
        available_laboratories = manager_class.get_laboratories()
    except:
        traceback.print_exc()
        flash("There was an error retrieving laboratories. Check the trace")
        return render_template("labmanager_admin/rlms_errors.html")

    available_laboratory_ids = [
        lab.laboratory_id for lab in available_laboratories
    ]

    if request.method == 'POST':
        if request.form.get('action', '') == 'add':
            for laboratory_id in request.form:
                if laboratory_id != 'action' and laboratory_id in available_laboratory_ids and laboratory_id not in existing_laboratory_ids:
                    new_lab = Laboratory(laboratory_id, laboratory_id, rlms)
                    db_session.add(new_lab)
            db_session.commit()
            return redirect(
                url_for('admin_rlms_rlms_list',
                        rlmstype=rlmstype,
                        rlmsversion=rlmsversion,
                        id=id))

    return render_template("labmanager_admin/rlms_rlms_list_external.html",
                           available_laboratories=available_laboratories,
                           type_name=rlmstype,
                           version=rlmsversion,
                           rlms_name=rlms.name,
                           existing_laboratory_ids=existing_laboratory_ids)
Пример #33
0
def admin_rlms_versions(rlmstype):
    rlms_type = db_session.query(RLMSType).filter_by(name = rlmstype).first()
    if rlms_type is not None:
        versions = rlms_type.versions

        retrieved_versions = set( (retrieved_version.version for retrieved_version in versions) )

        if request.method == 'POST' and request.form.get('action','').lower().startswith('add'):
            for supported_version in get_supported_versions(rlmstype):
                if supported_version not in retrieved_versions:
                    new_version = RLMSTypeVersion(rlms_type, supported_version)
                    db_session.add(new_version)
            db_session.commit()

            versions = rlms_type.versions
            retrieved_versions = set( (retrieved_version.version for retrieved_version in versions) )

        any_supported_missing = any([ supported_version not in retrieved_versions for supported_version in get_supported_versions(rlmstype)])

        return render_template("labmanager_admin/rlms_versions.html", versions = versions, name = rlms_type.name, supported = get_supported_versions(rlmstype), any_supported_missing = any_supported_missing )

    return render_template("labmanager_admin/rlms_errors.html")
Пример #34
0
def admin_rlms_rlms_list(rlmstype, rlmsversion, id):
    rlms = db_session.query(RLMS).filter_by(id = id).first()
    if rlms is None or rlms.rlms_version.version != rlmsversion or rlms.rlms_version.rlms_type.name != rlmstype:
        return render_template("labmanager_admin/rlms_errors.html")

    if request.method == 'POST':
        if request.form.get('action','') == 'add':
            return redirect(url_for('admin_rlms_rlms_list_external', rlmstype = rlmstype, rlmsversion = rlmsversion, id = id))

    laboratories = rlms.laboratories

    ManagerClass          = get_manager_class(rlmstype, rlmsversion)
    manager_class         = ManagerClass(rlms.configuration)
    try:
        confirmed_laboratories = manager_class.get_laboratories()
    except:
        traceback.print_exc()
        flash("There was an error retrieving laboratories. Check the trace")
        return render_template("labmanager_admin/rlms_errors.html")

    confirmed_laboratory_ids = [ confirmed_laboratory.laboratory_id for confirmed_laboratory in confirmed_laboratories ]

    return render_template("labmanager_admin/rlms_rlms_list.html", laboratories = laboratories, type_name = rlmstype, version = rlmsversion, rlms_name = rlms.name, confirmed_laboratory_ids = confirmed_laboratory_ids, rlms_id = rlms.id)
Пример #35
0
def admin_login():
    login_error = False

    if request.method == 'POST':
        login    = request.form['username']
        password = request.form['password']

        user = db_session.query(LabManagerUser).filter_by(login = login, password = hash_password(password) ).first()

        if user is not None:
            session['logged_in']    = True
            session['session_type'] = 'labmanager_admin'
            session['user_id']      = user.id
            session['user_name']    = user.name
            session['login']        = login

            next = request.args.get('next')
            if next is not None and next.startswith(app.config.get('URL_ROOT', request.url_root)) and next != '':
                return redirect(next)
            return redirect(url_for('admin_index'))

        login_error = True

    return render_template("labmanager_admin/login.html", login_error = login_error, next = request.args.get('next','') )
Пример #36
0
def admin_rlms_rlms_list(rlmstype, rlmsversion, id):
    rlms = db_session.query(RLMS).filter_by(id=id).first()
    if rlms is None or rlms.rlms_version.version != rlmsversion or rlms.rlms_version.rlms_type.name != rlmstype:
        return render_template("labmanager_admin/rlms_errors.html")

    if request.method == 'POST':
        if request.form.get('action', '') == 'add':
            return redirect(
                url_for('admin_rlms_rlms_list_external',
                        rlmstype=rlmstype,
                        rlmsversion=rlmsversion,
                        id=id))

    laboratories = rlms.laboratories

    ManagerClass = get_manager_class(rlmstype, rlmsversion)
    manager_class = ManagerClass(rlms.configuration)
    try:
        confirmed_laboratories = manager_class.get_laboratories()
    except:
        traceback.print_exc()
        flash("There was an error retrieving laboratories. Check the trace")
        return render_template("labmanager_admin/rlms_errors.html")

    confirmed_laboratory_ids = [
        confirmed_laboratory.laboratory_id
        for confirmed_laboratory in confirmed_laboratories
    ]

    return render_template("labmanager_admin/rlms_rlms_list.html",
                           laboratories=laboratories,
                           type_name=rlmstype,
                           version=rlmsversion,
                           rlms_name=rlms.name,
                           confirmed_laboratory_ids=confirmed_laboratory_ids,
                           rlms_id=rlms.id)
Пример #37
0
def admin_user_edit(user_login):
    user = db_session.query(LabManagerUser).filter_by(login = user_login).first()
    if user is None:
        return render_template("labmanager_admin/user_errors.html")

    return _add_or_edit_user(user.id)
Пример #38
0
def admin_user_edit(user_login):
    user = db_session.query(LabManagerUser).filter_by(login=user_login).first()
    if user is None:
        return render_template("labmanager_admin/user_errors.html")

    return _add_or_edit_user(user.id)
Пример #39
0
def admin_users():
    if request.method == 'POST' and request.form.get('action','').lower().startswith('add'):
        return redirect(url_for('admin_user_add'))

    users = db_session.query(LabManagerUser).all()
    return render_template("labmanager_admin/user.html", users = users)
Пример #40
0
def admin_rlms_rlms_lab_edit_permissions_lms(rlmstype, rlmsversion, id, lab_id, lms_login):
    template_variables = {}

    lab, rlms = get_lab_and_lms(rlmstype, rlmsversion, id, lab_id)
    if lab is None or rlms is None:
        return render_template("labmanager_admin/rlms_errors.html")

    lms = db_session.query(LMS).filter_by(lms_login = lms_login).first()
    if lms is None:
        return render_template("labmanager_admin/rlms_errors.html")

    permission = db_session.query(PermissionOnLaboratory).filter_by(laboratory_id = lab_id, lms_id = lms.id).first()

    LmsPermissionsForm = get_lms_permissions_form_class(rlmstype, rlmsversion)
    form = LmsPermissionsForm()
    if form.validate_on_submit():
        configuration_dict = {}
        for field in form.get_field_names():
            if field != 'identifier':
                data = getattr(form, field).data
                if data != '':
                    configuration_dict[field] = data
        identifier = form.identifier.data

        configuration = json.dumps(configuration_dict)

        permission_with_same_identifier = db_session.query(PermissionOnLaboratory).filter_by(lms_id = lms.id, local_identifier = identifier).first()
        if permission_with_same_identifier is not None and permission_with_same_identifier != permission:
            flash("Could not grant permission. The identifier %s was already used in the LMS %s for the laboratory %s. Choose other identifier." % (identifier, lms.name, permission_with_same_identifier.laboratory.name))
            return render_template("labmanager_admin/rlms_errors.html")

        

        if permission is None: # Not yet granted: add it
            permission = PermissionOnLaboratory(lms = lms, laboratory = lab, configuration = configuration, local_identifier = identifier)
            db_session.add(permission)
        else: # Already granted: edit it
            permission.local_identifier = identifier
            permission.configuration    = configuration

        db_session.commit()
        return redirect(url_for('admin_rlms_rlms_lab_edit_permissions', rlmstype = rlmstype, rlmsversion = rlmsversion, id = id, lab_id = lab_id))

    if permission is not None:
        configuration_dict = json.loads(permission.configuration or '{}')
        for field in configuration_dict:
            if hasattr(form, field):
                getattr(form, field).data = configuration_dict.get(field,'')
        form.identifier.data = permission.local_identifier

    template_variables['type_name']       = rlmstype
    template_variables['version']         = rlmsversion
    template_variables['rlms_name']       = rlms.name
    template_variables['rlms_id']         = id
    template_variables['lab_name']        = lab.name
    template_variables['lab_id']          = lab_id
    template_variables['lms_name']        = lms.name
    template_variables['add_or_edit']     = permission is None
    template_variables['form']            = form

    return render_template("labmanager_admin/rlms_rlms_lab_edit_permissions_add.html", **template_variables)
Пример #41
0
def admin_lms_authenticate_scorm(lms_login):
    db_lms = db_session.query(LMS).filter_by(lms_login = lms_login).first()
    if db_lms is None:
        return render_template("labmanager_admin/lms_errors.html")

    return get_authentication_scorm(db_lms.url)
Пример #42
0
def lms_admin_external_courses():
    q = request.args.get('q', '')
    try:
        start = int(request.args.get('start', '0'))
    except:
        start = 0
    db_lms = db_session.query(LMS).filter_by(lms_login=session['lms']).first()
    user = db_lms.labmanager_login
    password = db_lms.labmanager_password
    url = "%s?q=%s&start=%s" % (db_lms.url, q, start)

    VISIBLE_PAGES = 10
    results = retrieve_courses(url, user, password)
    if isinstance(results, basestring):
        return "Invalid JSON provided or could not connect to the LMS. Look at the logs for more information"

    try:
        courses_data = results['courses']
        courses = [(course['id'], course['name']) for course in courses_data]
        course_dict = dict(courses)
        number = int(results['number'])
        per_page = int(results['per-page'])
        number_of_pages = ((number - 1) / per_page) + 1
        current_page = ((start - 1) / per_page) + 1

        THEORICAL_BEFORE_PAGES = VISIBLE_PAGES / 2
        if current_page < THEORICAL_BEFORE_PAGES:
            BEFORE_PAGES = current_page
            AFTER_PAGES = VISIBLE_PAGES - current_page
        else:
            BEFORE_PAGES = THEORICAL_BEFORE_PAGES
            AFTER_PAGES = BEFORE_PAGES

        min_page = (start / VISIBLE_PAGES - BEFORE_PAGES)
        max_page = (start / VISIBLE_PAGES + AFTER_PAGES)
        if max_page >= number_of_pages:
            max_page = number_of_pages
        if min_page <= -1:
            min_page = 0
        current_pages = range(min_page, max_page)
    except:
        traceback.print_exc()
        return "Malformed data retrieved. Look at the logs for more information"

    existing_courses = db_session.query(Course).filter(
        Course.course_id.in_(course_dict.keys()), Course.lms == db_lms).all()
    existing_course_ids = [
        existing_course.course_id for existing_course in existing_courses
    ]

    if request.method == 'POST':
        for course_id in request.form:
            if course_id != 'action' and course_id in course_dict and course_id not in existing_course_ids:
                db_course = Course(db_lms, course_id, course_dict[course_id])
                db_session.add(db_course)
        db_session.commit()
        return redirect(url_for('lms_admin_courses'))

    return render_template("lms_admin/courses_external.html",
                           courses=courses,
                           existing_course_ids=existing_course_ids,
                           q=q,
                           current_page=current_page,
                           number=number,
                           current_pages=current_pages,
                           per_page=per_page,
                           start=start)
Пример #43
0
def admin_lms():
    if request.method == 'POST' and request.form.get('action','').lower().startswith('add'):
        return redirect(url_for('admin_lms_add'))

    lmss = db_session.query(LMS).all()
    return render_template("labmanager_admin/lms.html", lmss = lmss)
Пример #44
0
def requests():
    """SCORM packages will perform requests to this method, which will 
    interact with the permitted laboratories"""

    if request.method == 'GET':
        return render_template("test_requests.html")

    json_data = get_json()
    if json_data is None: return "Could not process JSON data"

    courses             = json_data['courses']
    request_payload_str = json_data['request-payload']
    general_role        = json_data.get('is-admin', False)
    author              = json_data['user-id']
    complete_name       = json_data['full-name']
    user_agent          = json_data.get('user-agent', 'unknown user agent')
    origin_ip           = json_data.get('origin-ip', 'unknown IP address')
    referer             = json_data.get('referer', 'unknown referer')

    try:
        request_payload = json.loads(request_payload_str)
    except:
        traceback.print_exc()
        return "error: the request payload is not a valid JSON request"

    try:
        action = request_payload['action']
        if action == 'reserve':
            experiment_identifier = request_payload['experiment']
        else:
            # TODO: other operations: for teachers, etc.
            return "Unsupported operation"
    except KeyError:
        traceback.print_exc()
        return "Invalid response"

    # reserving...
    db_lms = db_session.query(LMS).filter_by(lms_login = g.lms).first()
    permission_on_lab = db_session.query(PermissionOnLaboratory).filter_by(lms_id = db_lms.id, local_identifier = experiment_identifier).first()
    good_msg  = "No good news :-("
    error_msg = None
    if permission_on_lab is None:
        error_msg = "Your LMS does not have permission to use that laboratory or that identifier does not exist"
    else:
        courses_configurations = []
        for course_permission in permission_on_lab.course_permissions:
            if course_permission.course.course_id in courses:
                # Let the server choose among the best possible configuration
                courses_configurations.append(course_permission.configuration)
        if len(courses_configurations) == 0 and not general_role:
            error_msg = "Your LMS has permission to use that laboratory; but you are not enrolled in any course with permissions to use it"
        else:
            lms_configuration = permission_on_lab.configuration
            db_laboratory   = permission_on_lab.laboratory
            db_rlms         = db_laboratory.rlms
            db_rlms_version = db_rlms.rlms_version
            db_rlms_type    = db_rlms_version.rlms_type

            ManagerClass = get_manager_class(db_rlms_type.name, db_rlms_version.version)
            remote_laboratory = ManagerClass(db_rlms.configuration)
            reservation_url = remote_laboratory.reserve(db_laboratory.laboratory_id, author, lms_configuration, courses_configurations, request_payload, user_agent, origin_ip, referer)

            good_msg = "You have been assigned %s of type %s version %s! <br/> Try it at <a href='%s'>%s</a>" % (db_rlms.name, db_rlms_type.name, db_rlms_version.version, reservation_url, reservation_url)

    if app.config.get('DEBUGGING_REQUESTS', True):
        courses_code = "<table><thead><tr><th>Course ID</th><th>Role</th></tr></thead><tbody>\n"
        for course_id in courses:
            role_in_course = courses[course_id]
            courses_code += "<tr><td>%s</td><td>%s</td></tr>\n" % (course_id, role_in_course)
        courses_code += "</tbody></table>"

        return """Hi %(name)s (username %(author)s),

            <p>I know that you're an admin ( %(admin)s ) in the LMS %(lms)s, and that you are in the following courses:</p>
            <br/>
            %(course_code)s
            <br/>
            <p>The following error messages were sent: %(error_msg)s</p>
            <p>The following good messages were sent: %(good_msg)s</p>

            Furthermore, you sent me this request:
            <pre>
            %(request)s
            </pre>
            
            And I'll process it!

            Original request:
            <pre> 
            %(json)s
            </pre>
        """ % {
            'name'        : cgi.escape(complete_name),
            'author'      : cgi.escape(author),
            'lms'         : cgi.escape(g.lms),
            'course_code' : courses_code,
            'request'     : cgi.escape(request_payload_str),
            'admin'        : general_role,
            'json'        : cgi.escape(json.dumps(json_data)),
            'error_msg'   : cgi.escape(error_msg or 'no error message'),
            'good_msg'    : good_msg or 'no good message',
        }
    else:
        if error_msg is None:
            return reservation_url
        else:
            return 'error:%s' % error_msg
Пример #45
0
def requests():
    """SCORM packages will perform requests to this method, which will 
    interact with the permitted laboratories"""

    if request.method == 'GET':
        return render_template("test_requests.html")

    json_data = get_json()
    if json_data is None: return "Could not process JSON data"

    courses = json_data['courses']
    request_payload_str = json_data['request-payload']
    general_role = json_data.get('is-admin', False)
    author = json_data['user-id']
    complete_name = json_data['full-name']
    user_agent = json_data.get('user-agent', 'unknown user agent')
    origin_ip = json_data.get('origin-ip', 'unknown IP address')
    referer = json_data.get('referer', 'unknown referer')

    try:
        request_payload = json.loads(request_payload_str)
    except:
        traceback.print_exc()
        return "error: the request payload is not a valid JSON request"

    try:
        action = request_payload['action']
        if action == 'reserve':
            experiment_identifier = request_payload['experiment']
        else:
            # TODO: other operations: for teachers, etc.
            return "Unsupported operation"
    except KeyError:
        traceback.print_exc()
        return "Invalid response"

    # reserving...
    db_lms = db_session.query(LMS).filter_by(lms_login=g.lms).first()
    permission_on_lab = db_session.query(PermissionOnLaboratory).filter_by(
        lms_id=db_lms.id, local_identifier=experiment_identifier).first()
    good_msg = "No good news :-("
    error_msg = None
    if permission_on_lab is None:
        error_msg = "Your LMS does not have permission to use that laboratory or that identifier does not exist"
    else:
        courses_configurations = []
        for course_permission in permission_on_lab.course_permissions:
            if course_permission.course.course_id in courses:
                # Let the server choose among the best possible configuration
                courses_configurations.append(course_permission.configuration)
        if len(courses_configurations) == 0 and not general_role:
            error_msg = "Your LMS has permission to use that laboratory; but you are not enrolled in any course with permissions to use it"
        else:
            lms_configuration = permission_on_lab.configuration
            db_laboratory = permission_on_lab.laboratory
            db_rlms = db_laboratory.rlms
            db_rlms_version = db_rlms.rlms_version
            db_rlms_type = db_rlms_version.rlms_type

            ManagerClass = get_manager_class(db_rlms_type.name,
                                             db_rlms_version.version)
            remote_laboratory = ManagerClass(db_rlms.configuration)
            reservation_url = remote_laboratory.reserve(
                db_laboratory.laboratory_id, author, lms_configuration,
                courses_configurations, user_agent, origin_ip, referer)

            good_msg = "You have been assigned %s of type %s version %s! <br/> Try it at <a href='%s'>%s</a>" % (
                db_rlms.name, db_rlms_type.name, db_rlms_version.version,
                reservation_url, reservation_url)

    if app.config.get('DEBUGGING_REQUESTS', True):
        courses_code = "<table><thead><tr><th>Course ID</th><th>Role</th></tr></thead><tbody>\n"
        for course_id in courses:
            role_in_course = courses[course_id]
            courses_code += "<tr><td>%s</td><td>%s</td></tr>\n" % (
                course_id, role_in_course)
        courses_code += "</tbody></table>"

        return """Hi %(name)s (username %(author)s),

            <p>I know that you're an admin ( %(admin)s ) in the LMS %(lms)s, and that you are in the following courses:</p>
            <br/>
            %(course_code)s
            <br/>
            <p>The following error messages were sent: %(error_msg)s</p>
            <p>The following good messages were sent: %(good_msg)s</p>

            Furthermore, you sent me this request:
            <pre>
            %(request)s
            </pre>
            
            And I'll process it!

            Original request:
            <pre> 
            %(json)s
            </pre>
        """ % {
            'name': cgi.escape(complete_name),
            'author': cgi.escape(author),
            'lms': cgi.escape(g.lms),
            'course_code': courses_code,
            'request': cgi.escape(request_payload_str),
            'admin': general_role,
            'json': cgi.escape(json.dumps(json_data)),
            'error_msg': cgi.escape(error_msg or 'no error message'),
            'good_msg': good_msg or 'no good message',
        }
    else:
        if error_msg is None:
            return reservation_url
        else:
            return 'error:%s' % error_msg
Пример #46
0
def lms_admin_courses():
    if request.method == 'POST':
        if request.form['action'] == 'add':
            return redirect(url_for('lms_admin_external_courses'))
    db_lms = db_session.query(LMS).filter_by(lms_login=session['lms']).first()
    return render_template("lms_admin/courses.html", courses=db_lms.courses)
Пример #47
0
def _add_or_edit_rlms(rlmstype, rlmsversion, id):
    if not is_supported(rlmstype, rlmsversion):
        return "Not supported"

    rlms_version = _get_rlms_version(rlmstype, rlmsversion)
    if rlms_version is None:
        return render_template("labmanager_admin/rlms_errors.html")

    if id is not None:
        rlms = db_session.query(RLMS).filter_by(id=id).first()
        if rlms is None or rlms.rlms_version != rlms_version:
            return render_template("labmanager_admin/rlms_errors.html")

    AddForm = get_form_class(rlmstype, rlmsversion)
    form = AddForm(id is None)

    if form.validate_on_submit():
        configuration_dict = {}
        for field in form.get_field_names():
            if field not in ('location', 'name'):
                configuration_dict[field] = getattr(form, field).data

        configuration = json.dumps(configuration_dict)

        if id is None:
            new_rlms = RLMS(name=form.name.data,
                            location=form.location.data,
                            rlms_version=rlms_version,
                            configuration=configuration)
            db_session.add(new_rlms)
        else:
            rlms = db_session.query(RLMS).filter_by(id=id).first()
            if rlms is None:
                return render_template("labmanager_admin/rlms_errors.html")
            rlms.name = form.name.data
            rlms.location = form.location.data
            rlms.configuration = AddForm.process_configuration(
                rlms.configuration, configuration)

        db_session.commit()
        return redirect(
            url_for('admin_rlms_rlms',
                    rlmstype=rlmstype,
                    rlmsversion=rlmsversion))

    if id is not None:
        rlms = db_session.query(RLMS).filter_by(id=id).first()
        if rlms is None:
            return render_template("labmanager_admin/rlms_errors.html")

        form.name.data = rlms.name
        form.location.data = rlms.location
        if rlms.configuration is not None and rlms.configuration != '':
            configuration = json.loads(rlms.configuration)
            for key in configuration:
                getattr(form, key).data = configuration[key]

    return render_template("labmanager_admin/rlms_rlms_add.html",
                           rlmss=rlms_version.rlms,
                           name=rlms_version.rlms_type.name,
                           version=rlms_version.version,
                           form=form)
Пример #48
0
def admin_lms_edit(lms_login):
    lms = db_session.query(LMS).filter_by(lms_login = lms_login).first()
    if lms is None:
        return render_template("labmanager_admin/lms_errors.html")

    return _add_or_edit_lms(lms.id)
Пример #49
0
def check_lms_auth(lmsname, password):
    hash_password = hashlib.new("sha", password).hexdigest()
    lms = db_session.query(LMS).filter_by(lms_login=lmsname,
                                          lms_password=hash_password).first()
    g.lms = lmsname
    return lms is not None
Пример #50
0
def admin_lms_edit(lms_login):
    lms = db_session.query(LMS).filter_by(lms_login=lms_login).first()
    if lms is None:
        return render_template("labmanager_admin/lms_errors.html")

    return _add_or_edit_lms(lms.id)
Пример #51
0
def check_lms_auth(lmsname, password):
    hash_password = hashlib.new("sha", password).hexdigest()
    lms = db_session.query(LMS).filter_by(lms_login = lmsname, lms_password = hash_password).first()
    g.lms = lmsname
    return lms is not None
Пример #52
0
def admin_lms_authenticate_scorm(lms_login):
    db_lms = db_session.query(LMS).filter_by(lms_login=lms_login).first()
    if db_lms is None:
        return render_template("labmanager_admin/lms_errors.html")

    return get_authentication_scorm(db_lms.url)
Пример #53
0
def lms_admin_courses():
    if request.method == 'POST':
        if request.form['action'] == 'add':
            return redirect(url_for('lms_admin_external_courses'))
    db_lms = db_session.query(LMS).filter_by(lms_login = session['lms']).first()
    return render_template("lms_admin/courses.html", courses = db_lms.courses)
Пример #54
0
def admin_rlms_rlms_lab_edit_permissions_lms(rlmstype, rlmsversion, id, lab_id,
                                             lms_login):
    template_variables = {}

    lab, rlms = get_lab_and_lms(rlmstype, rlmsversion, id, lab_id)
    if lab is None or rlms is None:
        return render_template("labmanager_admin/rlms_errors.html")

    lms = db_session.query(LMS).filter_by(lms_login=lms_login).first()
    if lms is None:
        return render_template("labmanager_admin/rlms_errors.html")

    permission = db_session.query(PermissionOnLaboratory).filter_by(
        laboratory_id=lab_id, lms_id=lms.id).first()

    LmsPermissionsForm = get_lms_permissions_form_class(rlmstype, rlmsversion)
    form = LmsPermissionsForm()
    if form.validate_on_submit():
        configuration_dict = {}
        for field in form.get_field_names():
            if field != 'identifier':
                data = getattr(form, field).data
                if data != '':
                    configuration_dict[field] = data
        identifier = form.identifier.data

        configuration = json.dumps(configuration_dict)

        permission_with_same_identifier = db_session.query(
            PermissionOnLaboratory).filter_by(
                lms_id=lms.id, local_identifier=identifier).first()
        if permission_with_same_identifier is not None and permission_with_same_identifier != permission:
            flash(
                "Could not grant permission. The identifier %s was already used in the LMS %s for the laboratory %s. Choose other identifier."
                % (identifier, lms.name,
                   permission_with_same_identifier.laboratory.name))
            return render_template("labmanager_admin/rlms_errors.html")

        if permission is None:  # Not yet granted: add it
            permission = PermissionOnLaboratory(lms=lms,
                                                laboratory=lab,
                                                configuration=configuration,
                                                local_identifier=identifier)
            db_session.add(permission)
        else:  # Already granted: edit it
            permission.local_identifier = identifier
            permission.configuration = configuration

        db_session.commit()
        return redirect(
            url_for('admin_rlms_rlms_lab_edit_permissions',
                    rlmstype=rlmstype,
                    rlmsversion=rlmsversion,
                    id=id,
                    lab_id=lab_id))

    if permission is not None:
        configuration_dict = json.loads(permission.configuration or '{}')
        for field in configuration_dict:
            if hasattr(form, field):
                getattr(form, field).data = configuration_dict.get(field, '')
        form.identifier.data = permission.local_identifier

    template_variables['type_name'] = rlmstype
    template_variables['version'] = rlmsversion
    template_variables['rlms_name'] = rlms.name
    template_variables['rlms_id'] = id
    template_variables['lab_name'] = lab.name
    template_variables['lab_id'] = lab_id
    template_variables['lms_name'] = lms.name
    template_variables['add_or_edit'] = permission is None
    template_variables['form'] = form

    return render_template(
        "labmanager_admin/rlms_rlms_lab_edit_permissions_add.html",
        **template_variables)