From edba867aca68293b4f6caf43f1ea562d3638d5d5 Mon Sep 17 00:00:00 2001 From: Rodolfo Jordao <45004351+Rojods@users.noreply.github.com> Date: Wed, 9 Mar 2022 23:17:02 +0100 Subject: [PATCH] Better custom gradebook column CRUD Without the `{"content": column_data}`, we get a nasty `500` error everytime this algorithm tries to execute. I was surprised to hit it the first time I executed my script. The other change is to be able to set the column data for the first time. When there is already some data in the custom column, the `get_entries` routine returns and then we can modify then. But if there's nothing there in the first place, it is impossible with the current code base to add anything to start with. Therefore I propose to add in the `CustomGradebookColumn` a update the column data; this way it will be possible to create entries as well as modify them. --- canvasapi/custom_gradebook_columns.py | 30 ++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/canvasapi/custom_gradebook_columns.py b/canvasapi/custom_gradebook_columns.py index a9e5d640..5e18a39e 100644 --- a/canvasapi/custom_gradebook_columns.py +++ b/canvasapi/custom_gradebook_columns.py @@ -73,6 +73,34 @@ def reorder_custom_columns(self, order, **kwargs): ) return response.json().get("reorder") + + def update_custom_column_entry(self, user_id, column_data, **kwargs): + """ + Sets the content of a custom column or create them if inexistent. + + :calls: `PUT /api/v1/courses/:course_id/custom_gradebook_columns/:id/data/:user_id \ + `_ + + :param column_data: The content in the column. + :type column_data: str + :param user_id: The id for the user to be updated. + :type column_data: int + + :rtype: :class:`canvasapi.custom_gradebook_columns.ColumnData` + """ + + kwargs["column_data"] = {"content": column_data} + + response = self._requester.request( + "PUT", + "courses/{}/custom_gradebook_columns/{}/data/{}".format( + self.course_id, self.id, user_id + ), + _kwargs=combine_kwargs(**kwargs), + ) + + if response.json().get("content"): + return ColumnData(self._requester, response.json()) def update_custom_column(self, **kwargs): """ @@ -113,7 +141,7 @@ def update_column_data(self, column_data, **kwargs): :rtype: :class:`canvasapi.custom_gradebook_columns.ColumnData` """ - kwargs["column_data"] = column_data + kwargs["column_data"] = {"content": column_data} response = self._requester.request( "PUT",