Skip to content

Commit 49bb303

Browse files
authored
Merge pull request #36 from dcastrowa/addRemoveChallenge
Add new project api functionality
2 parents b6f0034 + 2718f8b commit 49bb303

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

maproulette/api/project.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,96 @@ def create_project(self, data):
8585
body=data)
8686
return response
8787

88+
def add_challenge_to_project(self, project_id, challenge_id):
89+
"""
90+
Method to add a challenge to a virtual project.
91+
92+
:param project_id: the id of the virtual project
93+
:param challenge_id: the id of the challenge being added
94+
:return: the API response from the POST request
95+
"""
96+
response = self.post(
97+
endpoint=f"/project/{project_id}/challenge/{challenge_id}/add"
98+
)
99+
return response
100+
101+
def remove_challenge_from_project(self, project_id, challenge_id):
102+
"""
103+
Method to remove a challenge from a virtual project.
104+
105+
:param project_id: the id of the virtual project
106+
:param challenge_id: the id of the challenge being removed
107+
:return: the API response from the POST request
108+
"""
109+
response = self.post(
110+
endpoint=f"/project/{project_id}/challenge/{challenge_id}/remove"
111+
)
112+
return response
113+
114+
def delete_project(self, project_id):
115+
"""
116+
Method to delete a project.
117+
118+
:param project_id: the id of the project being deleted
119+
:return: the API response form the DELETE request
120+
"""
121+
response = self.delete(
122+
endpoint=f"/project/{project_id}"
123+
)
124+
return response
125+
126+
def update_project(self, project_id, data):
127+
"""
128+
Method to update an existing project
129+
130+
:param project_id: the id of the project being updated
131+
:param data: the data to use to update the project
132+
:return: the API response from the PUT request
133+
"""
134+
if self.is_project_model(data):
135+
data = ProjectModel.to_dict(data)
136+
response = self.put(
137+
endpoint=f"/project/{project_id}",
138+
body=data)
139+
return response
140+
141+
def get_projects_by_ids(self, project_ids):
142+
"""
143+
Method to retrieve projects from comma separated list of ids
144+
145+
:param project_ids: comma separated list of project ids to be retrieved
146+
:return: the API response from the GET request
147+
"""
148+
query_params = {
149+
"projectIds": project_ids
150+
}
151+
response = self.get(
152+
endpoint=f"/projectsById",
153+
params=query_params
154+
)
155+
return response
156+
157+
def get_random_tasks(self, project_id, limit=1, proximity=-1, search=''):
158+
"""
159+
Method to retrieve random tasks from a project.
160+
161+
:param project_id: the id of the parent project of tasks
162+
:param limit: limit amount of results returned
163+
:param proximity: task to find based on proximity of that task
164+
:param search: a search parameter object stored in a cookie
165+
:return: the API response form the GET request
166+
"""
167+
query_params = {
168+
"limit": str(limit),
169+
"proximity": str(proximity),
170+
"search": str(search)
171+
}
172+
response = self.get(
173+
endpoint=f"/project/{project_id}/tasks",
174+
params=query_params
175+
)
176+
return response
177+
88178
@staticmethod
89179
def is_project_model(input_object):
90180
"""Method to determine whether user input is a valid project model

tests/test_project_api.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,61 @@ def test_create_project(self, mock_request, api_instance=api):
4444
mock_request.return_value.status_code = '200'
4545
response = api_instance.create_project(test_project_model)
4646
self.assertEqual(response['status'], '200')
47+
48+
@patch('maproulette.api.maproulette_server.requests.Session.post')
49+
def test_add_challenge_to_project(self, mock_request, api_instance=api):
50+
test_virtual_project_model = maproulette.ProjectModel(name='Test Virtual Project Name',
51+
id=1234,
52+
is_virtual=True)
53+
test_challenge_model = maproulette.ChallengeModel(name='Test Challenge Name',
54+
id=246)
55+
test_virtual_project_id = test_virtual_project_model.id
56+
test_challenge_id = test_challenge_model.id
57+
mock_request.return_value.status_code = '200'
58+
response = api_instance.add_challenge_to_project(test_virtual_project_id, test_challenge_id)
59+
self.assertEqual(response['status'], '200')
60+
61+
@patch('maproulette.api.maproulette_server.requests.Session.post')
62+
def test_remove_challenge_from_project(self, mock_request, api_instance=api):
63+
test_virtual_project_model = maproulette.ProjectModel(name='Test Virtual Project Name',
64+
id=1234,
65+
is_virtual=True)
66+
test_challenge_model = maproulette.ChallengeModel(name='Test Challenge Name', id=246)
67+
test_virtual_project_id = test_virtual_project_model.id
68+
test_challenge_id = test_challenge_model.id
69+
mock_request.return_value.status_code = '200'
70+
response = api_instance.remove_challenge_from_project(test_virtual_project_id, test_challenge_id)
71+
self.assertEqual(response['status'], '200')
72+
73+
@patch('maproulette.api.maproulette_server.requests.Session.delete')
74+
def test_delete_project(self, mock_request, api_instance=api):
75+
test_project_model = maproulette.ProjectModel(name='Test Project Name', id=1234)
76+
test_project_id = test_project_model.id
77+
mock_request.return_value.status_code = '200'
78+
response = api_instance.delete_project(test_project_id)
79+
self.assertEqual(response['status'], '200')
80+
81+
@patch('maproulette.api.maproulette_server.requests.Session.put')
82+
def test_update_project(self, mock_request, api_instance=api):
83+
test_project_model = maproulette.ProjectModel(name='Test Project Name', id=1234)
84+
test_updated_project_model = maproulette.ProjectModel(name='Test Updated Project Name')
85+
test_project_model_id = test_project_model.id
86+
mock_request.return_value.status_code = '200'
87+
response = api_instance.update_project(test_project_model_id, test_updated_project_model)
88+
self.assertEqual(response['status'], '200')
89+
90+
@patch('maproulette.api.maproulette_server.requests.Session.get')
91+
def test_get_project_by_ids(self, mock_request, api_instance=api):
92+
test_project_ids = '1234,2468,1356'
93+
mock_request.return_value.status_code = '200'
94+
response = api_instance.get_projects_by_ids(test_project_ids)
95+
self.assertEqual(response['status'], '200')
96+
97+
@patch('maproulette.api.maproulette_server.requests.Session.get')
98+
def test_get_random_tasks(self, mock_request, api_instance=api):
99+
test_project_model = maproulette.ProjectModel(name='Test Project Name',
100+
id=1234)
101+
test_project_id = test_project_model.id
102+
mock_request.return_value.status_code = '200'
103+
response = api_instance.get_random_tasks(test_project_id)
104+
self.assertEqual(response['status'], '200')

0 commit comments

Comments
 (0)