Пример #1
0
def v_report_create():
    form = ReportCreateForm()
    form.lang.choices = [(l.id, l.lang) for l in lang_api.list()]
    if request.method == 'POST' and form.validate_on_submit():
        a_report = ReportApi()
        input_data = {
            'title': form.title.data,
            'lang_id': form.lang.data
        }
        try:
            new_report = a_report.create(input_data)
        except DatabaseItemAlreadyExists as e:
            flash(_('A report called {0} already exists.').format(input_data['title']))
            return render_template('admin/report/create.html', form=form)
        except RequiredAttributeMissing as e:
            flash(_('A required form element was not submitted: {0}').format(e))
            return render_template('admin/report/create.html', form=form)
        except Exception as e:  # Remove this after debugging
            #    flash('An unexpected error occurred: {0}'.format(e))
            flash(_('An unexpected error occurred.'))
            return render_template('admin/report/create.html', form=form)
        else:
            flash(_('Report created.'))
            return redirect(url_for('admin.v_report_edit', report_id=new_report.id))
    return render_template('admin/report/create.html', form=form)
Пример #2
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'))
Пример #3
0
 def test_delete(self):
     en = LangApi().by_lang('en')
     r = ReportApi().create({'title': 'Test', 'lang_id': en.id})
     s = SectionApi().create({'title': 'Test', 'report_id': r.id})
     q = QuestionApi().create({'question': 'Test', 'weight': 1, 'section_id': s.id})
     assert QuestionApi().delete(q.id) is True
     assert q not in scoremodel.db.session
Пример #4
0
def v_report_edit(report_id):
    a_report = ReportApi()
    a_answer = AnswerApi()
    a_risk_factor = RiskFactorApi()
    a_lang = LangApi()
    try:
        existing_report = a_report.read(report_id)
    except DatabaseItemDoesNotExist:
        flash(_('No report with id {0} exists.').format(report_id))
        return redirect(url_for('admin.v_report_list'))
    # Do not use fallback_locale for the answers and risk_factor choices: if they don't exist, the administrator
    # must create them.
    report_lang = a_lang.read(existing_report.lang_id)
    return render_template('admin/report/edit.html', report=existing_report,
                           all_risk_factors=a_risk_factor.by_lang(report_lang.lang),
                           all_answers=a_answer.by_lang(report_lang.lang), languages=lang_api.list())
Пример #5
0
def v_user_report_summary(user_id, user_report_id):
    if current_user.id != user_id:
        flash(_('You can only view your own reports.'))
        abort(403)
    user_report = user_report_api.read(user_report_id)

    question_answers = {}
    all_scores = {}

    for section in user_report.template.sections:
        all_scores[section.id] = 0

    for question_answer in user_report.question_answers:
        question_answers[question_answer.question_id] = question_answer
        multiplication_factor = SectionApi().multiplication_factor(
            question_answer.question_template.section_id)
        all_scores[question_answer.question_template.section.id] += question_answer.score * \
                                                                    multiplication_factor

    highest_unanswered = []

    for question in ReportApi().questions_by_combined_weight(
            user_report.template.id):
        if question['question_id'] not in question_answers or question_answers[question['question_id']].score < \
                question['max_score']:
            try:
                highest_unanswered.append(QuestionApi().read(
                    question['question_id']))
            except DatabaseItemDoesNotExist:
                pass

    if len(highest_unanswered) >= 5:
        visible_unanswered = highest_unanswered[:5]
    else:
        visible_unanswered = highest_unanswered

    benchmarks_by_question = {}
    for bm_r in user_report.template.benchmark_reports:
        for bm in bm_r.benchmarks:
            if bm.question_id in benchmarks_by_question:
                benchmarks_by_question[bm.question_id].append(bm)
            else:
                benchmarks_by_question[bm.question_id] = [bm]

    # Create a color-range for the risk_factors
    risk_factors = [r.risk_factor for r in RiskFactorApi().list()]
    colored_risk_factors = Color().range(risk_factors)

    return render_template(
        'public/summary.html',
        report_template=user_report.template,
        user_report=user_report,
        user_report_creation_time='{:%Y-%m-%d %H:%M:%S}'.format(
            user_report.creation_time),
        highest_unanswered=visible_unanswered,
        benchmarks_by_question=benchmarks_by_question,
        question_answers_by_id=question_answers,
        colored_risk_factors=colored_risk_factors)
Пример #6
0
 def test_update(self):
     en = LangApi().by_lang('en')
     r = ReportApi().create({'title': 'Test', 'lang_id': en.id})
     s = SectionApi().create({'title': 'Test', 'report_id': r.id})
     q = QuestionApi().create({'question': 'Test', 'weight': 1, 'section_id': s.id})
     q_d = QuestionApi().update(q.id, {'question': 'Foo', 'weight': 1, 'section_id': s.id})
     assert q_d == QuestionApi().read(q.id)
     assert QuestionApi().read(q.id).question == 'Foo'
     self.assertIsInstance(q_d, Question)
