Ejemplo n.º 1
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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
 def index(self):
     basic_http_authentications = current_user.lt.basic_http_authentications
     if not basic_http_authentications:
         message = gettext("No authentication is configured in your LMS. If you are not using the Basic HTTP system (e.g., you're using LTI), don't worry. Otherwise, contact the Labmanager administrator.")
         return self.render("lms_admin/discover-errors.html", message = message)
     basic_http_authentication = basic_http_authentications[0]
     q     = request.args.get('q','')
     try:
         start = int(request.args.get('start','0'))
     except:
         start = 0
     user     = basic_http_authentication.labmanager_login
     password = basic_http_authentication.labmanager_password
     url = "%s?q=%s&start=%s" % (basic_http_authentication.lt_url, q, start)
     VISIBLE_PAGES = 10
     results = retrieve_courses(url, user, password)
     if isinstance(results, basestring):
         message = gettext("Invalid JSON provided or could not connect to the LMS. Look at the logs for more information")
         return self.render("lms_admin/discover-errors.html", message = message)
     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()
         message = gettext("Malformed data retrieved. Look at the logs for more information")
         return self.render('lms_admin/discover-errors.html', message = message)
     if request.method == 'POST':
         courses_to_manage = []
         for key in request.form:
             if key.startswith('course-'):
                 courses_to_manage.append(key[len('course-'):])
         existing_courses = self.session.query(Course).filter(Course.context_id.in_(courses_to_manage), Course.lt == current_user.lt).all()
         existing_course_ids = [ existing_course.context_id for existing_course in existing_courses ]
         if request.form['action'] == 'add':
             for course_to_manage in courses_to_manage:
                 print course_to_manage not in existing_course_ids and course_to_manage in course_dict
                 if course_to_manage not in existing_course_ids and course_to_manage in course_dict:
                     new_course = Course(name = course_dict[course_to_manage], lt = current_user.lt, context_id = course_to_manage)
                     self.session.add(new_course)
         elif request.form['action'] == 'delete':
             for course_to_manage in courses_to_manage:
                 if course_to_manage in existing_course_ids:
                     existing_course = self.session.query(Course).filter(Course.context_id == course_to_manage, Course.lt == current_user.lt).first()
                     if existing_course:
                         self.session.delete(existing_course)
         else:
             return self.render('lms_admin/discover-errors.html', message = gettext("Invalid action found (add or delete expected)"))
         self.session.commit()
     existing_courses = self.session.query(Course).filter(Course.context_id.in_(course_dict.keys()), Course.lt == current_user.lt).all()
     existing_course_ids = [ existing_course.context_id for existing_course in existing_courses ]
     return self.render("lms_admin/discover.html", current_page = current_page, current_pages = current_pages, max_page = max_page, q = q, start = start, courses = courses, per_page = per_page, max_position = number - VISIBLE_PAGES, max_position_page = (number - VISIBLE_PAGES) / VISIBLE_PAGES, existing_course_ids = existing_course_ids )
Ejemplo n.º 5
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)