def generate_paper(course_id):
    paper_template = json_url.loads(session["paper_template"])
    conflicting_questions = []
    for qtype in paper_template:
        for question in paper_template[qtype]:
            for subquestion, constraints in paper_template[qtype][
                    question].items():
                constraints["cognitive"] = CognitiveEnum.from_string(
                    constraints["cognitive"])
                constraints["difficulty"] = DifficultyEnum.from_string(
                    constraints["difficulty"])
                constraints["question_type"] = QuestionTypeEnum.from_string(
                    qtype)
                conflicting_questions.extend(
                    find_conflicting_questions(course_id, constraints))
                paper_template[qtype][question][subquestion] = constraints
    form = PaperLogoForm()
    if form.validate_on_submit():
        paper_data = {}
        if form.picture.data:
            paper_data["paper_logo"] = save_logo(form.picture.data)
        paper_data["name"] = form.name.data
        paper_data["term"] = form.term.data
        paper_data["mark"] = json_url.loads(session["total_marks"])
        paper_data["exam_date"] = form.exam_date.data
        paper_data["time_limit"] = form.time_limit.data
        paper_data["course_id"] = course_id
        paper_data["paper_format"] = defaultdict(lambda: defaultdict(dict))
        for qtype in paper_template:
            for question in paper_template[qtype]:
                for subquestion, constraints in paper_template[qtype][
                        question].items():
                    try:
                        paper_data["paper_format"][qtype][question].update({
                            subquestion:
                            find_random_question(course_id, constraints)
                        })

                    except QuestionNotFoundError:
                        flash(
                            "Question that satisfies all given constraints doesn't exist in database."
                        )
                        return redirect(url_for("papers.home"))
        paper_data["paper_format"] = dict(paper_data["paper_format"])
        paper = Paper(**paper_data)
        db.session.add(paper)
        db.session.commit()
        return redirect(
            url_for("papers.confirm_generated_paper", paper_id=paper.id))
    return render_template(
        "papers/generate_paper.html",
        conflicting_questions=conflicting_questions,
        course_id=course_id,
        form=form,
    )
 def wrapper(*args, **kwargs):
     for session_key in session_keys:
         data = session.get(session_key, None)
         if not data:
             return redirect(
                 url_for("papers.paper_generate_request",
                         course_id=kwargs["course_id"]))
         try:
             json_url.loads(data)
         except BadSignature:
             abort(406)
     return func(*args, **kwargs)
def mark_distribution_form(course_id):
    """Mark distribution of form
    Args:
        course_id (int): Course ID of course
    Returns:
        HTML: Go to mark distribution form page
    """
    total_marks = json_url.loads(session["total_marks"])
    no_of_subquestions = json_url.loads(session["no_of_subquestions"])
    form = MarkDistributionForm(course_id, no_of_subquestions, total_marks)
    if form.validate_on_submit():
        question_no = list(
            itertools.chain(*map(
                lambda x: list(itertools.repeat(x[0] + 1, x[1])),
                enumerate(no_of_subquestions),
            )))
        raw_template = QPTGenerator(dict(form.data), question_no).generate()
        paper_template = defaultdict(lambda: defaultdict(dict))
        subque_counter = Counter()
        que_counter = defaultdict(dict)
        for i in range(len(raw_template["question_no"])):
            data = dict(
                mark=raw_template["question"][i],
                cognitive=CognitiveEnum(raw_template["cognitive"][i]).name,
                difficulty=DifficultyEnum(raw_template["difficulty"][i]).name,
                unit=raw_template["unit"][i],
            )
            question_type = QuestionTypeEnum(
                raw_template["question_type"][i]).name
            if raw_template["question_no"][i] not in que_counter[
                    question_type]:
                que_counter[question_type][raw_template["question_no"][i]] = (
                    len(que_counter[question_type]) + 1)
            current_que = que_counter[question_type][
                raw_template["question_no"][i]]
            current_subque = ascii_lowercase[subque_counter[(question_type,
                                                             current_que)]]
            paper_template[question_type][current_que][current_subque] = data
            subque_counter[(question_type, current_que)] += 1
        session["paper_template"] = json_url.dumps(dict(paper_template))
        return redirect(
            url_for("papers.confirm_paper_template", course_id=course_id))
    return render_template("papers/mark_distribution_form.html",
                           form=form,
                           title="Mark Distribution")
def confirm_paper_template(course_id):
    if request.method == "POST":
        if request.get_json():
            return redirect(
                url_for("papers.generate_paper", course_id=course_id))
        flash("Form can't be empty!")
    paper_template = json_url.loads(session["paper_template"])
    return render_template(
        "papers/confirm_paper_template.html",
        course_id=course_id,
        paper_template=paper_template,
        css_files=["css/papers/confirm_paper_template.css"],
        js_files=["js/papers/confirm_paper_template.js"],
        image_file=profile_path(),
        title="Mark Distribution",
    )
示例#5
0
def mark_distribution_form(course_id, data):
    """Mark distribution for given course id

    Args:
        course_id (int): Id for valid course
        data (object): description of marks distribution

    Returns:
        HTML: If valid form then reder to marks distribution page else 
    """
    if not data:
        return redirect(
            url_for("papers.paper_generate_request", course_id=course_id))
    data = json_url.loads(data)
    form = MarkDistributionForm(course_id, data["questions"],
                                data["total_marks"])
    if form.validate_on_submit():
        return jsonify(form.data)
    return render_template("papers/mark_distribution_form.html", form=form)