def handle(self, *args, **options):
        all_bnf_codes = get_all_bnf_codes()

        classes = [
            (Section, "bnf_id"),
            (Chemical, "bnf_code"),
            (Product, "bnf_code"),
            (Presentation, "bnf_code"),
        ]
        with transaction.atomic():
            for model, field_name in classes:
                for obj in model.objects.filter(is_current=False):
                    prefix = getattr(obj, field_name)
                    if any(
                            bnf_code.startswith(prefix)
                            for bnf_code in all_bnf_codes):
                        obj.is_current = True
                        obj.save()
예제 #2
0
def get_bnf_codes(base_query, filter_):
    """Return list of BNF codes used to caluclate measure numerator/denominator
    values.

    At least one of `base_query` and `filter_` must not be None.

    `base_query` is a query to be run against BigQuery.  It should return
    results with a single column called `bnf_code`.

    `filter_` is a list of BNF code filters, which have one of the following
    forms:

      * a BNF code (eg 0501015P0AAABAB)
      * a BNF code prefix (eg 0501015P0)
      * a string including a SQL wildcard (eg 0501015P0%AB)

    Each filter can be negated by being prefixed with a ~.

    If both `base_query` and `filter_` are not None, the results of
    `base_query` are filtered by the filters specified in `filter_`.

    If `base_query` is None, all BNF codes are filtered by the filters
    specified in `filter_`.

    If `filter_` is None, the results of `base_query` are not filtered.
    """

    assert base_query or filter_

    query = build_bnf_codes_query(base_query, filter_)
    results = Client().query(query)

    # Before 2017, the published prescribing data included trailing spaces in
    # certain BNF codes.  We strip those here.  See #2447.
    bnf_codes = {row[0].strip() for row in results.rows}

    return sorted(bnf_codes & get_all_bnf_codes())