diff --git a/canvasapi/canvas.py b/canvasapi/canvas.py index bc6501af..1b87e208 100644 --- a/canvasapi/canvas.py +++ b/canvasapi/canvas.py @@ -211,6 +211,8 @@ def create_appointment_group(self, appointment_group, **kwargs): :type appointment_group: `dict` :param title: The title of the appointment group. :type title: `str` + :param context_codes: Course IDs for the appointment group. + :type context_codes: `list` :rtype: :class:`canvasapi.appointment_group.AppointmentGroup` """ if ( @@ -218,6 +220,24 @@ def create_appointment_group(self, appointment_group, **kwargs): and "context_codes" in appointment_group and "title" in appointment_group ): + # Both properties exist, check the context codes list + if ( + isinstance(appointment_group["context_codes"][0], str) + and "course_" in appointment_group["context_codes"][0] + ): + # Legacy support for context codes passed as list of `course_123`-style strings + # Everything is formatted correctly, so go ahead and move on + pass + else: + # The type of object in `context_codes` is taken care of by obj_or_id, extracting + # the course ID from a object or by returning plain strings. + course_ids = [ + obj_or_id(course_id, "context_codes", (Course,)) + for course_id in appointment_group["context_codes"] + ] + appointment_group["context_codes"] = course_ids + + # Everything is formatted properly to send. kwargs["appointment_group"] = appointment_group elif ( @@ -530,16 +550,24 @@ def get_appointment_group(self, appointment_group, **kwargs): ) return AppointmentGroup(self.__requester, response.json()) - def get_appointment_groups(self, **kwargs): + def get_appointment_groups(self, group_type="reservable", **kwargs): """ List appointment groups. + This returns "reservable" appointment groups by default, + but will result in an empty list for teachers. + Set the `group_type` parameter to "manageable" to override. + :calls: `GET /api/v1/appointment_groups \ `_ + :param group_type: Type of `AppointmentGroup` objects to return. Defaults to "reservable". + :type group_type: `str` :rtype: :class:`canvasapi.paginated_list.PaginatedList` of :class:`canvasapi.appointment_group.AppointmentGroup` """ + kwargs["scope"] = group_type + return PaginatedList( AppointmentGroup, self.__requester, diff --git a/tests/fixtures/appointment_group.json b/tests/fixtures/appointment_group.json index 2c7a41e3..ed9c6545 100644 --- a/tests/fixtures/appointment_group.json +++ b/tests/fixtures/appointment_group.json @@ -67,6 +67,23 @@ ], "status_code": 200 }, + "list_manageable_appointment_groups": { + "method": "GET", + "endpoint": "appointment_groups", + "data": [ + { + "id": 999, + "context_codes": ["course_123"], + "title": "Test Group 1" + }, + { + "id": 888, + "context_codes": ["course_456"], + "title": "Test Group 2" + } + ], + "status_code": 200 + }, "list_user_participants": { "method": "GET", "endpoint": "appointment_groups/222/users", diff --git a/tests/test_canvas.py b/tests/test_canvas.py index 8533565e..056be2a7 100644 --- a/tests/test_canvas.py +++ b/tests/test_canvas.py @@ -635,6 +635,14 @@ def test_get_appointment_groups(self, m): appt_groups_list = [appt_group for appt_group in appt_groups] self.assertEqual(len(appt_groups_list), 2) + # get_appointment_group(), not reservable + def test_get_manageable_appointment_groups(self, m): + register_uris({"appointment_group": ["list_manageable_appointment_groups"]}, m) + + appt_groups = self.canvas.get_appointment_groups(group_type="manageable") + appt_groups_list = [appt_group for appt_group in appt_groups] + self.assertEqual(len(appt_groups_list), 2) + # get_appointment_group() def test_get_appointment_group(self, m): register_uris({"appointment_group": ["get_appointment_group"]}, m) @@ -661,6 +669,17 @@ def test_create_appointment_group(self, m): self.assertEqual(evnt.context_codes[0], "course_123") self.assertEqual(evnt.id, 234) + def test_create_appointment_group_with_course_ids(self, m): + register_uris({"appointment_group": ["create_appointment_group"]}, m) + + evnt = self.canvas.create_appointment_group( + {"context_codes": [123], "title": "Test Group"} + ) + + self.assertIsInstance(evnt, AppointmentGroup) + self.assertEqual(evnt.context_codes[0], "course_123") + self.assertEqual(evnt.id, 234) + def test_create_appointment_group_fail_on_context_codes(self, m): with self.assertRaises(RequiredFieldMissing): self.canvas.create_appointment_group({"title": "Test Group"})