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

Endpoint for Submission Class that returns data from the "submission_comments" field ...? #581

Open
kspicer80 opened this issue Jan 9, 2023 · 1 comment

Comments

@kspicer80
Copy link

I have a script that uploads comments to a specific assignment based on the course_id, user_id, etc. I know there is an
endpoint] (v1/courses/{course_id}/assignments/{assignment_id}/submissions/{user_id}) that returns information about the submission comments field. Is there a way to check to see if comments have been added to a particular assignment before, say, I upload comments in my script?

I don't think canvasapi has any function in the Submission class that would allow me to not produce duplicate comments uploaded to a student assignment. There is an "edit" function and also a "get_submission_peer_reviews" but I'm not sure the latter returns the data I would need.

Obviously, the script now doesn't have this check, so, as it stands currently, it'll just upload duplicate comments if I were to run the script more than once (say, by error).

My apologies ahead of time if there is a way to get this data and I just missed it. (Also, if this is a duplicate here in the issues arena, feel free to let me know that too!)

Thanks to everyone there that works on this repository—it's been a hugely significant for me and my workflows, that's for sure!

@IonMich
Copy link

IonMich commented Jan 10, 2023

As you mentioned, when you upload a comment in a submission with canvas_submission.edit, a submission object is returned and it includes a list of submission_comments. This list appears to be sorted by the created_at key of each comment, so you can grab all the information about your newly uploaded comment by accessing submission_comments[-1]. One of the keys is the Canvas id of the comment, which uniquely identifies it.

Now your script to avoid uploading duplicates really depends on your implementation of a Comment.
Assuming that you have something like:

class Comment():
    def __init__(self, text: str):
        self.text = text
        self.canvas_id = None

then you can first check if comment.canvas_id is None before you try uploading a particular comment, and update the canvas_id attribute after each successful comment upload. This of course requires that you somehow store these objects in some kind of a database so that these attributes persist when you run your upload script again. Of course, the comment that you have uploaded with your script in the past might have been deleted manually on Canvas, so something like:

for comment in my_stored_comments:
    if comment.canvas_id is not None:
        is_still_on_canvas = # your method that checks if a comment with id=comment.canvas_id is still in the submission
        if is_still_on_canvas:
            continue
    newly_updated_submission = submission.edit(
        comment = {"text_comment":comment.text})
    
    new_comment_id_on_canvas = newly_updated_submission.submission_comments[-1]["id"]
    comment.canvas_id = new_comment_id_on_canvas

To deal with the possibility that a comment might be edited manually on Canvas, you could check if the edited_at property of the comment is None.

If for some reason you don't want to store the canvas_id in your database, I think you could simply add a boolean property is_uploaded_to_canvas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants