diff --git a/app.py b/app.py index 0d1ea78..ce1b417 100644 --- a/app.py +++ b/app.py @@ -6,6 +6,7 @@ from chalicelib.api.insights import insights_api from chalicelib.api.members import members_api from chalicelib.api.events import events_api +from chalicelib.api.accountability import accountability_api app = Chalice(app_name="zap") app.register_blueprint(announcements_api) @@ -14,6 +15,7 @@ app.register_blueprint(insights_api) app.register_blueprint(members_api) app.register_blueprint(events_api) +app.register_blueprint(accountability_api) @app.route("/") diff --git a/chalicelib/api/accountability.py b/chalicelib/api/accountability.py new file mode 100644 index 0000000..464c148 --- /dev/null +++ b/chalicelib/api/accountability.py @@ -0,0 +1,18 @@ +from chalice import Blueprint +from chalicelib.decorators import auth +from chalicelib.services.AccountabilityService import accountability_service + +accountability_api = Blueprint(__name__) + + +@accountability_api.route("/accountability", methods=["GET"], cors=True) +@auth(accountability_api, roles=["admin"]) +def get_accountability(): + if accountability_api.current_request.query_params: + page = int(accountability_api.current_request.query_params.get("page", 0)) + page_size = int( + accountability_api.current_request.query_params.get("page_size", 20) + ) + else: + page, page_size = 0, 20 + return accountability_service.get_total_accountability(page, page_size) diff --git a/chalicelib/modules/google_sheets.py b/chalicelib/modules/google_sheets.py index 61a16e8..4034d6c 100644 --- a/chalicelib/modules/google_sheets.py +++ b/chalicelib/modules/google_sheets.py @@ -24,6 +24,26 @@ def __init__(self): ) self.service = build("sheets", "v4", credentials=self.creds) + def get_rows(self, spreadsheet_id: str, sheet_name: str, start_row: int, end_row: int): + range = f"{sheet_name}!A{start_row}:Z{end_row}" + response = ( + self.service.spreadsheets() + .values() + .get(spreadsheetId=spreadsheet_id, range=range) + .execute() + ) + return response + + def get_all_cells(self, spreadsheet_id: str, sheet_name: str): + range = f"{sheet_name}!A1:Z" + response = ( + self.service.spreadsheets() + .values() + .get(spreadsheetId=spreadsheet_id, range=range) + .execute() + ) + return response + def get_sheets(self, spreadsheet_id: str, include_properties: bool = False): sheet_metadata = ( self.service.spreadsheets().get(spreadsheetId=spreadsheet_id).execute() diff --git a/chalicelib/services/AccountabilityService.py b/chalicelib/services/AccountabilityService.py new file mode 100644 index 0000000..bb6fda7 --- /dev/null +++ b/chalicelib/services/AccountabilityService.py @@ -0,0 +1,46 @@ +from chalice import NotFoundError, BadRequestError +from chalicelib.modules.google_sheets import GoogleSheetsModule + + +class AccountabilityService: + def __init__(self): + # Currently hardcoded --> need to set as dynamic parameter either in DB or SSM + self.spreadsheet_id = "1VEj06zGrxq-1Hrp8ArbYJkvPw58g5e4C3MhDeBjL6o4" + self.gs = GoogleSheetsModule() + + def get_total_accountability(self, page: int, page_size: int = 20) -> dict[list]: + START_ROW = 2 + page * page_size + cells = self.gs.get_rows( + spreadsheet_id=self.spreadsheet_id, + sheet_name="Total", + start_row=START_ROW, + end_row=START_ROW + page_size - 1, + ) + values = cells["values"] + values = [ + { + "name": f"{value[0]} {value[1]}", + "status": value[2], + "currentPoints": int(value[3]), + "required": int(value[4]), + "chapterBucketMet": True if "YES" == value[9] else False, + "rushBucketMet": True if "YES" == value[10] else False, + "fundraisingBucketMet": True if "YES" == value[11] else False, + "eventsBucketMet": True if "YES" == value[12] else False, + "teamsBucketMet": True if "YES" == value[13] else False, + "variableBucketMet": True if "YES" == value[14] else False, + } + for value in values + ] + return values + + def get_accountability_from_leaderboard(self) -> dict[list]: + cells = self.gs.get_all_cells(self.spreadsheet_id, "Leaderboard") + values = cells["values"][1:] + values = [ + {"name": f"{value[1]} {value[2]}", "score": value[3]} for value in values + ] + return values + + +accountability_service = AccountabilityService()