def get_household_types(): """Pulls in a list of the most recent members of the Congregation.""" members = Members() jwt_user = get_jwt_identity() authorized = utils.check_access(jwt_user, conf.REPORT_GROUP, members.database) utils.log_request(request, jwt_user, authorized) if not authorized: response = { 'message': '{} does not have access to reports.'.format(jwt_user) } return jsonify(response), 403 response = {} all_households = members.get_household_types() for item in all_households: item['total'] = str(item['total']) response['all_households'] = all_households now = datetime.datetime.now() year_ago = now - datetime.timedelta(days=365) new_start = str(year_ago)[:10] new_households = members.get_household_types(start=new_start) for item in new_households: item['total'] = str(item['total']) response['new_households'] = new_households return jsonify(response)
def test_count_new_members(monkeypatch): fake_response = pd.DataFrame({'count': [200]}) monkeypatch.setattr('pandas.read_sql', lambda *args, **kwargs: fake_response) members = Members() count = members.count_new_members('2018-01-01', '2019-01-01') assert count == 200
def get_households_by_year(): """Pulls in a list of the most recent members of the Congregation.""" members = Members() jwt_user = get_jwt_identity() authorized = utils.check_access(jwt_user, conf.REPORT_GROUP, members.database) utils.log_request(request, jwt_user, authorized) if not authorized: response = { 'message': '{} does not have access to reports.'.format(jwt_user) } return jsonify(response), 403 years = request.args.get('years') years = int(years) if years else 10 tally = request.args.get('tally') tally = 'households' if not tally else tally now = datetime.datetime.now() end = now.year start = end - years if tally == 'resignations': response = members.get_resignations_by_year(start, end) else: response = members.get_households_by_year(start, end) return jsonify(response)
def get_resignation_types(): """Breaks down resignations over hte past year by type.""" members = Members() jwt_user = get_jwt_identity() authorized = utils.check_access(jwt_user, conf.REPORT_GROUP, members.database) utils.log_request(request, jwt_user, authorized) if not authorized: response = { 'message': '{} does not have access to reports.'.format(jwt_user) } return jsonify(response), 403 resignation_types = members.get_resignation_types() return jsonify(resignation_types)
def test_get_households_by_year(monkeypatch): fake_response = pd.DataFrame({'total': [5000]}) monkeypatch.setattr('pandas.read_sql', lambda *args, **kwargs: fake_response) members = Members() demographics = members.get_households_by_year(2014, 2017) assert demographics == [{ 'year': '2014', 'count': '5000' }, { 'year': '2015', 'count': '5000' }, { 'year': '2016', 'count': '5000' }, { 'year': '2017', 'count': '5000' }]
def test_get_demographics(monkeypatch): fake_response = pd.DataFrame({ 'age_group': ['Parrots', 'Penguins'], 'total': [100, 262] }) monkeypatch.setattr('pandas.read_sql', lambda *args, **kwargs: fake_response) members = Members() demographics = members.get_demographics(new_members=True) assert demographics == [{ 'age_group': 'All', 'total': 362 }, { 'age_group': 'Parrots', 'total': 100 }, { 'age_group': 'Penguins', 'total': 262 }]
def test_get_resignation_types(monkeypatch): fake_response = pd.DataFrame({ 'resignation_reason': ['Too Far', 'Inactive'], 'total': [200, 300] }) monkeypatch.setattr('pandas.read_sql', lambda *args, **kwargs: fake_response) members = Members() demographics = members.get_resignation_types() assert demographics == [{ 'resignation_reason': 'All', 'total': 500 }, { 'resignation_reason': 'Inactive', 'total': 300 }, { 'resignation_reason': 'Too Far', 'total': 200 }]
def test_get_households_types(monkeypatch): fake_response = pd.DataFrame({ 'member_type': ['Family', 'Individual'], 'total': [200, 300] }) monkeypatch.setattr('pandas.read_sql', lambda *args, **kwargs: fake_response) members = Members() demographics = members.get_household_types() assert demographics == [{ 'member_type': 'All', 'total': 500 }, { 'member_type': 'Family', 'total': 200 }, { 'member_type': 'Individual', 'total': 300 }]
def get_member_demographics(): """Pulls the current membership demographics.""" members = Members() jwt_user = get_jwt_identity() authorized = utils.check_access(jwt_user, conf.REPORT_GROUP, members.database) utils.log_request(request, jwt_user, authorized) if not authorized: response = { 'message': '{} does not have access to reports.'.format(jwt_user) } return jsonify(response), 403 only = request.args.get('only') new_members = only == 'new_members' response = members.get_demographics(new_members=new_members) return jsonify(response)
def test_get_member_locations(monkeypatch): fake_response = pd.DataFrame({ 'location': ['Bird Town', 'Dog City', 'Fishville'], 'total': [500, 200, 100] }) monkeypatch.setattr('pandas.read_sql', lambda *args, **kwargs: fake_response) members = Members() locations = members.get_member_locations('city', limit=2) assert locations == [{ 'location': 'All', 'total': 800 }, { 'location': 'Bird Town', 'total': 500 }, { 'location': 'Dog', 'total': 200 }, { 'location': 'Other', 'total': 100 }] locations = members.get_member_locations('city', limit=3, start='2018-01-01', end='2019-01-01') assert locations == [{ 'location': 'All', 'total': 800 }, { 'location': 'Bird Town', 'total': 500 }, { 'location': 'Dog', 'total': 200 }, { 'location': 'Fishville', 'total': 100 }]
def upload_members(): """ Uploads membership data as a .csv or excel file """ member_manager = Members() jwt_user = get_jwt_identity() user = member_manager.database.get_item('users', jwt_user) authorized = user['role'] == conf.ADMIN_ROLE log_request(request, jwt_user, authorized) if not authorized: response = {'message': 'only admins can upload files'} return jsonify(response), 403 file_type = request.args.get('file_type') file_type = 'members' if not file_type else file_type good_upload = member_manager.upload_file(request, file_type=file_type) if good_upload: response = {'message': 'success'} return jsonify(response) else: abort(422)
def get_new_member_count(): """Pulls the new event counts by quarter.""" members = Members() jwt_user = get_jwt_identity() authorized = utils.check_access(jwt_user, conf.REPORT_GROUP, members.database) utils.log_request(request, jwt_user, authorized) if not authorized: response = { 'message': '{} does not have access to reports.'.format(jwt_user) } return jsonify(response), 403 quarters = get_quarters(19) response = get_quarterly_new_members(quarters, members) response = {k: str(response[k]) for k in response} return jsonify(response)
def test_upload_file(monkeypatch): fake_response = pd.DataFrame({'count': [200]}) monkeypatch.setattr('pandas.read_csv', lambda *args, **kwargs: True) monkeypatch.setattr('pandas.read_excel', lambda *args, **kwargs: True) class FakeFile: def __init__(self, filename): self.filename = filename class FakeRequest: def __init__(self, extension): self.files = {'file': FakeFile('camels_rock' + extension)} class FakeMemberLoader: def __init__(self): pass def load(self, df): return True def load_resignations(self, df): return True members = Members() members.member_loader = FakeMemberLoader() good_upload = members.upload_file(FakeRequest('.csv'), 'members') assert good_upload == True good_upload = members.upload_file(FakeRequest('.xlsx'), 'resignations') assert good_upload == True good_upload = members.upload_file(FakeRequest('.xls'), 'resignations') assert good_upload == True good_upload = members.upload_file(FakeRequest('.txt'), 'resignations') assert good_upload == False good_upload = members.upload_file(FakeRequest('.csv'), 'penguins') assert good_upload == False
def get_member_locations(): """Pulls in the current breakdown of member location.""" members = Members() jwt_user = get_jwt_identity() authorized = utils.check_access(jwt_user, conf.REPORT_GROUP, members.database) utils.log_request(request, jwt_user, authorized) if not authorized: response = { 'message': '{} does not have access to reports.'.format(jwt_user) } return jsonify(response), 403 level = request.args.get('level') level = level if level else 'city' now = datetime.datetime.now() year_ago = str(now - datetime.timedelta(days=365))[:10] five_years_ago = str(now - datetime.timedelta(days=365 * 5))[:10] # Hard coding these settings for now, but we can move these # to the config files if a client wants something different response = {} response['all_members'] = members.get_member_locations('city', limit=8) response['new_members'] = members.get_member_locations('city', limit=8, start=year_ago) response['year_ago'] = members.get_member_locations('city', limit=8, end=year_ago) response['five_years_ago'] = members.get_member_locations( 'city', limit=8, end=five_years_ago) common_locations = _find_common_locations(response) response['percentages'] = build_locations_pct(response, common_locations) response['common_locations'] = list(common_locations) return jsonify(response)