예제 #1
0
파일: sync_redcap.py 프로젝트: faanwar/avrc
def get_results(redcap):
    def clean(s):
        """ Helper method to sanitize the string """
        return s.strip() or None

    def is_criteria_met(x):
        a = x['nat_results_complete'] == '2' and x['rec_status'] != '1'
        return a

    results = []
    try:
        all_records = redcap.project['CTS'].export_records(
            fields=['rc_id', 'nat_results_complete', 'rec_status'])
        filtered_records = list(x['rc_id'] for x in all_records
                                if is_criteria_met(x) == True)

        if (len(filtered_records) > 0):
            records = redcap.project['CTS'].export_records(
                records=filtered_records)
        else:
            records = []

        for record in records:
            print 'record'
            print record
            rcid = record['rc_id']
            result = models.Result(site_code=rcid[:-5],
                                   reference_number=rcid[-5:],
                                   nat=clean(record['nat']),
                                   nat_sco=clean(record['nat_sco']),
                                   dhiv=clean(record['dhiv']),
                                   dhiv_sco=clean(record['dhiv_sco']),
                                   dhcv=clean(record['dhcv']),
                                   dhcv_sco=clean(record['dhcv_sco']),
                                   dhbv=clean(record['dhbv']),
                                   test_date=datetime.datetime.strptime(
                                       clean(record['nat_result_date']),
                                       '%Y-%m-%d'),
                                   dhbv_sco=clean(record['dhbv_sco']))
            results.append(result)
    except Exception as e:
        print e
    return results
예제 #2
0
def test_parsefp_duplicate_result():
    """
    Ensure we can detect duplicates
    """
    site_code = 'XXXX'
    reference_number = 'YYYYY'

    # Create a pre-existing result
    Session.add(
        models.Result(site_code=site_code,
                      reference_number=reference_number,
                      nat='N',
                      file='oldfile.txt'))
    Session.flush()

    fp = StringIO()
    print >> fp, 'Line 1'
    print >> fp, (site_code + ' ' + reference_number).ljust(80)
    fp.seek(0)
    results, duplicates = parser.parsefp(fp)
    eq_(len(results), 0)
    eq_(len(duplicates), 1)
예제 #3
0
    def test_FindMissingDraw_MixedResultsMoreAndLessThan6MonthsOldAndAllMissingDrawDates_ReturnListOfValidResults(
            self):
        """
        Should return 3 results out of 5 in a list. These 3 results' test date are less
        than 180 days. All results are missing draw dates.
        """

        missing_draw = []
        current_time = datetime.date.today()
        date1 = current_time - datetime.timedelta(days=30)
        date2 = current_time - datetime.timedelta(days=60)
        date3 = current_time - datetime.timedelta(days=90)
        # Older than 180 days
        date4 = current_time - datetime.timedelta(days=200)
        date5 = current_time - datetime.timedelta(days=300)

        # Add 5 unique results where 5 results are missing their draw date and
        # 3 are less than 6 months old. The other 2 are more than 6 months old.
        site_code = '99X'
        reference_number1 = '11111'
        reference_number2 = '22222'
        reference_number3 = '33333'
        reference_number4 = '44444'
        reference_number5 = '55555'

        Session.add(
            models.Result(site_code=site_code,
                          reference_number=reference_number1,
                          test_date=date1,
                          nat='P',
                          dhiv='N',
                          file='results.txt'))

        Session.add(
            models.Result(site_code=site_code,
                          reference_number=reference_number2,
                          test_date=date2,
                          nat='P',
                          dhiv='N',
                          file='results.txt'))

        Session.add(
            models.Result(site_code=site_code,
                          reference_number=reference_number3,
                          test_date=date3,
                          nat='P',
                          dhiv='N',
                          file='results.txt'))

        Session.add(
            models.Result(site_code=site_code,
                          reference_number=reference_number4,
                          test_date=date4,
                          nat='P',
                          dhiv='N',
                          file='results.txt'))

        Session.add(
            models.Result(site_code=site_code,
                          reference_number=reference_number5,
                          test_date=date5,
                          nat='P',
                          dhiv='N',
                          file='results.txt'))

        transaction.commit()

        missing_draw = scripts.parse.find_missing_draw()

        # Correct number of missing draw date cases?
        assert len(missing_draw) == 3

        # Are they really missing draw dates?
        for x in missing_draw:
            assert not x.draw_date

        results_expiration = current_time - datetime.timedelta(days=180)

        # Are their test dates under 180 days
        for x in missing_draw:
            assert x.test_date > results_expiration
예제 #4
0
    def test_FindMissingDraw_AllResultsMoreThan6MonthsOldAndAllMissingDrawDates_ReturnEmptyListOfResults(
            self):
        """
        Should return an empty list of results because all results' test date is older
        than 180 days. All results are missing draw dates.
        """

        missing_draw = []

        current_time = datetime.date.today()
        date1 = current_time - datetime.timedelta(days=190)
        date2 = current_time - datetime.timedelta(days=200)
        date3 = current_time - datetime.timedelta(days=250)
        date4 = current_time - datetime.timedelta(days=300)
        date5 = current_time - datetime.timedelta(days=350)

        # Add 5 unique results where 5 results are missing their draw date and
        # 5 are more than 6 months old
        site_code = '99X'
        reference_number1 = '11111'
        reference_number2 = '22222'
        reference_number3 = '33333'
        reference_number4 = '44444'
        reference_number5 = '55555'

        Session.add(
            models.Result(site_code=site_code,
                          reference_number=reference_number1,
                          test_date=date1,
                          nat='P',
                          dhiv='N',
                          file='results.txt'))

        Session.add(
            models.Result(site_code=site_code,
                          reference_number=reference_number2,
                          test_date=date2,
                          nat='P',
                          dhiv='N',
                          file='results.txt'))

        Session.add(
            models.Result(site_code=site_code,
                          reference_number=reference_number3,
                          test_date=date3,
                          nat='P',
                          dhiv='N',
                          file='results.txt'))

        Session.add(
            models.Result(site_code=site_code,
                          reference_number=reference_number4,
                          test_date=date4,
                          nat='P',
                          dhiv='N',
                          file='results.txt'))

        Session.add(
            models.Result(site_code=site_code,
                          reference_number=reference_number5,
                          test_date=date5,
                          nat='P',
                          dhiv='N',
                          file='results.txt'))

        transaction.commit()

        missing_draw = scripts.parse.find_missing_draw()

        # Correct number of missing draw date cases?
        assert len(missing_draw) == 0