Skip to content

Commit

Permalink
ucfopen#596 - Add accept and reject methods for enrollment (course in…
Browse files Browse the repository at this point in the history
…vites) (ucfopen#597)

* Solve issue 596 - Add accept and reject methods for enrollment (course invites)

* Minor update to tests. Docstring fixes. Update black version and source for precommit. Add svanderwulp to authors and add contribution to Changelog

---------

Co-authored-by: Matthew Emond <[email protected]>
  • Loading branch information
svanderwulp and Thetwam authored Apr 17, 2023
1 parent c1d52f8 commit 79a6443
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/ambv/black
rev: stable
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
language_version: python3.9
language_version: python3
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Patches and Suggestions
- Stephen Woosley [@stephenwoosley](https://github.com/stephenwoosley)
- Steven Bell [@stevenbell](https://github.com/stevenbell)
- Steven Williams [@onomou](https://github.com/onomou)
- [@svanderwulp](https://github.com/svanderwulp)
- Tinson Lai [@laitingsheng](https://github.com/laitingsheng)
- Toan Vu [@vutoan1245](https://github.com/vutoan1245)
- [@Tobiaqs](https://github.com/Tobiaqs)
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
- Update a calendar's visibility
- Update many calendars' visibility
- List all account calendars
- Enrollments (Thanks, [@svanderwulp](https://github.com/svanderwulp))
- Accept Course Invitation
- Reject Course Invitation
- JWTs (Thanks [@dmols](https://github.com/dmols))
- Create JWTs
- Refresh JWTs
Expand Down
34 changes: 34 additions & 0 deletions canvasapi/enrollment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ class Enrollment(CanvasObject):
def __str__(self):
return "{} ({})".format(self.type, self.id)

def accept(self, **kwargs):
"""
Accept a pending course invitation.
:calls: `POST /api/v1/courses/:course_id/enrollments/:id/accept \
<https://canvas.instructure.com/doc/api/enrollments.html#method.enrollments_api.accept>`_
:returns: True if the course invitation was accepted.
:rtype: bool
"""
response = self._requester.request(
"POST",
"courses/{}/enrollments/{}/accept".format(self.course_id, self.id),
_kwargs=combine_kwargs(**kwargs),
)
return response.json().get("success", False)

def deactivate(self, task, **kwargs):
"""
Delete, conclude, or deactivate an enrollment.
Expand Down Expand Up @@ -53,3 +70,20 @@ def reactivate(self, **kwargs):
_kwargs=combine_kwargs(**kwargs),
)
return Enrollment(self._requester, response.json())

def reject(self, **kwargs):
"""
Reject a pending course invitation.
:calls: `POST /api/v1/courses/:course_id/enrollments/:id/reject \
<https://canvas.instructure.com/doc/api/enrollments.html#method.enrollments_api.reject>`_
:returns: True if the course invitation was rejected.
:rtype: bool
"""
response = self._requester.request(
"POST",
"courses/{}/enrollments/{}/reject".format(self.course_id, self.id),
_kwargs=combine_kwargs(**kwargs),
)
return response.json().get("success", False)
18 changes: 17 additions & 1 deletion tests/fixtures/enrollment.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,21 @@
"state": "active"
},
"status_code": 200
},
"accept": {
"method": "POST",
"endpoint": "courses/1/enrollments/1/accept",
"data": {
"success": true
},
"status_code": 200
},
"reject": {
"method": "POST",
"endpoint": "courses/1/enrollments/1/reject",
"data": {
"success": true
},
"status_code": 200
}
}
}
18 changes: 18 additions & 0 deletions tests/test_enrollment.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,21 @@ def test_reactivate(self, m):
target_enrollment = self.enrollment.reactivate()

self.assertIsInstance(target_enrollment, Enrollment)

# accept()
def test_accept(self, m):
register_uris({"enrollment": ["accept"]}, m)

enrollment_accepted = self.enrollment.accept()

self.assertIsInstance(enrollment_accepted, bool)
self.assertTrue(enrollment_accepted)

# reject()
def test_reject(self, m):
register_uris({"enrollment": ["reject"]}, m)

enrollment_rejected = self.enrollment.reject()

self.assertIsInstance(enrollment_rejected, bool)
self.assertTrue(enrollment_rejected)

0 comments on commit 79a6443

Please sign in to comment.