Skip to content

Commit

Permalink
Classes for handling Opt In Form result retrieval using Sheets API, u…
Browse files Browse the repository at this point in the history
…ntested
  • Loading branch information
laurenwolfe-isb committed Feb 13, 2020
1 parent ca3643f commit 61e4795
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
30 changes: 30 additions & 0 deletions common_tests/sheets_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest

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")
data = test_sheet_obj.get_sheet_data()

self.assertTrue(type(data) is list)

"""
class TestOptInSupport(unittest.TestCase):
def test_set_user_response(self):
pass
def test_has_responded(self):
pass
"""

if __name__ == "__main__":
unittest.main()
20 changes: 19 additions & 1 deletion google_helpers/sheets/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,29 @@
from future.utils import with_metaclass


# Base Abstract class which defines the shared methods and properties for interaction with BigQuery
class SheetsABC(with_metaclass(ABCMeta, object)):
"""
Base abstract class which defines the shared methods and properties
for interaction with Sheets API.
"""
@abstractmethod
def __init__(self):
pass

@abstractmethod
def get_sheet_data(self, include_grid_data):
pass


class OptInABC(SheetsABC):
"""
Abstract base class extension that adds Opt-In Form specific methods.
"""

@abstractmethod
def set_user_response(self, user_email, executing_project):
pass

@abstractmethod
def has_responded(self):
pass
67 changes: 67 additions & 0 deletions google_helpers/sheets/opt_in_support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#
# Copyright 2015-2020, Institute for Systems Biology
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from google_helpers.sheets.sheets_support import SheetsSupport

SPREADSHEET_ID = "1FUZqWZv5drJDH4kqi0pU-oau0qVhFKevhbKRMMZrLmA"
SHEET_ID = "Sheet1"

class OptInSupport(SheetsSupport):
"""
Child class of SheetsSupport that adds opt-in form specific fields and methods.
"""

def __init__(self, user_email):
"""
OptInSupport constructor method.
:param user_email: email address of user beginning/ending session
"""
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
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]
"""
# preset to None in case no response result is found
user_response = None

responses = self.get_sheet_data()

user_email = user_email.strip().lower()

for response in responses:
response_email = response[1].strip().lower()

if response_email == user_email:
user_response = response
break

return user_response

def has_responded(self):
"""
Checks to see if user has submitted opt-in form.
:return: True if user has responded, else False
"""
responded = False if not self.user_response else True
return responded
13 changes: 13 additions & 0 deletions google_helpers/sheets/sheets_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,16 @@ def __init__(self, sheet_id, spreadsheet_id):

self.sheet_service = get_sheet_service()

def get_sheet_data(self, include_grid_data=True):
"""
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
:return: List (or list of lists) of retrieved data.
"""
request = self.sheet_service.spreadsheets().get(
spreadsheetId=self.spreadsheet_id,
ranges=self.sheet_id,
include_grid_data=include_grid_data
)

return request.execute()

0 comments on commit 61e4795

Please sign in to comment.