def add(): form = EditForm(request.form) courses = examination_service.find_all_courses() educations = examination_service.find_all_educations() form.course.choices = [(c.id, c.name) for c in courses] form.education.choices = [(e.id, e.name) for e in educations] form.test_type.choices = test_types.items() if form.validate_on_submit(): exam_file_data = request.files.get('examination', None) answer_file_data = request.files.get('answers', None) # Exam file is required if exam_file_data is not None and len(exam_file_data.name) > 0: exam_file = file_service.add_file(FileCategory.EXAMINATION, exam_file_data, exam_file_data.filename) else: flash(_('No examination uploaded.'), 'danger') return render_template('examination/edit.htm', courses=courses, educations=educations, form=form, test_types=test_types, new_exam=True) # Answer file is optional if answer_file_data is not None and len(answer_file_data.name) > 0: answers_file = file_service.add_file(FileCategory.EXAMINATION, answer_file_data, answer_file_data.filename) else: answers_file = None flash(_('No answers uploaded.'), 'warning') examination_service.add_examination( exam_file, form.date.data, form.comment.data, form.course.data, form.education.data, form.test_type.data, answers_file) flash(_('Examination successfully uploaded.'), 'success') return redirect(url_for('examination.view_examination')) return render_template('examination/edit.htm', courses=courses, educations=educations, form=form, test_types=test_types, new_exam=True)
def edit(exam_id): if not ModuleAPI.can_write('examination', True): session['prev'] = 'examination.edit_examination' return abort(403) exam = Examination.query.get(exam_id) if not exam: flash(_('Examination could not be found.'), 'danger') return redirect(url_for('examination.view_examination')) session['examination_edit_id'] = exam_id form = EditForm(request.form, exam) courses = Course.query.order_by(Course.name).all() educations = Education.query.order_by(Education.name).all() form.course.choices = [(c.id, c.name) for c in courses] form.education.choices = [(e.id, e.name) for e in educations] form.test_type.choices = test_types.items() if request.method == 'POST': if form.validate_on_submit(): file = request.files['examination'] answers = request.files['answers'] exam.date = form.date.data exam.comment = form.comment.data exam.course_id = form.course.data exam.education_id = form.education.data exam.test_type = form.test_type.data new_path = upload_file_real(file, exam.path) if new_path: exam.path = new_path elif new_path is None: flash(_('Wrong format examination.'), 'danger') if not new_path: flash(_('Old examination preserved.'), 'info') new_answer_path = upload_file_real(answers, exam.answer_path) if new_answer_path: exam.answer_path = new_answer_path elif new_answer_path is None: flash(_('Wrong format answers.'), 'danger') if not new_answer_path: flash(_('Old answers preserved.'), 'info') db.session.commit() flash(_('Examination succesfully changed.'), 'success') return redirect(url_for('examination.edit', exam_id=exam_id)) else: flash_form_errors(form) path = '/static/uploads/examinations/' return render_template('examination/edit.htm', form=form, path=path, examination=exam, title=_('Examinations'), courses=courses, educations=educations, new_exam=False)
def add(): if not ModuleAPI.can_write('examination', True): session['prev'] = 'examination.add' return abort(403) form = EditForm(request.form, ) courses = Course.query.order_by(Course.name).all() educations = Education.query.order_by(Education.name).all() form.course.choices = [(c.id, c.name) for c in courses] form.education.choices = [(e.id, e.name) for e in educations] form.test_type.choices = test_types.items() if request.method == 'POST': if form.validate_on_submit(): file = request.files.get('examination', None) answers = request.files.get('answers', None) error = False filename = upload_file_real(file) if file: if not filename: flash(_('Wrong format examination.'), 'danger') error = True answer_path = upload_file_real(answers) if answer_path is False: flash(_('No answers uploaded.'), 'warning') answer_path = None elif answer_path is None: flash(_('Wrong format answers.'), 'danger') error = True else: flash(_('No examination uploaded.'), 'danger') error = True if error: dummy_exam = Examination(filename, form.date.data, form.comment.data, form.course.data, form.education.data, test_type=form.test_type.data) return render_template('examination/edit.htm', courses=courses, educations=educations, examination=dummy_exam, form=form, test_types=test_types, new_exam=False) exam = Examination(filename, form.date.data, form.comment.data, form.course.data, form.education.data, answers=answer_path, test_type=form.test_type.data) db.session.add(exam) db.session.commit() flash(_('Examination successfully uploaded.'), 'success') return redirect(url_for('examination.edit', exam_id=exam.id)) else: flash_form_errors(form) return render_template('examination/edit.htm', courses=courses, educations=educations, new_exam=True, form=form)