def index(date=None): '''Generates and renders time report in HTML viewl Parameters ---------- date : str (optional) The date for which report will be generated, formated as "YYYY-mm-dd" Returns ------- Response Flask response object ''' today = datetime.now() if date is None: a_day = 1 # datetime.strftime(datetime.now() - timedelta(a_day), '%Y-%m-%d') date = helpers.format_date(today - timedelta(a_day)) today_str = helpers.format_date(today) by_projects = {} users = [] if not helpers.is_valid_date_string(date): error = INVALID_DATE_MESSAGE app.logger.error(error) return flask.render_template('index.html', by_projects=by_projects, users=users, error=error, date=date, today=today_str) try: report_data = fetch_report(date) except BaseException as e: app.logger.error(e) error = HUBSTAFF_CLIENT_ERROR_MESSAGE return flask.render_template('index.html', by_projects=by_projects, users=users, error=error, date=date, today=today_str) rg = ReportGenerator() report = rg.prepare_for_template(rg.build_report(report_data)) # if report is not empty unpack report if report: users, by_projects = report return flask.render_template('index.html', by_projects=by_projects, users=users, date=date, today=today_str)
def export_report(report_date): '''Handles requests to export report for a given date as CSV file Parameters ---------- report_date : str The date for which report will be generated and exported, formated as "YYYY-mm-dd" Returns ------- Response Flask response object ''' if not report_date or not helpers.is_valid_date_string(report_date): bad_request_code = 400 app.logger.error(INVALID_DATE_MESSAGE) error_response = prepare_error_response(INVALID_DATE_MESSAGE, bad_request_code) return flask.abort(error_response) try: report_data = fetch_report(report_date) except BaseException as e: app.logger.error(e) server_error_code = 500 error_response = prepare_error_response(HUBSTAFF_CLIENT_ERROR_MESSAGE, server_error_code) return flask.abort(error_response) rg = ReportGenerator() report = rg.build_report(report_data) export_filename = f'daily_time_report_for_{report_date}.csv' writer = CsvWriter(report) temp_file = writer.write_all() mimetype = 'Content-Type: text/csv; charset=utf-8' response = flask.send_file(temp_file, as_attachment=True, mimetype=mimetype, attachment_filename=export_filename, cache_timeout=1) # explicitly clean up temp file if os.path.exists(temp_file): os.remove(temp_file) return response
def test_that_is_valid_date_string_returns_false(): '''Test if a date string that does not match given format returns false''' test_date = '2019-10-21' date_format = '%m/%d/%Y' assert helpers.is_valid_date_string(test_date, date_format) is False
def test_that_is_valid_date_string_returns_true(): ''' Test that is_valid_date_string works returns true for valid date string ''' test_date = '2019-10-21' assert helpers.is_valid_date_string(test_date) is True
def test_that_is_valid_date_string_accepts_format(): '''Accept that is_valid_date_string accepts format''' test_date = '12/24/2018' date_format = '%m/%d/%Y' assert helpers.is_valid_date_string(test_date, date_format) is True