Пример #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)
Пример #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()
Пример #3
0
def mx_admin_add_question():
    sid = mx_get_cookie('session_id')
    q_n = model.q_s_session_value(sid, 'q_n')[3]
    quiz_id = model.q_s_session_value(sid, 'quiz_id')[3]
    edit = int(model.q_s_session_value(sid, 'quiz_edit')[3]) == 1
    postfail = False
    q_text = ''; n_answers = ''; q_value = ''
    question_id = 0
    frm = MxForm()
    frm.add_field('text', 'Question is required',\
                    lambda x: x != None and len(x) < 1024)
    frm.add_field('n_answers', 'Number of answers is required',\
                    lambda x: x != None and int(x) < 256, int)
    frm.add_field('value', 'Value is required',\
                    lambda x: x != None and int(x) < 100, int)
    if edit:
        q_id, q_text, n_answers, q_value, q_image, qq_quiz_id\
            = model.q_s_quiz_question(quiz_id, q_n)

    rqm = mx_request_method()
    if rqm == 'POST':
        q_text = frm.get('text')
        n_answers = frm.get('n_answers')
        q_value = frm.get('value')
        if frm.is_valid():
            model.q_i_session_value(sid, 'n_answers', n_answers)
            model.q_i_session_value(sid, 'q_text', q_text)
            if edit:
                model.q_u_question(\
                    question_id, q_text, q_n, q_value, None)
                model.q_i_session_value(sid, 'question_id',\
                                        question_id)
                mx_redirect('/admin/edit_answer')
            else:
                question_id = model.q_i_question(\
                                quiz_id, q_text, q_n, q_value, None) 
                model.q_i_session_value(sid, 'question_id',\
                                        question_id)
                mx_redirect('/admin/add_answer')
        else:
            postfail = True
    if postfail or rqm == 'GET':
        quiz_name = model.q_s_quiz(quiz_id)[1]
        return mx_template('admin_add_quiz_B.tpl',\
                            quiz_name=quiz_name, q_n=q_n,\
                            errors=frm.errors,\
                            q_text=q_text,\
                            n_answers=n_answers,\
                            q_value=q_value,
                            edit=edit)