Пример #7
0
 def test_create(self):
     en = LangApi().by_lang('en')
     r = ReportApi().create({'title': 'Test', 'lang_id': en.id})
     s = SectionApi().create({'title': 'Test', 'report_id': r.id})
     q = QuestionApi().create({'question': 'Test', 'weight': 1, 'section_id': s.id})
     assert q in scoremodel.db.session
     # This only works in a context manager, and not with self.assertRaises() - I have no idea why.
     with self.assertRaises(DatabaseItemAlreadyExists):
         x = QuestionApi().create({'question': 'Test', 'weight': 1, 'section_id': s.id})
     self.assertIsInstance(q, Question)
Пример #8
0
 def test_complex(self):
     en = LangApi().by_lang('en')
     r = ReportApi().create({'title': 'Test', 'lang_id': en.id})
     s = SectionApi().create({'title': 'Test', 'report_id': r.id})
     ri = RiskFactorApi().create({'risk_factor': 'Test', 'lang_id': en.id})
     a = AnswerApi().create({'answer': 'Test', 'lang_id': en.id})
     q = QuestionApi().create({'question': 'Test', 'weight': 1, 'section_id': s.id, 'answers': [a.id],
                               'risk_factor_id': ri.id})
     assert q in scoremodel.db.session
     self.assertIsInstance(q, Question)
Пример #9
0
 def test_read(self):
     en = LangApi().by_lang('en')
     r = ReportApi().create({'title': 'Test', 'lang_id': en.id})
     u = UserApi().create({'email': '*****@*****.**', 'password': '******'})
     o = UserReportApi().create({
         'name': 'Test',
         'user_id': u.id,
         'report_id': r.id
     })
     assert o == UserReportApi().read(o.id)
Пример #10
0
 def test_delete(self):
     en = LangApi().by_lang('en')
     r = ReportApi().create({'title': 'Test', 'lang_id': en.id})
     u = UserApi().create({'email': '*****@*****.**', 'password': '******'})
     o = UserReportApi().create({
         'name': 'Test',
         'user_id': u.id,
         'report_id': r.id
     })
     assert UserReportApi().delete(o.id) is True
     assert o not in scoremodel.db.session
Пример #11
0
 def test_create(self):
     en = LangApi().by_lang('en')
     r = ReportApi().create({'title': 'Test', 'lang_id': en.id})
     s = SectionApi().create({'title': 'Test', 'report_id': r.id})
     assert s in scoremodel.db.session
     assert s.order_in_report == 0
     self.assertIsInstance(s, Section)
     self.assertRaises(DatabaseItemAlreadyExists,
                       SectionApi().create, {
                           'title': 'Test',
                           'report_id': r.id
                       })
Пример #12
0
 def test_create(self):
     en = LangApi().by_lang('en')
     r = ReportApi().create({'title': 'Test', 'lang_id': en.id})
     u = UserApi().create({'email': '*****@*****.**', 'password': '******'})
     o = UserReportApi().create({
         'name': 'Test',
         'user_id': u.id,
         'report_id': r.id
     })
     assert o in scoremodel.db.session
     # No checking for already_exists
     self.assertIsInstance(o, UserReport)
Пример #13
0
 def test_complex(self):
     en = LangApi().by_lang('en')
     r = ReportApi().create({'title': 'Test', 'lang_id': en.id})
     s = SectionApi().create({
         'title': 'Test',
         'report_id': r.id,
         'order_in_report': 2,
         'weight': 5
     })
     assert s in scoremodel.db.session
     assert s.order_in_report == 2
     assert s.weight == 5
     self.assertIsInstance(s, Section)
Пример #14
0
 def test_update(self):
     en = LangApi().by_lang('en')
     r = ReportApi().create({'title': 'Test', 'lang_id': en.id})
     s = SectionApi().create({'title': 'Test', 'report_id': r.id})
     s_u = SectionApi().update(s.id, {
         'title': 'Toto',
         'report_id': r.id,
         'order_in_report': 1
     })
     assert s_u == SectionApi().read(s.id)
     assert SectionApi().read(s.id).title == 'Toto'
     assert SectionApi().read(s.id).order_in_report == 1
     self.assertIsInstance(s_u, Section)
