diff --git a/openprescribing/api/views_labs.py b/openprescribing/api/views_labs.py index dcd121277b..73528c8dc4 100644 --- a/openprescribing/api/views_labs.py +++ b/openprescribing/api/views_labs.py @@ -12,12 +12,26 @@ 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( @@ -25,6 +39,6 @@ def price_per_dose(request, format=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 diff --git a/openprescribing/frontend/management/commands/import_ppq_savings.py b/openprescribing/frontend/management/commands/import_ppq_savings.py index 54c52b2934..da465ee87e 100644 --- a/openprescribing/frontend/management/commands/import_ppq_savings.py +++ b/openprescribing/frontend/management/commands/import_ppq_savings.py @@ -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) + ) diff --git a/openprescribing/frontend/migrations/0017_auto_20170202_1335.py b/openprescribing/frontend/migrations/0017_auto_20170202_1335.py new file mode 100644 index 0000000000..a6d2b2eb9f --- /dev/null +++ b/openprescribing/frontend/migrations/0017_auto_20170202_1335.py @@ -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')]), + ), + ] diff --git a/openprescribing/frontend/models.py b/openprescribing/frontend/models.py index dba966c844..39e042559c 100644 --- a/openprescribing/frontend/models.py +++ b/openprescribing/frontend/models.py @@ -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() diff --git a/openprescribing/templates/price_per_dose.html b/openprescribing/templates/price_per_dose.html index 77889511f8..bec1cd8bd7 100644 --- a/openprescribing/templates/price_per_dose.html +++ b/openprescribing/templates/price_per_dose.html @@ -60,7 +60,7 @@