Exemple #1
0
def v_report_benchmark_create(report_id=None):
    form = BenchmarkReportCreateForm()
    report_api = ReportApi()
    benchmark_report_api = BenchmarkReportApi()
    choices = [(r.id, r.title) for r in report_api.list()]
    form.report.choices = choices
    if report_id:
        form.report.data = report_id
    if request.method == 'POST' and form.validate_on_submit():
        benchmark_report_data = {
            'title': form.name.data,
            'report_id': form.report.data
        }
        try:
            new_benchmark_report = benchmark_report_api.create(benchmark_report_data)
        except DatabaseItemAlreadyExists as e:
            flash(_('This report already exists.'))
            return redirect(url_for('admin.v_report_benchmark_create'))
        except RequiredAttributeMissing as e:
            flash(_('A required form element was not submitted: {0}').format(e))
            return redirect(url_for('admin.v_report_benchmark_create'))
        except Exception as e:
            flash(_('An unexpected error occurred.'))
            return redirect(url_for('admin.v_report_benchmark_create'))
        else:
            flash('Benchmark created')
            return redirect(url_for('admin.v_report_benchmark_edit', benchmark_report_id=new_benchmark_report.id))
    else:
        return render_template('admin/report/benchmark/create.html', form=form, title=_('New benchmark'))
Exemple #2
0
def v_report_list():
    a_report = ReportApi()
    l_reports = a_report.list()
    return render_template('admin/report/list.html', reports=l_reports)
Exemple #3
0
def v_user_report_edit(user_id, user_report_id):
    if current_user.id != user_id:
        flash(_('You can only view your own reports.'))
        abort(403)
    form = UserReportCreateForm()
    report_api = ReportApi()
    form.report.choices = [(r.id, r.title) for r in report_api.list()]
    try:
        existing_user_report = user_report_api.read(user_report_id)
    except DatabaseItemDoesNotExist as e:
        flash(_('This report does not exist.'))
        return redirect(
            url_for('public.v_user_report_list_by_user',
                    user_id=current_user.id))
    except Exception as e:
        flash(_('An unexpected error occurred.'))
        return redirect(
            url_for('public.v_user_report_list_by_user',
                    user_id=current_user.id))
    if request.method == 'POST' and form.validate_on_submit():
        input_data = {
            'user_id': current_user.id,
            'report_id': form.report.data,
            'name': form.name.data
        }
        if input_data['report_id'] != existing_user_report.report_id:
            # We have to delete all the QuestionAnswer's, as they are no longer current
            for question_answer in existing_user_report.question_answers:
                qa_api = QuestionAnswerApi()
                try:
                    qa_api.delete(question_answer.id)
                except Exception as e:
                    flash(_('An unexpected error occurred.'))
                    return redirect(
                        url_for('public.v_user_report_list_by_user',
                                user_id=current_user.id))
        try:
            edited_user_report = user_report_api.update(
                existing_user_report.id, input_data)
        except RequiredAttributeMissing as e:
            flash(
                _('A required form element was not submitted: {0}').format(e))
            return redirect(
                url_for('public.v_user_report_edit',
                        user_id=current_user.id,
                        report_id=user_report_id))
        except Exception as e:
            flash(_('An unexpected error occurred.'))
            return redirect(
                url_for('public.v_user_report_list_by_user',
                        user_id=current_user.id))
        else:
            return redirect(
                url_for('public.v_user_report_section',
                        user_id=current_user.id,
                        user_report_id=edited_user_report.id,
                        section_id=edited_user_report.template.
                        ordered_sections[0].id))
    else:
        form.report.default = str(existing_user_report.report_id)
        form.name.default = existing_user_report.name
        form.process()
        return render_template('public/edit.html',
                               form=form,
                               title=_('Edit report'),
                               report_id=user_report_id)