diff --git a/common_tests/__init__.py b/common_tests/__init__.py index e2747122..ef50240b 100644 --- a/common_tests/__init__.py +++ b/common_tests/__init__.py @@ -1,2 +1,3 @@ from .solr_tests import * from .metadata_utils_tests import * +from .sheets_tests import * diff --git a/common_tests/sheets_tests.py b/common_tests/sheets_tests.py index d35b0d5a..9127e662 100644 --- a/common_tests/sheets_tests.py +++ b/common_tests/sheets_tests.py @@ -1,30 +1,34 @@ import unittest +from google_helpers.sheets.sheets_support import SheetsSupport +from google_helpers.sheets.opt_in_support import OptInSupport -try: - from google_helpers.sheets.sheets_support import SheetsSupport -except ImportError: - print("[ERROR] Failed to import test subject: {}".format("SheetsSupport")) - -try: - from google_helpers.sheets.opt_in_support import OptInSupport -except ImportError: - print("[ERROR] Failed to import test subject: {}".format("OptInSupport")) class TestSheetsSupport(unittest.TestCase): def test_get_sheet_data(self): - test_sheet_obj = SheetsSupport("Sheet1", "1FUZqWZv5drJDH4kqi0pU-oau0qVhFKevhbKRMMZrLmA") + test_sheet_obj = SheetsSupport("Form Responses 1", "1FUZqWZv5drJDH4kqi0pU-oau0qVhFKevhbKRMMZrLmA") data = test_sheet_obj.get_sheet_data() self.assertTrue(type(data) is list) -""" + class TestOptInSupport(unittest.TestCase): - def test_set_user_response(self): - pass + lwolfe_response = OptInSupport("lwolfe@systemsbiology.org") + lwolfe2_response = OptInSupport("lwolfe2@systemsbiology.org") + no_response = OptInSupport("non-existant@test.org") def test_has_responded(self): - pass -""" + self.assertTrue(self.lwolfe_response.has_responded()) + + self.assertFalse(self.no_response.has_responded()) + + def test_returned_data(self): + self.assertEqual(self.lwolfe_response.user_response["timestamp"], '2/5/2020 15:20:48') + self.assertEqual(self.lwolfe_response.user_response["email"], 'lwolfe@systemsbiology.org') + self.assertEqual(self.lwolfe_response.user_response["name"], 'Lauren Wolfe') + self.assertEqual(self.lwolfe_response.user_response["affiliation"], 'ISB-CGC') + self.assertEqual(self.lwolfe_response.user_response["comments"], None) + self.assertEqual(self.lwolfe2_response.user_response["comments"], "Test comments") + if __name__ == "__main__": unittest.main() diff --git a/google_helpers/sheets/abstract.py b/google_helpers/sheets/abstract.py index 2863915f..6762ab83 100644 --- a/google_helpers/sheets/abstract.py +++ b/google_helpers/sheets/abstract.py @@ -29,7 +29,7 @@ def __init__(self): pass @abstractmethod - def get_sheet_data(self, include_grid_data): + def get_sheet_data(self): pass diff --git a/google_helpers/sheets/opt_in_support.py b/google_helpers/sheets/opt_in_support.py index 0ad21109..f8043956 100644 --- a/google_helpers/sheets/opt_in_support.py +++ b/google_helpers/sheets/opt_in_support.py @@ -17,7 +17,8 @@ from google_helpers.sheets.sheets_support import SheetsSupport SPREADSHEET_ID = "1FUZqWZv5drJDH4kqi0pU-oau0qVhFKevhbKRMMZrLmA" -SHEET_ID = "Sheet1" +SHEET_ID = "Form Responses 1" + class OptInSupport(SheetsSupport): """ @@ -31,32 +32,48 @@ def __init__(self, user_email): """ super(OptInSupport, self).__init__(SHEET_ID, SPREADSHEET_ID) - # None (if no response) or list containing the user's response fields - # Indices: 0=Timestamp, 1=Email, 2=Name, 3=Affiliation, 4=Ok to contact, 5=Comments + # None (if no response) or dict containing the retrieved response. + # Fields: timestamp, email, name, affiliation, can_contact, comments self.user_response = self.set_user_response(user_email) def set_user_response(self, user_email): """ Retrieves user response data from google sheet and sets the instance variable. :param user_email: user email for which to retrieve record - :return: None if no user response, else list containing user response fields [Timestamp, Email, Name, Affiliation, Ok to contact, Comments] + :return: None if no user response, else dict of response data """ - # preset to None in case no response result is found - user_response = None - responses = self.get_sheet_data() user_email = user_email.strip().lower() + # convert email strings so that comparisons are valid even with minor formatting differences for response in responses: + user_response = None response_email = response[1].strip().lower() if response_email == user_email: user_response = response break - return user_response + if user_response: + # putting values into dictionary for readability + response_dict = { + "timestamp": user_response[0], + "email": user_response[1], + "name": user_response[2], + "affiliation": user_response[3], + "can_contact": user_response[4] + } + + if len(user_response) == 6: + response_dict["comments"] = user_response[5] + else: + response_dict["comments"] = None + + return response_dict + else: + return None def has_responded(self): """ diff --git a/google_helpers/sheets/sheets_service.py b/google_helpers/sheets/sheets_service.py index 3b457c27..ef6c07c2 100644 --- a/google_helpers/sheets/sheets_service.py +++ b/google_helpers/sheets/sheets_service.py @@ -17,7 +17,7 @@ from oauth2client.client import GoogleCredentials from django.conf import settings import httplib2 -from .utils import build_with_retries +from google_helpers.utils import build_with_retries def get_sheet_service(): diff --git a/google_helpers/sheets/sheets_support.py b/google_helpers/sheets/sheets_support.py index 8933165a..f81857b6 100644 --- a/google_helpers/sheets/sheets_support.py +++ b/google_helpers/sheets/sheets_support.py @@ -22,8 +22,8 @@ from uuid import uuid4 import copy from django.conf import settings -from .abstract import SheetsABC -from .sheets_service import get_sheet_service +from google_helpers.sheets.abstract import SheetsABC +from google_helpers.sheets.sheets_service import get_sheet_service logger = logging.getLogger('main_logger') @@ -33,18 +33,20 @@ def __init__(self, sheet_id, spreadsheet_id): self.sheet_id = sheet_id self.spreadsheet_id = spreadsheet_id - self.sheet_service = get_sheet_service() + self.sheet_service = get_sheet_service().spreadsheets() - def get_sheet_data(self, include_grid_data=True): + def get_sheet_data(self): """ - Retrieve data from Google Sheet for a specified data range. - :param include_grid_data: TODO: clarify if this does what I think, retrieve the actual data vs just metadata + Retrieve list of lists representing rows and columns of Google Sheet for a specified data range. :return: List (or list of lists) of retrieved data. """ - request = self.sheet_service.spreadsheets().get( + + request = self.sheet_service.values().get( spreadsheetId=self.spreadsheet_id, - ranges=self.sheet_id, - include_grid_data=include_grid_data + range=self.sheet_id ) - return request.execute() + response = request.execute() + + # Strips away additional metadata from response, just returns the data contained in cells + return response['values']