Skip to content

Commit

Permalink
Don't show duplicate rows for CCGs
Browse files Browse the repository at this point in the history
Fixes price-per-dose/#17
  • Loading branch information
sebbacon committed Feb 2, 2017
1 parent 4e81638 commit 11cc724
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
24 changes: 19 additions & 5 deletions openprescribing/api/views_labs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,33 @@ class NotValid(APIException):

@api_view(['GET'])
def price_per_dose(request, format=None):
code = request.query_params.get('q')
entity_code = request.query_params.get('entity_code', None)
date = request.query_params.get('date')
bnf_code = request.query_params.get('bnf_code', None)
if not (entity_code or bnf_code):
raise NotValid("You must supply an entity code or a bnf_code")

query = {'date': date}
filename = date
if entity_code:
filename += "-%s" % entity_code
if len(entity_code) == 3:
query['pct'] = entity_code
query['practice__isnull'] = True
else:
query['practice'] = entity_code
if bnf_code:
filename += "-%s" % bnf_code
query['bnf_code'] = bnf_code
savings = []
# We return the savings.
# Somehow in this data we end up with codes like 0601060D0AAA0A0 which does not exist.
for x in PPQSaving.objects.filter(pct=code, date=date):
for x in PPQSaving.objects.filter(**query):
d = model_to_dict(x)
d['name'] = x.product_name
d['flag_bioequivalence'] = getattr(
x.product, 'is_non_bioequivalent', None)
savings.append(d)
response = Response(savings)
if request.accepted_renderer.format == 'csv':
filename = "%s-%s-ppd.csv" % (code, date)
filename = "%s-ppd.csv" % (filename, date)
response['content-disposition'] = "attachment; filename=%s" % filename
return response
28 changes: 16 additions & 12 deletions openprescribing/frontend/management/commands/import_ppq_savings.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,21 @@ def handle(self, *args, **options):
PPQSaving.objects.filter(date=date).delete()
for entity_type, min_saving in [('pct', 1000), ('practice', 50)]:
result = get_savings(
group_by=entity_type, month=date, limit=0, min_saving=1000)
group_by=entity_type,
month=date,
limit=0,
min_saving=min_saving)
for row in result.itertuples():
asdict = row._asdict()
PPQSaving.objects.create(
date=date,
bnf_code=asdict['Index'],
lowest_decile=asdict['lowest_decile'],
quantity=asdict['quantity'],
price_per_dose=asdict['price_per_dose'],
possible_savings=asdict['possible_savings'],
formulation_swap=asdict['formulation_swap'] or None,
pct_id=asdict.get('pct', None),
practice_id=asdict.get('practice', None)
)
if asdict['price_per_dose']:
PPQSaving.objects.create(
date=date,
bnf_code=asdict['Index'],
lowest_decile=asdict['lowest_decile'],
quantity=asdict['quantity'],
price_per_dose=asdict['price_per_dose'],
possible_savings=asdict['possible_savings'],
formulation_swap=asdict['formulation_swap'] or None,
pct_id=asdict.get('pct', None),
practice_id=asdict.get('practice', None)
)
21 changes: 21 additions & 0 deletions openprescribing/frontend/migrations/0017_auto_20170202_1335.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.1 on 2017-02-02 13:35
from __future__ import unicode_literals

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('frontend', '0016_auto_20170127_1313'),
]

operations = [
migrations.AlterField(
model_name='ppqsaving',
name='bnf_code',
field=models.CharField(db_index=True, max_length=15, validators=[django.core.validators.RegexValidator(b'^[\\w]*$', code=b'Invalid name', message=b'name must be alphanumeric')]),
),
]
3 changes: 2 additions & 1 deletion openprescribing/frontend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,8 @@ class PPQSaving(models.Model):
"""
date = models.DateField(db_index=True)
bnf_code = models.CharField(max_length=15, validators=[isAlphaNumeric])
bnf_code = models.CharField(
max_length=15, validators=[isAlphaNumeric], db_index=True)
lowest_decile = models.FloatField()
quantity = models.IntegerField()
price_per_dose = models.FloatField()
Expand Down
2 changes: 1 addition & 1 deletion openprescribing/templates/price_per_dose.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ <h1>Possible monthly savings over £1,000 for {{ccg.name }}</h1>
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.js"></script>
<script type="text/javascript">
$('#price_per_pill').DataTable({
"ajax": {"url": "/api/1.0/price_per_dose/?q={{ ccg.code }}&date=2016-09-01&format=json", "dataSrc": ""},
"ajax": {"url": "/api/1.0/price_per_dose/?entity_code={{ ccg.code }}&date=2016-09-01&format=json", "dataSrc": ""},
"order": [[1, "desc"]],
"columns": [
{"data": "bnf_code",
Expand Down

0 comments on commit 11cc724

Please sign in to comment.