def edit(summary_id): if not ModuleAPI.can_write('summary', True): session['prev'] = 'summary.edit_summary' return abort(403) summary = Summary.query.get(summary_id) if not summary: flash(_('Summary could not be found.'), 'danger') return redirect(url_for('summary.view')) session['summary_edit_id'] = summary_id form = EditForm(request.form, summary) 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] if request.method == 'POST': if form.validate_on_submit(): file = request.files['summary'] summary.title = form.title.data summary.course_id = form.course.data summary.education_id = form.education.data summary.date = form.date.data new_path = upload_file_real(file, summary.path) if new_path: summary.path = new_path elif new_path is None: flash(_('Wrong format summary.'), 'danger') if not new_path: flash(_('Old summary preserved.'), 'info') db.session.commit() flash(_('Summary succesfully changed.'), 'success') return redirect(url_for('summary.edit', summary_id=summary_id)) else: flash_form_errors(form) path = '/static/uploads/summaries/' return render_template( 'summary/edit.htm', path=path, summary=summary, courses=courses, educations=educations, new_summary=False, form=form)
def add(): if not ModuleAPI.can_write('summary', True): session['prev'] = 'summary.edit_summary' 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] path = '/static/uploads/summaries/' if request.method == 'POST': if form.validate_on_submit(): file = request.files['summary'] new_path = upload_file_real(file) if new_path: summary = Summary(form.title.data, new_path, form.date.data, form.course.data, form.education.data) db.session.add(summary) db.session.commit() flash(_('Summary uploaded succesfully.'), 'success') elif new_path is None: flash(_('Wrong format summary.'), 'danger') if not new_path: flash(_('Summary required.'), 'danger') # TODO: Fix dummy summary to show previous information on new # page return render_template( 'summary/edit.htm', path=path, courses=courses, educations=educations, new_summary=True, form=form) return redirect(url_for('summary.edit', summary_id=summary.id)) else: flash_form_errors(form) return render_template( 'summary/edit.htm', path=path, courses=courses, educations=educations, new_summary=True, form=form)