def generate_reports(conditions, reports_start=0, reports_end=0): session = Session() reports_max = int(session.query(models.Result).filter(conditions).count()) if reports_end == 0: reports_end = reports_max return session.query(models.Result).filter(conditions).order_by( desc(models.Result.test_date)).slice(reports_start, reports_end)
def test_valid_entry_update_result_draw_date(self): """ Should pass, save entry in draw table, and update draw date in corresponding result entry in result table """ from aeh.earlytest.draw_input import process from datetime import date from datetime import datetime from aeh.earlytest import Session, models site_code = '76C' reference_number = '55555' expected = site_code + reference_number draw_date = '2015-01-01' Session.add( models.Result(site_code=site_code, reference_number=reference_number, test_date=date.today(), nat='N', file='results.txt')) request = DummyRequest({ 'redcrossid-entry': expected, 'drawdate-input': draw_date, 'rcidform.submitted': True, }) ret = process(request) self.assertEquals(ret['errorStatus'], 'rcidAccepted') draw = Session.query(models.Draw)\ .filter(models.Draw.site_code == site_code)\ .filter(models.Draw.reference_number == reference_number).first() self.assertEquals(draw.site_code, site_code) self.assertEquals(draw.reference_number, reference_number) self.assertEquals( draw.draw_date, datetime.date(datetime.strptime(draw_date, "%Y-%m-%d"))) result = Session.query(models.Result)\ .filter(models.Result.site_code == site_code)\ .filter(models.Result.reference_number == reference_number).first() self.assertEquals( result.draw_date, datetime.date(datetime.strptime(draw_date, "%Y-%m-%d")))
def process_date_update(request, site_codes): # This method receives the GET request for draw date update # and updates the new date in the DB # FIX: Date validations should not be greater than #redcap = RCProject(site_codes, rcs) max_date = datetime.date.today() min_date = max_date - datetime.timedelta(days=365) new_date_raw = request.GET['new_date_raw'] print new_date_raw new_date = datetime.datetime.strptime(new_date_raw, "%Y-%m-%d").date() print new_date ref_num = request.GET['ref_num'] site_code = request.GET['site'] # retrieve the record from the db if min_date <= new_date and new_date <= max_date: session = Session() record = session.query(models.Result).\ filter(models.Result.site_code == site_code).\ filter(models.Result.reference_number == ref_num).first() if record: record.draw_date = new_date transaction.commit() return response
def test_valid_entry_update_database(self): """ Should pass and the entry gets saved in draw table """ from aeh.earlytest.draw_input import process from datetime import date from datetime import datetime from aeh.earlytest import Session, models site_code = '76C' reference_number = '44444' expected = site_code + reference_number draw_date = '2015-01-01' request = DummyRequest({ 'redcrossid-entry': expected, 'drawdate-input': draw_date, 'rcidform.submitted': True, }) ret = process(request) self.assertEquals(ret['errorStatus'], 'rcidAccepted') draw = Session.query(models.Draw)\ .filter(models.Draw.site_code == site_code)\ .filter(models.Draw.reference_number == reference_number).first() self.assertTrue(draw) self.assertEquals(draw.site_code, site_code) self.assertEquals(draw.reference_number, reference_number) self.assertEquals( draw.draw_date, datetime.date(datetime.strptime(draw_date, "%Y-%m-%d")))
def get_reports_max(conditions): session = Session() return int(session.query(models.Result).filter(conditions).count())
def process(request, default_view={}): error_status = '' site_code = '' ref_num = '' draw_date = '' draw_date_raw = '' # Calculate min and max attributes for draw-input.pt page max_date = datetime.date.today() min_date = max_date - datetime.timedelta(days=365) max_date = max_date.strftime("%Y-%m-%d") min_date = min_date.strftime("%Y-%m-%d") if 'rcidform.submitted' in request.params: redcrossid = parse_input(request.params['redcrossid-entry']) site_code = redcrossid['site_code'] ref_num = redcrossid['ref_num'] draw_date_raw = request.params['drawdate-input'] draw_date = datetime.datetime.strptime(draw_date_raw, "%Y-%m-%d").date() # Check if red cross id is in correct format if not check_format(site_code, ref_num): #Error. Incorrect red cross id format error_status = "incorrectFormat" else: session = Session() draw = session.query(models.Draw)\ .filter(models.Draw.site_code == site_code)\ .filter(models.Draw.reference_number == ref_num).first() #check if it exists in the database if not draw: #Sucess! Save the entry in the draw database new_draw_entry = models.Draw(site_code=site_code, reference_number=ref_num, draw_date=draw_date, has_result=int(0)) session.add(new_draw_entry) transaction.commit() error_status = "rcidAccepted" #Check if this draw's rcid is currently in our results table. If so, update #the result's draw date. result = session.query(models.Result)\ .filter(models.Result.site_code == site_code)\ .filter(models.Result.reference_number == ref_num).first() if result: result.draw_date = draw_date # Update has_result status of the draw date entry to True draw_entry = session.query(models.Draw)\ .filter(models.Draw.site_code == site_code)\ .filter(models.Draw.reference_number == ref_num).first() draw_entry.has_result = int(1) transaction.commit() else: #Error. Duplicate entry error_status = "duplicateEntry" ret = { 'defaultNumber': site_code + ref_num, 'errorStatus': error_status, 'drawDate': draw_date, 'drawDateRaw': draw_date_raw, 'maxDate': max_date, 'minDate': min_date, } return dict(default_view.items() + ret.items())
def get_results(input_rcid): print 'im in there ' # parse inputs for site_code and reference_number clean_rcid = parse_input(input_rcid) site_code = clean_rcid['site_code'] ref_num = clean_rcid['ref_num'] current_time = datetime.date.today() # Var used to determine expiration of test results results_expiration = current_time - datetime.timedelta(days=int(Config.get('aeh:results', 'results_expiration'))) # Var used to calculate buffer time for test result retrieval results_buffer = current_time - datetime.timedelta(days=int(Config.get('aeh:results', 'results_buffer'))) site_code_list = Config.get('aeh:results', 'site.codes').split() if not site_code in site_code_list: query_status = 'RESULTS_NOT_FOUND' else: session = Session() result = session.query(models.Result)\ .filter(models.Result.site_code == site_code)\ .filter(models.Result.reference_number == ref_num).first() show_results = Config.getboolean('aeh:results', 'site.{0}.show_results'.format(site_code.upper())) # Check if result exists if result: print 'results exist' query_draw_date = result.draw_date # Check if the site allows access to results if not show_results: print 'results not allowed' query_status = 'RESULTS_NOT_AVAILABLE' # Check if there is a draw date elif not query_draw_date: print 'no draw date' query_status = 'RESULTS_NOT_AVAILABLE' # Check if dhiv result is P elif result.check('dhiv'): print 'dhiv p' query_status = 'RESULTS_NOT_AVAILABLE' # Check if dhiv result in N elif result.check('dhiv') is False: # Check if draw date is 14 or more days print 'dhiv n' if query_draw_date <= results_buffer: # Check if draw date is less than 90 days old if query_draw_date > results_expiration: query_status = 'RESULTS_NEGATIVE' print 'draw date less than 90' else: query_status = 'RESULTS_OUT_OF_DATE' else: query_status = 'RESULTS_NOT_AVAILABLE' print 'draw date less than 14' # Else return results not available, such if dhiv is NT or something weird else: query_status = 'RESULTS_NOT_AVAILABLE' print 'something weird' else: query_status = 'RESULTS_NOT_FOUND' print 'result dont exist' return query_status