def test_create_qtemplate(self): """ Test qtemplates creation """ qt1_id = DB.create_qt(1, "TESTQ1", "Test question 1", 0, 5.0, 1) qt2_id = DB.create_qt(1, "TESTQ2", "Test question 2", 0, 4.1, 2) self.assertIsInstance(qt1_id, int) self.assertIsInstance(qt2_id, int) qt1 = DB.get_qtemplate(qt1_id) qt2 = DB.get_qtemplate(qt2_id) self.assertEqual(qt1['title'], "TESTQ1") self.assertEqual(qt2['title'], "TESTQ2") self.assertEqual(qt1['description'], "Test question 1") self.assertEqual(qt2['description'], "Test question 2") course_id = Courses.create("TEST107", "Test create qtemplate", 1, 1) topic1_id = Topics.create(course_id, "TESTTOPIC9", 1, 2) qt3_id = DB.create_qt(1, "TESTQ3", "Test question 3", 0, 5.0, 1, topic1_id) self.assertIsInstance(qt3_id, int) qt3 = DB.get_qtemplate(qt3_id) self.assertEqual(qt3['title'], "TESTQ3") self.assertEqual(qt3['description'], "Test question 3") self.assertEqual(DB.get_topic_for_qtemplate(qt3_id), topic1_id)
def practice_do_question(topic_id, qt_id): """ Show them a question and allow them to fill in some answers """ user_id = session['user_id'] try: course_id = Topics.get_course_id(topic_id) except KeyError: course_id = None abort(404) try: course = Courses2.get_course(course_id) except KeyError: course = None abort(404) topictitle = "UNKNOWN" try: topictitle = Topics.get_name(topic_id) except KeyError: abort(404) try: qtemplate = DB.get_qtemplate(qt_id) except KeyError: qtemplate = None abort(404) questions = Practice.get_sorted_questions(course_id, topic_id, user_id) q_title = qtemplate['title'] q_pos = DB.get_qtemplate_topic_pos(qt_id, topic_id) blocked = Practice.is_q_blocked(user_id, course_id, topic_id, qt_id) if blocked: return render_template( "practicequestionblocked.html", mesg=blocked, topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) try: q_id = Practice.get_practice_q(qt_id, user_id) except (ValueError, TypeError), err: log(ERROR, "ERROR 1001 (%s,%s) %s" % (qt_id, user_id, err)) return render_template( "practicequestionerror.html", mesg="Error generating question.", topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos="?", )
def cadmin_view_qtemplate_history(course_id, topic_id, qt_id): """ Show the practice history of the question template. """ if not course_id: abort(404) course = Courses2.get_course(course_id) topic = {"id": topic_id, "position": Topics.get_pos(topic_id), "name": Topics.get_name(topic_id)} qtemplate = DB.get_qtemplate(qt_id) year = datetime.now().year years = range(year, year - 6, -1) return render_template( "courseadmin_viewqtemplate.html", course=course, topic=topic, qtemplate=qtemplate, years=years )
def cadmin_view_qtemplate_history(course_id, topic_id, qt_id): """ Show the practice history of the question template. """ if not course_id: abort(404) course = Courses2.get_course(course_id) topic = { 'id': topic_id, 'position': Topics.get_pos(topic_id), 'name': Topics.get_name(topic_id) } qtemplate = DB.get_qtemplate(qt_id) year = datetime.now().year years = range(year, year - 6, -1) return render_template("courseadmin_viewqtemplate.html", course=course, topic=topic, qtemplate=qtemplate, years=years)
def qts_to_zip(qt_ids, fname="oa_export", extra_info = None): """ Take a list of QTemplate IDs and return a binary string containing them as an .oaq file. (a zip file in special format) """ fname = os.path.basename(fname) qdir = tempfile.mkdtemp(prefix="oa") info = { 'oasis': { 'oa_version': "3.9.4", 'qt_version': '0.9', 'url': OaConfig.parentURL }, 'qtemplates': {}, 'extra': extra_info } arc = zipfile.ZipFile(os.path.join(qdir, "oasisqe.zip"), 'w', zipfile.ZIP_DEFLATED) for qt_id in qt_ids: qtemplate = DB.get_qtemplate(qt_id) qtdir = os.path.join(qdir, str(qt_id)) attachments = DB.get_qt_atts(qt_id) if not 'qtemplate.html' in attachments: attachments.append('qtemplate.html') if not 'datfile.txt' in attachments: attachments.append('datfile.txt') if not 'image.gif' in attachments: attachments.append('image.gif') os.mkdir(qtdir) os.mkdir(os.path.join(qtdir, "attach")) info["qtemplates"][qt_id] = {'qtemplate': qtemplate} info["qtemplates"][qt_id]["attachments"] = [] for name in attachments: if not name: name='UNKNOWN' mtype = DB.get_qt_att_mimetype(qt_id, name) if not mtype: mtype = "" data = DB.get_qt_att(qt_id, name) if not data: data = "" info["qtemplates"][qt_id]["attachments"].append([name, mtype, len(data)]) subdir = os.path.join(qtdir, "attach", name) outf = open(subdir, "wb") outf.write(data) outf.close() arc.write(subdir, os.path.join("%s" % qt_id, "attach", name), zipfile.ZIP_DEFLATED) infof = open(os.path.join(qdir, "info.json"), "wb") infof.write(json.dumps(info)) infof.close() arc.write(os.path.join(qdir, "info.json"), os.path.join("info.json"), zipfile.ZIP_DEFLATED) arc.close() readback = open(os.path.join(qdir, "oasisqe.zip"), "rb") data = readback.read() readback.close() shutil.rmtree(qdir) return data
def practice_do_question_id(topic_id, qt_id): """ Show them a question and allow them to fill in some answers """ user_id = session['user_id'] try: course_id = Topics.get_course_id(topic_id) except KeyError: course_id = None abort(404) try: course = Courses2.get_course(course_id) except KeyError: course = None abort(404) topictitle = "UNKNOWN" try: topictitle = Topics.get_name(topic_id) except KeyError: abort(404) qtemplate = DB.get_qtemplate(qt_id) questions = Practice.get_sorted_questions(course_id, topic_id, user_id) q_title = qtemplate['title'] q_pos = DB.get_qtemplate_topic_pos(qt_id, topic_id) blocked = Practice.is_q_blocked(user_id, course_id, topic_id, qt_id) if blocked: return render_template( "practicequestionblocked.html", mesg=blocked, topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) try: q_id = Practice.get_practice_q(qt_id, user_id) except (ValueError, TypeError) as err: L.error("ERROR 1001 (%s,%s) %s" % (qt_id, user_id, err)) return render_template( "practicequestionerror.html", mesg="Error generating question.", topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) if not q_id > 0: L.error("ERROR 1002 (%s,%s) Question not generated" % (qt_id, user_id)) return render_template( "practicequestionerror.html", mesg="Error generating question.", topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) q_body = General.render_q_html(q_id) q_body = q_body.replace(r"\240", u" ") # TODO: why is this here? return render_template( "practicedoquestion.html", q_body=q_body, topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, q_id=q_id, )
def practice_do_question(topic_id, position): """ Show them a question and allow them to fill in some answers """ user_id = session['user_id'] try: course_id = Topics.get_course_id(topic_id) except KeyError: course_id = None abort(404) try: course = Courses2.get_course(course_id) except KeyError: course = None abort(404) topictitle = "UNKNOWN" try: topictitle = Topics.get_name(topic_id) except KeyError: abort(404) try: choices = DB.get_qtemplates_in_topic_position(topic_id, position) except KeyError: choices = None abort(404) if len(choices) == 1: qt_id = choices[0] elif len(choices) > 1: L.debug("Practice choosing random from: %s" % repr(choices)) qt_id = random.choice(choices) else: L.warn("Access to non existent practice topic %s question %s" % (topic_id, position)) return render_template( "practicequestionerror.html", mesg="Error accessing question.", topic_id=topic_id, course=course, q_pos=position ) qtemplate = DB.get_qtemplate(qt_id) questions = Practice.get_sorted_questions(course_id, topic_id, user_id) q_title = qtemplate['title'] q_pos = DB.get_qtemplate_topic_pos(qt_id, topic_id) blocked = Practice.is_q_blocked(user_id, course_id, topic_id, qt_id) if blocked: return render_template( "practicequestionblocked.html", mesg=blocked, topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) try: q_id = Practice.get_practice_q(qt_id, user_id) except (ValueError, TypeError) as err: L.error("ERROR 1001 (%s,%s) %s" % (qt_id, user_id, err)) return render_template( "practicequestionerror.html", mesg="Error generating question.", topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) if not q_id > 0: L.error("ERROR 1002 (%s,%s) Question not generated" % (qt_id, user_id)) return render_template( "practicequestionerror.html", mesg="Error generating question.", topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) q_body = General.render_q_html(q_id) q_body = q_body.replace(r"\240", u" ") # TODO: why is this here? return render_template( "practicedoquestion.html", q_body=q_body, topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, q_id=q_id, )
def qts_to_zip(qt_ids, extra_info=None): """ Take a list of QTemplate IDs and return a binary string containing them as an .oaq file. (a zip file in special format) """ qdir = tempfile.mkdtemp(prefix="oa") info = { 'oasis': { 'oa_version': "3.9.4", 'qt_version': '0.9', 'url': OaConfig.parentURL }, 'qtemplates': {}, 'extra': extra_info } arc = zipfile.ZipFile(os.path.join(qdir, "oasisqe.zip"), 'w', zipfile.ZIP_DEFLATED) for qt_id in qt_ids: qtemplate = DB.get_qtemplate(qt_id) qtdir = os.path.join(qdir, str(qt_id)) attachments = DB.get_qt_atts(qt_id) if 'qtemplate.html' not in attachments: attachments.append('qtemplate.html') if 'datfile.txt' not in attachments: attachments.append('datfile.txt') if 'image.gif' not in attachments: attachments.append('image.gif') os.mkdir(qtdir) os.mkdir(os.path.join(qtdir, "attach")) info["qtemplates"][qt_id] = {'qtemplate': qtemplate} info["qtemplates"][qt_id]["attachments"] = [] for name in attachments: if not name: name = 'UNKNOWN' mtype = DB.get_qt_att_mimetype(qt_id, name) if not mtype: mtype = "" data = DB.get_qt_att(qt_id, name) if not data: data = "" info["qtemplates"][qt_id]["attachments"].append([name, mtype, len(data)]) subdir = os.path.join(qtdir, "attach", name) outf = open(subdir, "wb") outf.write(data) outf.close() arc.write(subdir, os.path.join("%s" % qt_id, "attach", name), zipfile.ZIP_DEFLATED) infof = open(os.path.join(qdir, "info.json"), "wb") infof.write(json.dumps(info)) infof.close() arc.write(os.path.join(qdir, "info.json"), os.path.join("info.json"), zipfile.ZIP_DEFLATED) arc.close() readback = open(os.path.join(qdir, "oasisqe.zip"), "rb") data = readback.read() readback.close() shutil.rmtree(qdir) return data