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

Course Create Discussion Attach Files #621 #622

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

- Added support for pagination with metadata when headers are missing (Thanks, [@bennettscience](https://github.com/bennettscience))

### Bugfixes

- Fixed an issue where `Course.create_discussion_topic` wouldn't accept attachment files.

## [3.1.0] - 2023-04-21

### New Endpoint Coverage
Expand Down
30 changes: 21 additions & 9 deletions canvasapi/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,25 +279,37 @@ def create_custom_column(self, column, **kwargs):

return CustomGradebookColumn(self._requester, column_json)

def create_discussion_topic(self, **kwargs):
def create_discussion_topic(self, attachment=None, **kwargs):
"""
Creates a new discussion topic for the course or group.

:calls: `POST /api/v1/courses/:course_id/discussion_topics \
<https://canvas.instructure.com/doc/api/discussion_topics.html#method.discussion_topics.create>`_

:param attachment: (Optional) A file handler or path of the file to import.
:type attachment: file or str

:rtype: :class:`canvasapi.discussion_topic.DiscussionTopic`
"""
response = self._requester.request(
"POST",
"courses/{}/discussion_topics".format(self.id),
_kwargs=combine_kwargs(**kwargs),
)
if attachment is not None:
attachment_file, is_path = file_or_path(attachment)
attachment = {"attachment": attachment_file}

response_json = response.json()
response_json.update({"course_id": self.id})
try:
response = self._requester.request(
"POST",
"courses/{}/discussion_topics".format(self.id),
file=attachment,
_kwargs=combine_kwargs(**kwargs),
)

return DiscussionTopic(self._requester, response_json)
response_json = response.json()
response_json.update({"course_id": self.id})

return DiscussionTopic(self._requester, response_json)
finally:
if attachment is not None and is_path:
attachment_file.close()

def create_epub_export(self, **kwargs):
"""
Expand Down
2 changes: 1 addition & 1 deletion canvasapi/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def _post_request(self, url, headers, data=None, json=False):
files = None
for field, value in data:
if field == "file":
if isinstance(value, dict):
if isinstance(value, dict) or value is None:
files = value
else:
files = {"file": value}
Expand Down