Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more methods with unittests #10

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
833e4c0
adjusted that token_id is returned as str in test_list_participants_s…
Jan 3, 2018
75ef36c
added possibility to name a survey id that should be used for the tes…
Jan 4, 2018
d98465a
small change in docstring of add_participants
Jan 4, 2018
2aaa9d2
added lsa txt and xml survey file in fixtures
Jan 4, 2018
b0eb5d7
added copy_survey (not working) and import_survey incl tests
Jan 4, 2018
751cf15
added delete_survey and tests
Jan 4, 2018
3f8bc06
delete_survey tests
Jan 4, 2018
efbad3c
cleaned master branch from new methods
Jan 4, 2018
b6d70fb
added method copy_survey and unittests, not working so far
Jan 4, 2018
ddb5468
added import_survey and tests
Jan 4, 2018
7caeabd
added delete_survey and tests
Jan 4, 2018
72921a0
added remind_participants and tests
Jan 4, 2018
365ba5f
added unittests for optional parameters
Jan 4, 2018
6bc046c
added export_responses and basic unittest
Jan 4, 2018
b105763
Merge pull request #1 from ESchae/remind_participants
ESchae Jan 4, 2018
325b199
Merge pull request #2 from ESchae/export_responses
ESchae Jan 4, 2018
d1961ee
Merge branch 'master' into delete_survey
ESchae Jan 4, 2018
666ef41
Merge pull request #3 from ESchae/delete_survey
ESchae Jan 4, 2018
0320ab5
changed response_type in export_response assert to str
Jan 4, 2018
e2d3c8d
added method import_survey
Jan 4, 2018
e6bb041
added unittests for import_survey
Jan 4, 2018
cebc309
Merge pull request #4 from ESchae/import_survey2
ESchae Jan 4, 2018
f4a180e
Update README.md
ESchae Jan 4, 2018
e533ea8
added activate_survey
Jan 4, 2018
03e087c
added unittest for activate_survey
Jan 4, 2018
55617e3
Merge pull request #5 from ESchae/activate_survey
ESchae Jan 4, 2018
c488b9c
Added activate_survey as implemented in README
ESchae Jan 4, 2018
8e4a51d
two small changes in docstrings
Jan 4, 2018
a039347
corrected warning for != lss in import_survey
ESchae Jan 7, 2018
e4c7467
added method activate_tokens + tests
Jan 8, 2018
50625e5
added activate_tokens in README
Jan 8, 2018
8d2e8b4
update on author name and mail
ESchae Jan 12, 2018
e0d4918
deleted unneeded variable
Jan 20, 2018
527da68
added list_groups
Jan 20, 2018
84e6d4d
Merge pull request #6 from ESchae/list_groups
ESchae Jan 20, 2018
49c2bbc
added list_groups
Jan 20, 2018
5f6b70d
Merge pull request #7 from ESchae/list_groups
ESchae Jan 20, 2018
2394bbe
add get_motion_name in FileStructure taken from old MDR
ESchae Apr 13, 2018
2728c85
add initial travis file
Nov 29, 2018
f363ece
add list_group
Nov 29, 2018
31a1ff8
add list_group
Nov 29, 2018
f80830f
Merge branch 'master' of https://github.com/ESchae/limesurveyrc2api
Nov 29, 2018
cb61e46
change script to run unittest
Nov 29, 2018
7d6a035
add config.ini such that travis can use it
Nov 29, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions .idea/limesurveyrc2api.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: python
python:
- "3.6"
script:
- python -m unittest
7 changes: 7 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@ It's just a start, so the list of implemented methods is shorter than not.
- Survey
+ list_surveys
+ list_questions
+ delete_survey
+ export_responses
+ import_survey
+ activate_survey
+ activate_tokens
+ list_groups
- Token
+ add_participants
+ delete_participants
+ get_participant_properties
+ get_summary
+ invite_participants
+ list_participants
+ remind_participants


### Error Handling
Expand Down
239 changes: 238 additions & 1 deletion limesurveyrc2api/_survey.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import warnings
from collections import OrderedDict
from limesurveyrc2api.exceptions import LimeSurveyError

from os.path import splitext
from base64 import b64encode

class _Survey(object):

