def send_assessment(): user_id = int(session["userid"]) cur_week = int(request.form.get('week_id')) assqt_id = int(request.form.get('assqt_id')) answers_cou = request.form.get('answers_cou') _aq = Assqt.query.get(assqt_id) if _aq: for _c in _aq.checked_by: if _c.id == user_id: flash("You've already assessed this assignment") return redirect('/') for i in range(1, int(answers_cou) + 1): _a = Assessment() _a.answer = Answer.query.get( int(request.form.get('ans_id_{}'.format(i)))) if _a.answer.user.id == user_id: flash("It's impossible to assess your own assignment") return redirect('/') _a.assessed_by = User.query.get(int(session["userid"])) _a.score = request.form.get('rg2_{}'.format(i)) _a.descr = request.form.get('ans_desc_{}'.format(i)) db.session.add(_a) if _aq: _aq.checked_by.append(User.query.get(user_id)) _aq.checks_left -= 1 for lock in _aq.alocks: if lock.user_id == user_id: db.session.delete(lock) if _aq.checks_left == 0: db.session.delete(_aq) _ck = Check.query.filter( and_(Check.user_id == user_id, Check.week_id == cur_week)).first() if _ck is None: _ck = Check() _ck.user = User.query.get(user_id) _ck.week = Week.query.get(cur_week) _ck.checks_count = 0 db.session.add(_ck) _ck.checks_count += 1 if _ck.checks_count == checks_by_user: _ck.is_all_done = True db.session.commit() return redirect('/')
def adminAssessment(): if request.method == "POST": # Add new assessment to db newAssessment = Assessment(category=request.form['assessmentName']) db.session.add(newAssessment) number_of_question = 0 questions_from_form = [] for key in request.form.keys(): # Add questions to db if 'question' in key: newQuestion = request.form[key] questions_from_form.append(newQuestion) number_of_question += 1 # Get the scores scores = [] for value in request.form.getlist('score'): scores.append(value) # Get the answers answers = [] for value in request.form.getlist('answer'): answers.append(value) a1, a2, a3, a4 = 0, 1, 2, 3 for i in range(number_of_question): # Add answers and correct answers to db answer = Answers(question=questions_from_form[i], answer1=answers[a1], answer2=answers[a2], answer3=answers[a3], answer4=answers[a4], correctAnswer=answers[a4], mark=scores[i]) db.session.add(answer) # Set the values for DB newAssessment.answer.append(answer) a1 += 4 a2 += 4 a3 += 4 a4 += 4 # Sum up scores to get the final result result = sum([int(i) for i in scores]) newAssessment.score = result db.session.commit() assessments = Assessment.query.order_by( Assessment.dateCreated.desc())[:10] category = [] for assessment in assessments: category.append(assessment.category) return render_template("adminAssessment.html", page='admin', assessmentLen=len(assessments), assessment=category) else: assessments = Assessment.query.order_by( Assessment.dateCreated.desc())[:10] category = [] for assessment in assessments: category.append(assessment.category) if assessments is not None: return render_template("adminAssessment.html", page='admin', assessmentLen=len(assessments), assessment=category) else: return render_template("adminAssessment.html", page='admin', assessmentLen=0)