def test_make_ncso_concessions_email_for_ccg(self, attach_image):
        bookmark = NCSOConcessionBookmark.objects.create(user=self.user,
                                                         pct=self.ccg)

        msg = bookmark_utils.make_ncso_concession_email(bookmark)

        self.assertEqual(msg.subject,
                         "Your update about Price Concessions for CCG 0")
        self.assertIn("published for **July 2018**", msg.body)
        additional_cost = round(
            ncso_spending_for_entity(self.ccg, "ccg", 1)[0]["additional_cost"])
        self.assertIn("**\xa3{:,}**".format(additional_cost), msg.body)

        html = msg.alternatives[0][0]
        self.assertInHTML("<b>July 2018</b>", html)
Example #2
0
    def test_make_ncso_concessions_email_for_practice(self, attach_image):
        bookmark = NCSOConcessionBookmark.objects.create(
            user=self.user, practice=self.practice)

        msg = bookmark_utils.make_ncso_concession_email(bookmark)

        self.assertEqual(msg.subject,
                         "Your update about NCSO Concessions for Practice 2")
        self.assertIn("published for **July 2018**", msg.body)
        self.assertIn("prescribed at Practice 2", msg.body)
        additional_cost = round(
            ncso_spending_for_entity(self.practice, "practice",
                                     1)[0]["additional_cost"])
        self.assertIn("an additional **\xa3{:,}**".format(additional_cost),
                      msg.body)

        html = msg.alternatives[0][0]
        self.assertInHTML("<b>July 2018</b>", html)
Example #3
0
    def test_make_ncso_concessions_email_for_all_england(self, attach_image):
        bookmark = NCSOConcessionBookmark.objects.create(user=self.user)

        msg = bookmark_utils.make_ncso_concession_email(bookmark)

        self.assertEqual(
            msg.subject,
            "Your update about NCSO Concessions for the NHS in England")
        self.assertIn("published for **July 2018**", msg.body)
        additional_cost = round(
            ncso_spending_for_entity(None, "all_england",
                                     1)[0]["additional_cost"])
        self.assertIn(
            "cost the NHS in England an additional **\xa3{:,}**".format(
                additional_cost),
            msg.body,
        )

        html = msg.alternatives[0][0]
        self.assertInHTML("<b>July 2018</b>", html)
Example #4
0
def make_all_england_email(bookmark, tag=None):
    msg = initialise_email(bookmark, "all-england-alerts")
    msg.subject = "Your monthly update on prescribing across NHS England"

    date = ImportLog.objects.latest_in_category("dashboard_data").current_at

    # This allows us to switch between calculating savings at the practice or
    # CCG level. We use CCG at present for performance reasons but we may want
    # to switch in future.
    entity_type = "CCG"
    ppu_savings = get_total_savings_for_org(str(date),
                                            "all_standard_practices", None)
    measure_savings = cached(all_england_measure_savings, entity_type, date)
    low_priority_savings = cached(all_england_low_priority_savings,
                                  entity_type, date)
    low_priority_total = cached(all_england_low_priority_total, entity_type,
                                date)
    ncso_spending = first_or_none(
        ncso_spending_for_entity(None, "all_england", num_months=1))

    unsubscribe_path = reverse("bookmarks",
                               kwargs={"key": bookmark.user.profile.key})
    unsubscribe_link = settings.GRAB_HOST + unsubscribe_path

    context = {
        "entity_type": entity_type,
        "ppu_savings": ppu_savings,
        "measure_savings": measure_savings,
        "low_priority_savings": low_priority_savings,
        "low_priority_total": low_priority_total,
        "ncso_spending": ncso_spending,
        "date": date,
        "unsubscribe_link": unsubscribe_link,
        "HOST": settings.GRAB_HOST,
    }

    finalise_email(msg, "bookmarks/email_for_all_england.html", context,
                   ["all_england", tag])

    return msg
def make_all_england_email(bookmark, tag=None):
    msg = initialise_email(bookmark, 'all-england-alerts')
    msg.subject = 'Your monthly update on prescribing across NHS England'

    date = ImportLog.objects.latest_in_category('ppu').current_at

    # This allows us to switch between calculating savings at the practice or
    # CCG level. We use CCG at present for performance reasons but we may want
    # to switch in future.
    entity_type = 'CCG'
    ppu_savings = cached(all_england_ppu_savings, entity_type, date)
    measure_savings = cached(all_england_measure_savings, entity_type, date)
    low_priority_savings = cached(all_england_low_priority_savings,
                                  entity_type, date)
    low_priority_total = cached(all_england_low_priority_total, entity_type,
                                date)
    ncso_spending = first_or_none(
        ncso_spending_for_entity(None, 'all_england', num_months=1))

    unsubscribe_path = reverse('bookmark-login',
                               kwargs={'key': bookmark.user.profile.key})
    unsubscribe_link = settings.GRAB_HOST + unsubscribe_path

    context = {
        'entity_type': entity_type,
        'ppu_savings': ppu_savings,
        'measure_savings': measure_savings,
        'low_priority_savings': low_priority_savings,
        'low_priority_total': low_priority_total,
        'ncso_spending': ncso_spending,
        'date': date,
        'unsubscribe_link': unsubscribe_link,
        'HOST': settings.GRAB_HOST,
    }

    finalise_email(msg, 'bookmarks/email_for_all_england.html', context,
                   ['all_england', tag])

    return msg