Пример #15
0
def v_report_delete(report_id):
    form = ReportDeleteForm()
    a_report = ReportApi()
    try:
        existing_report = a_report.read(report_id)
    except DatabaseItemDoesNotExist:
        flash(_('No report with id {0} exists.').format(report_id))
        return url_for('admin.v_report_list')
    if request.method == 'POST' and form.validate_on_submit():
        try:
            if a_report.delete(report_id) is True:
                flash(_('Report {0} removed.').format(report_id))
            else:
                flash(_('Failed to remove report {0}.').format(report_id))
        except Exception as e:
            flash(_('An unexpected error occurred.'))
            return render_template('admin/generic/delete.html', action_url=url_for('admin.v_report_delete',
                                                                                   report_id=report_id),
                                   item_type=_('Report'), item_identifier=existing_report.title, form=form)
        else:
            return redirect(url_for('admin.v_report_list'))
    return render_template('admin/generic/delete.html', action_url=url_for('admin.v_report_delete',
                                                                           report_id=report_id),
                           item_type=_('Report'), item_identifier=existing_report.title, form=form)
Пример #16
0
 def create(self, object_data):
     cleaned_data = self.parse_input_data(object_data)
     existing_report = BenchmarkReport.query.filter(
         and_(BenchmarkReport.report_id == cleaned_data['report_id'],
              BenchmarkReport.title == cleaned_data['title'])).first()
     if existing_report:
         raise DatabaseItemAlreadyExists(_e['item_already_in'].format(
             BenchmarkReport, cleaned_data['title'], 'Report',
             cleaned_data['report_id']))
     new_report = BenchmarkReport(title=cleaned_data['title'])
     db.session.add(new_report)
     report = ReportApi().read(cleaned_data['report_id'])
     new_report.report = report
     db.session.commit()
     return new_report
Пример #17
0
 def test_update(self):
     en = LangApi().by_lang('en')
     r = ReportApi().create({'title': 'Test', 'lang_id': en.id})
     u = UserApi().create({'email': '*****@*****.**', 'password': '******'})
     o = UserReportApi().create({
         'name': 'Test',
         'user_id': u.id,
         'report_id': r.id
     })
     o_x = UserReportApi().update(o.id, {
         'name': 'Foo',
         'user_id': u.id,
         'report_id': r.id
     })
     assert o_x == UserReportApi().read(o.id)
     assert UserReportApi().read(o.id).name == 'Foo'
     self.assertIsInstance(o_x, UserReport)
Пример #18
0
def v_user_report_new(user_id):
    if current_user.id != user_id:
        flash(_('You can only view your own reports.'))
        abort(403)
    form = UserReportCreateForm()
    report_api = ReportApi()
    choices = [(r.id, r.title)
               for r in report_api.by_lang(locale_api.current_locale)]
    if len(choices) == 0:
        choices = [(r.id, r.title)
                   for r in report_api.by_lang(locale_api.fallback_locale)]
    form.report.choices = choices
    if request.method == 'POST' and form.validate_on_submit():
        user_report_data = {
            'user_id': current_user.id,
            'report_id': form.report.data,
            'name': form.name.data
        }
        try:
            new_user_report = user_report_api.create(user_report_data)
        except DatabaseItemAlreadyExists as e:
            flash(_('This report already exists.'))
            return redirect(
                url_for('public.v_user_report_new', user_id=user_id))
        except RequiredAttributeMissing as e:
            flash(
                _('A required form element was not submitted: {0}').format(e))
            return redirect(
                url_for('public.v_user_report_new', user_id=current_user.id))
        except Exception as e:
            flash(_('An unexpected error occurred.'))
            return redirect(
                url_for('public.v_user_report_new', user_id=current_user.id))
        else:
            report_api = ReportApi()
            report_template = report_api.read(new_user_report.report_id)
            first_section = report_template.ordered_sections[0]
            flash(_('Report created'))
            return redirect(
                url_for('public.v_user_report_section',
                        user_report_id=new_user_report.id,
                        user_id=current_user.id,
                        section_id=first_section.id))
    else:
        return render_template('public/create.html',
                               form=form,
                               title=_('New report'))
Пример #19
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)
Пример #20
0
 def test_read(self):
     en = LangApi().by_lang('en')
     r = ReportApi().create({'title': 'Test', 'lang_id': en.id})
     s = SectionApi().create({'title': 'Test', 'report_id': r.id})
     assert s == SectionApi().read(s.id)
Пример #21
0
 def test_delete(self):
     en = LangApi().by_lang('en')
     r = ReportApi().create({'title': 'Test', 'lang_id': en.id})
     s = SectionApi().create({'title': 'Test', 'report_id': r.id})
     assert SectionApi().delete(s.id) is True
     assert s not in scoremodel.db.session
Пример #22
0
def v_report_list():
    a_report = ReportApi()
    l_reports = a_report.list()
    return render_template('admin/report/list.html', reports=l_reports)
Пример #23
0
 def test_read(self):
     en = LangApi().by_lang('en')
     r = ReportApi().create({'title': 'Test', 'lang_id': en.id})
     s = SectionApi().create({'title': 'Test', 'report_id': r.id})
     q = QuestionApi().create({'question': 'Test', 'weight': 1, 'section_id': s.id})
     assert q == QuestionApi().read(q.id)