Skip to content

Commit

Permalink
Tested -- code for pulling data from the Opt-In results spreadsheet,
Browse files Browse the repository at this point in the history
check user email for response, store response data in dict
  • Loading branch information
laurenwolfe-isb committed Feb 13, 2020
1 parent 61e4795 commit 07dee09
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 35 deletions.
1 change: 1 addition & 0 deletions common_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .solr_tests import *
from .metadata_utils_tests import *
from .sheets_tests import *
34 changes: 19 additions & 15 deletions common_tests/sheets_tests.py
Original file line number Diff line number Diff line change
@@ -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("[email protected]")
lwolfe2_response = OptInSupport("[email protected]")
no_response = OptInSupport("[email protected]")

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"], '[email protected]')
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()
2 changes: 1 addition & 1 deletion google_helpers/sheets/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self):
pass

@abstractmethod
def get_sheet_data(self, include_grid_data):
def get_sheet_data(self):
pass


Expand Down
33 changes: 25 additions & 8 deletions google_helpers/sheets/opt_in_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion google_helpers/sheets/sheets_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
22 changes: 12 additions & 10 deletions google_helpers/sheets/sheets_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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']

0 comments on commit 07dee09

Please sign in to comment.