def make_ncso_concession_email(bookmark, tag=None):
    msg = initialise_email(bookmark, "ncso-concessions-alerts")

    monthly_totals = ncso_spending_for_entity(bookmark.entity,
                                              bookmark.entity_type,
                                              num_months=1)
    latest_month = max(row["month"] for row in monthly_totals)
    breakdown = ncso_spending_breakdown_for_entity(bookmark.entity,
                                                   bookmark.entity_type,
                                                   latest_month)[:10]
    last_prescribing_month = ImportLog.objects.latest_in_category(
        "prescribing").current_at

    if bookmark.entity_type == "CCG":
        concessions_view_name = "spending_for_one_ccg"
        concessions_kwargs = {"entity_code": bookmark.entity.code}
        dashboard_view_name = "ccg_home_page"
        dashboard_kwargs = {"ccg_code": bookmark.entity.code}
    elif bookmark.entity_type == "practice":
        concessions_view_name = "spending_for_one_practice"
        concessions_kwargs = {"entity_code": bookmark.entity.code}
        dashboard_view_name = "practice_home_page"
        dashboard_kwargs = {"practice_code": bookmark.entity.code}
    elif bookmark.entity_type == "all_england":
        concessions_view_name = "spending_for_all_england"
        concessions_kwargs = {}
        dashboard_view_name = "all_england"
        dashboard_kwargs = {}
    else:
        assert False

    concessions_path = reverse(concessions_view_name,
                               kwargs=concessions_kwargs)
    concessions_url = settings.GRAB_HOST + concessions_path

    dashboard_path = reverse(dashboard_view_name, kwargs=dashboard_kwargs)
    dashboard_url = settings.GRAB_HOST + dashboard_path

    unsubscribe_path = reverse("bookmark-login",
                               kwargs={"key": bookmark.user.profile.key})
    unsubscribe_link = settings.GRAB_HOST + unsubscribe_path

    with NamedTemporaryFile(suffix=".png") as f:
        chart_image_cid = attach_image(msg, concessions_path, f.name,
                                       "#monthly-totals-chart")

    context = {
        "latest_month": latest_month,
        "last_prescribing_month": last_prescribing_month,
        "entity_name": bookmark.entity_cased_name,
        "entity_type": bookmark.entity_type,
        "additional_cost": monthly_totals[0]["additional_cost"],
        "breakdown": breakdown,
        "concessions_url": concessions_url,
        "dashboard_url": dashboard_url,
        "chart_image_cid": chart_image_cid,
        "unsubscribe_link": unsubscribe_link,
    }

    finalise_email(
        msg,
        "bookmarks/email_for_ncso_concessions.html",
        context,
        ["ncso_concessions", tag],
    )

    return msg
 def validate_ncso_spending_for_entity(self, *args, **kwargs):
     # with self.subTest(function='_ncso_spending_for_entity'):
     results = ncso_spending_for_entity(*args, **kwargs)
     expected = recalculate_ncso_spending_for_entity(*args, **kwargs)
     self.assertEqual(round_floats(results), round_floats(expected))
def make_ncso_concession_email(bookmark, tag=None):
    msg = initialise_email(bookmark, 'ncso-concessions-alerts')

    monthly_totals = ncso_spending_for_entity(bookmark.entity,
                                              bookmark.entity_type,
                                              num_months=1)
    latest_month = max(row['month'] for row in monthly_totals)
    breakdown = ncso_spending_breakdown_for_entity(bookmark.entity,
                                                   bookmark.entity_type,
                                                   latest_month)[:10]
    last_prescribing_month = (
        ImportLog.objects.latest_in_category('prescribing').current_at)

    if bookmark.entity_type == 'CCG':
        concessions_view_name = 'spending_for_one_ccg'
        concessions_kwargs = {'entity_code': bookmark.entity.code}
        dashboard_view_name = 'ccg_home_page'
        dashboard_kwargs = {'ccg_code': bookmark.entity.code}
    elif bookmark.entity_type == 'practice':
        concessions_view_name = 'spending_for_one_practice'
        concessions_kwargs = {'entity_code': bookmark.entity.code}
        dashboard_view_name = 'practice_home_page'
        dashboard_kwargs = {'practice_code': bookmark.entity.code}
    elif bookmark.entity_type == 'all_england':
        concessions_view_name = 'spending_for_all_england'
        concessions_kwargs = {}
        dashboard_view_name = 'all_england'
        dashboard_kwargs = {}
    else:
        assert False

    concessions_path = reverse(concessions_view_name,
                               kwargs=concessions_kwargs)
    concessions_url = settings.GRAB_HOST + concessions_path

    dashboard_path = reverse(dashboard_view_name, kwargs=dashboard_kwargs)
    dashboard_url = settings.GRAB_HOST + dashboard_path

    unsubscribe_path = reverse('bookmark-login',
                               kwargs={'key': bookmark.user.profile.key})
    unsubscribe_link = settings.GRAB_HOST + unsubscribe_path

    with NamedTemporaryFile(suffix='.png') as f:
        chart_image_cid = attach_image(msg, concessions_path, f.name,
                                       '#monthly-totals-chart')

    context = {
        'latest_month': latest_month,
        'last_prescribing_month': last_prescribing_month,
        'entity_name': bookmark.entity_cased_name,
        'entity_type': bookmark.entity_type,
        'additional_cost': monthly_totals[0]['additional_cost'],
        'breakdown': breakdown,
        'concessions_url': concessions_url,
        'dashboard_url': dashboard_url,
        'chart_image_cid': chart_image_cid,
        'unsubscribe_link': unsubscribe_link,
    }

    finalise_email(msg, 'bookmarks/email_for_ncso_concessions.html', context,
                   ['ncso_concessions', tag])

    return msg