Skip to content

Commit

Permalink
Merge pull request redcap-tools#29 from sburns/add-new-export-parameters
Browse files Browse the repository at this point in the history
Add new export parameters
  • Loading branch information
Scott Burns committed Jan 22, 2014
2 parents 328aed9 + 4e26ce1 commit d3e204e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
20 changes: 17 additions & 3 deletions redcap/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ def export_metadata(self, fields=None, forms=None, format='obj',

def export_records(self, records=None, fields=None, forms=None,
events=None, raw_or_label='raw', event_name='label',
format='obj', df_kwargs=None):
format='obj', export_survey_fields=False,
export_data_access_groups=False, df_kwargs=None):
"""Return data
High level function of Project
Expand Down Expand Up @@ -196,6 +197,18 @@ def export_records(self, records=None, fields=None, forms=None,
Format of returned data. 'obj' returns json-decoded objects
'csv' and 'xml' return other formats. 'df' will attempt to
return a pandas.DataFrame.
export_survey_fields: True | False [default]
specifies whether or not to export the survey identifier
field (e.g., "redcap_survey_identifier") or survey timestamp
fields (e.g., form_name+"_timestamp") when surveys are
utilized in the project.
export_data_access_groups: True | False [default]
specifies whether or not to export the
"redcap_data_access_group" field when data access groups
are utilized in the project. NOTE: This flag is only viable
if the user whose token is being used to make the API
request is *not* in a data access group. If the user is
in a group, then this flag will revert to its default value.
df_kwargs: dict [default: {'index_col': self.def_field}]
Passed to pandas.read_csv to control construction of
returned DataFrame
Expand All @@ -209,9 +222,10 @@ def export_records(self, records=None, fields=None, forms=None,
ret_format = 'csv'
pl = self.__basepl('record', format=ret_format)
keys_to_add = (records, fields, forms, events,
raw_or_label, event_name)
raw_or_label, event_name, export_survey_fields,
export_data_access_groups)
str_keys = ('records', 'fields', 'forms', 'events', 'rawOrLabel',
'eventName')
'eventName', 'exportSurveyFields', 'exportDataAccessGroups')
for key, data in zip(str_keys, keys_to_add):
if data:
# Make a url-ok string
Expand Down
44 changes: 36 additions & 8 deletions test/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
class ProjectTests(unittest.TestCase):
"""docstring for ProjectTests"""

def setUp(self):
self.url = 'https://redcap.vanderbilt.edu/api/'
self.bad_url = 'https://redcap.vanderbilt.edu/api'
self.reg_token = '8E66DB6844D58E990075AFB51658A002'
self.long_proj = Project(self.url, '1387872621BBF1C17CC47FD8AE25FF54')
self.reg_proj = Project(self.url, self.reg_token)
self.ssl_proj = Project(self.url, self.reg_token,
verify_ssl=False)
url = 'https://redcap.vanderbilt.edu/api/'
bad_url = 'https://redcap.vanderbilt.edu/api'
reg_token = '8E66DB6844D58E990075AFB51658A002'
long_proj = Project(url, '1387872621BBF1C17CC47FD8AE25FF54')
reg_proj = Project(url, reg_token)
ssl_proj = Project(url, reg_token, verify_ssl=False)
survey_proj = Project(url, '37CAB1ABC2FEB3BB9D821DF13BA38A7B')

def setUp(self):
pass
def tearDown(self):
pass

Expand Down Expand Up @@ -195,6 +196,33 @@ def test_verify_ssl(self):
self.assertIn('verify', post_kwargs)
self.assertTrue(post_kwargs['verify'])

def test_export_data_access_groups(self):
"""Test we get 'redcap_data_access_group' in exported data"""
records = self.reg_proj.export_records(export_data_access_groups=True)
for record in records:
self.assertIn('redcap_data_access_group', record)
# When not passed, that key shouldn't be there
records = self.reg_proj.export_records()
for record in records:
self.assertNotIn('redcap_data_access_group', record)

def test_export_survey_fields(self):
"""Test that we get the appropriate survey keys in the exported
data.
Note that the 'demographics' form has been setup as the survey
in the `survey_proj` project. The _timestamp field will vary for
users as their survey form will be named differently"""
records = self.survey_proj.export_records(export_survey_fields=True)
for record in records:
self.assertIn('redcap_survey_identifier', record)
self.assertIn('demographics_timestamp', record)
# The regular project doesn't have a survey setup. Users should
# be able this argument as True but it winds up a no-op.
records = self.reg_proj.export_records(export_survey_fields=True)
for record in records:
self.assertNotIn('redcap_survey_identifier', record)
self.assertNotIn('demographics_timestamp', record)

@unittest.skipIf(skip_pd, "Couldn't import pandas")
def test_metadata_to_df(self):
Expand Down

0 comments on commit d3e204e

Please sign in to comment.