Expand Down Expand Up @@ -76,3 +78,238 @@ def list_questions(self, survey_id,
else:
assert response_type is list
return response

def delete_survey(self, survey_id):
""" Delete a survey.

Parameters
:param survey_id: The ID of the Survey to be deleted.
:type: Integer
"""
method = "delete_survey"
params = OrderedDict([
("sSessionKey", self.api.session_key),
("iSurveyID", survey_id)
])
response = self.api.query(method=method, params=params)
response_type = type(response)

if response_type is dict and "status" in response:
status = response["status"]
error_messages = [
"No permission",
"Invalid session key"
]
for message in error_messages:
if status == message:
raise LimeSurveyError(method, status)
else:
assert response_type is list
return response

def export_responses(self, survey_id, document_type, language_code=None,
completion_status='all', heading_type='code',
response_type='short', from_response_id=None,
to_response_id=None, fields=None):
""" Export responses in base64 encoded string.

Parameters
:param survey_id: Id of the Survey.
:type survey_id: Integer
:param document_type: Any format available by plugins
(e.g. pdf, csv, xls, doc, json)
:type document_type: String
:param language_code: (optional) The language to be used.
:type language_code: String
:param completion_status: (optional) 'complete', 'incomplete' or 'all'
:type completion_status: String
:param heading_type: (optional) 'code', 'full' or 'abbreviated'
:type heading_type: String
:param response_type: (optional) 'short' or 'long'
:type response_type: String
:param from_response_id: (optional)
:type from_response_id: Integer
:param to_response_id: (optional)
:type to_response_id: Integer
:param fields: (optional) Selected fields.
:type fields: Array
"""
method = "export_responses"
params = OrderedDict([
("sSessionKey", self.api.session_key),
("iSurveyID", survey_id),
("sDocumentType", document_type),
("sLanguageCode", language_code),
("sCompletionStatus", completion_status),
("sHeadingType", heading_type),
("sResponseType", response_type),
("iFromResponseID", from_response_id),
("iToResponseID", to_response_id),
("aFields", fields)
])
response = self.api.query(method=method, params=params)
response_type = type(response)

if response_type is dict and "status" in response:
status = response["status"]
error_messages = [
"Language code not found for this survey.",
"No Data, could not get max id.",
"No Data, survey table does not exist",
"No permission",
"Invalid session key"
]
for message in error_messages:
if status == message:
raise LimeSurveyError(method, status)
else:
assert response_type is str
return response

def import_survey(self, path_to_import_survey, new_name=None,
dest_survey_id=None):
""" Import a survey. Allowed formats: lss, csv, txt or lsa

Parameters
:param path_to_import_survey: Path to survey as file to copy.
:type path_to_import_survey: String
:param new_name: (optional) The optional new name of the survey
Important! Seems only to work if lss file is given!
:type new_name: String
:param dest_survey_id: (optional) This is the new ID of the survey -
if already used a random one will be taken instead
:type dest_survey_id: Integer
"""
import_datatype = splitext(path_to_import_survey)[1][1:]
# TODO: Naming seems only to work with lss files - why?
if import_datatype != 'lss' and new_name:
warnings.warn("New naming seems only to work with lss files",
RuntimeWarning)
# encode import data
with open(path_to_import_survey, 'rb') as f:
# import data must be a base 64 encoded string
import_data = b64encode(f.read())
# decoding needed because json.dumps() in method get of
# class LimeSurvey can not encode bytes
import_data = import_data.decode('ascii')

method = "import_survey"
params = OrderedDict([
("sSessionKey", self.api.session_key),
("sImportData", import_data),
("sImportDataType", import_datatype),
("sNewSurveyName", new_name),
("DestSurveyID", dest_survey_id)
])
response = self.api.query(method=method, params=params)
response_type = type(response)

if response_type is dict and "status" in response:
status = response["status"]
error_messages = [
"Error: ...", # TODO: Unclear what might be returned here
"Invalid extension",
"No permission",
"Invalid session key"
]
for message in error_messages:
if status == message:
raise LimeSurveyError(method, status)
else:
assert response_type is int # the new survey id
return response

def activate_survey(self, survey_id):
""" Activate an existing survey.

Parameters
:param survey_id: Id of the Survey to be activated.
:type survey_id: Integer
"""
method = "activate_survey"
params = OrderedDict([
("sSessionKey", self.api.session_key),
("iSurveyID", survey_id)
])
response = self.api.query(method=method, params=params)
response_type = type(response)

if response_type is dict and "status" in response:
status = response["status"]
error_messages = [
"Error: Invalid survey ID",
"Error: ...", # TODO: what could be output of ActivateResults?
"No permission",
"Invalid session key"
]
for message in error_messages:
if status == message:
raise LimeSurveyError(method, status)
else:
assert response_type is list
return response

def activate_tokens(self, survey_id, attribute_fields=[]):
"""

Parameters
:param survey_id: ID of the Survey where a participants table will
be created for.
:type survey_id: Integer
:param attribute_fields: An array of integer describing any additional
attribute fiields.
:type attribute_fields: Array
"""
method = "activate_tokens"
params = OrderedDict([
("sSessionKey", self.api.session_key),
("iSurveyId", survey_id),
("aAttributeFields", attribute_fields)
])
response = self.api.query(method=method, params=params)
response_type = type(response)

if response_type is dict and "status" in response:
status = response["status"]
error_messages = [
"Error: Invalid survey ID",
"Survey participants table could not be created",
"No permission",
"Invalid session key"
]
for message in error_messages:
if status == message:
raise LimeSurveyError(method, status)
else:
assert response_type is list
return response

def list_groups(self, survey_id):
""" Return the ids and all attributes of groups belonging to survey.

Parameters
:param survey_id: ID of the survey containing the groups.
:rtype survey_id: Integer
"""
method = "list_groups"
params = OrderedDict([
("sSessionKey", self.api.session_key),
("iSurveyID", survey_id)
])
response = self.api.query(method=method, params=params)
response_type = type(response)

if response_type is dict and "status" in response:
status = response["status"]
error_messages = [
"Error: Invalid survey ID",
"No groups found",
"No permission"
"Invalid S ession key" # typo in remotecontrol_handle.php
]
for message in error_messages:
if status == message:
raise LimeSurveyError(method, status)
else:
assert response_type is list
return response
47 changes: 43 additions & 4 deletions limesurveyrc2api/_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def add_participants(
Add participants to the specified survey.

Parameters
:param survey_id: ID of survey to delete participants from.
:param survey_id: ID of survey to add participants.
:type survey_id: Integer
:param participant_data: List of participant detail dictionaries.
:type participant_data: List[Dict]
Expand Down Expand Up @@ -277,6 +277,45 @@ def list_participants(
assert response_type is list
return response

def remind_participants(self):
# TODO
raise NotImplementedError
def remind_participants(self, survey_id, min_days_between=None,
max_reminders=None, token_ids=False):
""" Send a reminder to participants in a survey.

Returns result of sending.

Parameters
:param survey_id: ID of the Survey that participants belong.
:type survey_id: Integer
:param min_days_between: (optional) Days from last reminder.
:type min_days_between: Integer
:param max_reminders: (optional) Maximum reminders count.
:type max_reminders: Integer
:param token_ids: (optional filter) IDs of the participant to remind.
:type token_ids: array
"""
method = "remind_participants"
params = OrderedDict([
("sSessionKey", self.api.session_key),
("iSurveyID", survey_id),
("iMinDaysBetween", min_days_between),
("iMaxReminders", max_reminders),
("aTokenIds", token_ids)
])
response = self.api.query(method=method, params=params)
response_type = type(response)

if response_type is dict and "status" in response:
status = response["status"]
error_messages = [
"Error: No survey participants table",
"Error: No candidate tokens",
"Error: Invalid survey ID",
"No permission",
"Invalid Session Key"
]
for message in error_messages:
if status == message:
raise LimeSurveyError(method, status)
else:
assert response_type is list
return response
4 changes: 2 additions & 2 deletions setup.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
version=__version__,
description="LimeSurvey RC2 API Web Services Client",
url="https://github.com/lindsay-stevens",
author="Lindsay Stevens",
author_email="[email protected]",
author="Lindsay Stevens, forked by Elke Schaechtele",
author_email="[email protected], [email protected]",
packages=["limesurveyrc2api"],
test_suite="tests",
include_package_data=True,
Expand Down
5 changes: 5 additions & 0 deletions tests/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[test]
url = http://localhost/index.php/admin/remotecontrol
username = admin
password = admin
survey_id = 369411
1 change: 1 addition & 0 deletions tests/config.ini.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
url = http://localhost/limesurvey/index.php/admin/remotecontrol
username = admin
password = admin
survey_id = 0
Binary file not shown.
Loading