Example #1
0
def mx_admin_add_yaml_quiz():
    import yaml
    frm = MxForm()
    frm.add_field('quiz_yaml', 'Valid YAML is required',\
                  lambda x: x != None)
    postfail = False
    rqm = mx_request_method()
    if rqm == 'POST':
        y = frm.get('quiz_yaml')
        if frm.is_valid():
            try:
                ymap = yaml.safe_load(y)
                quiz = ymap['quiz']
                name = quiz['name'].strip()
                questions = quiz['questions']
                quiz_id = model.q_i_quiz(name, len(questions))
                c = 1 
                for q in questions:
                    question_id = model.q_i_question(\
                            quiz_id, q['text'].strip(), c, q['value'])
                    answers = q['answers']
                    for a in answers:
                        model.q_i_answer(question_id, a['text'], a['correct'])
                    c += 1
                return mx_template('admin_success.tpl',\
                                        message='Quiz added successfully!')
            except (KeyError, yaml.YAMLError):
                postfail = True
        else:
            postfail = True
    if postfail or rqm == 'GET': 
        return mx_template('admin_add_yaml_quiz.tpl',\
                            failed=postfail, errors=frm.errors)
Example #2
0
def mx_import_yaml():
    global YAML_FILES

    model.G_db = sqlite3.connect("mixopterus.db")
    for yf in YAML_FILES:
        f = open(yf)
        y = f.read()
        if y != None:
            y = y.strip()
            ymap = yaml.safe_load(y)
            try:
                quiz = ymap["quiz"]
                name = quiz["name"].strip()
                questions = quiz["questions"]
                quiz_id = model.q_i_quiz(name, len(questions))
                c = 1
                for q in questions:
                    question_id = model.q_i_question(quiz_id, q["text"].strip(), c, q["value"])
                    answers = q["answers"]
                    for a in answers:
                        model.q_i_answer(question_id, a["text"], a["correct"])
                    c += 1
            except KeyError:
                print "Skipping malformed YAML file: %s" % (yf)
                continue
        f.close()
Example #3
0
def mx_admin_add_quiz(quiz_id=None):
    edit = False; name = ''; length = '' 
    frm = MxForm()
    rqm = mx_request_method()
    postfail = False    
    frm.add_field('name', 'Name is required',\
                    lambda x: x != None and len(x) < 255)
    frm.add_field('length', 'Length is required',\
                    lambda x: x != None and x.isdigit() and int(x) < 100, int)

    if quiz_id != None:
        quiz_id, name, length = model.q_s_quiz(quiz_id)
        edit = True
    if rqm == 'POST':
        name = frm.get('name')
        length = frm.get('length')
        if frm.is_valid():
            sid = mx_get_cookie('session_id')
            model.q_i_session_value(sid, 'quiz_id', quiz_id)
            model.q_i_session_value(sid, 'q_n', 1)
            if edit:
                model.q_u_quiz(quiz_id, name, length) 
                model.q_i_session_value(sid, 'quiz_edit', 1)
                mx_redirect('/admin/edit_question')
            else:
                quiz_id = model.q_i_quiz(name, length)
                model.q_i_session_value(sid, 'quiz_edit', 0)
                mx_redirect('/admin/add_question')
        else:
            postfail = True
    if postfail or rqm == 'GET':
        return mx_template('admin_add_quiz_A.tpl', errors=frm.errors, name = name, length = length, quiz_id = quiz_id)