-
Notifications
You must be signed in to change notification settings - Fork 3
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
fix: course staff mgmt command #310
Conversation
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
236ed90
to
241fe94
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ready for review. Had one question about error handling included in comments!
) for row in reader[i:i + batch_size]), | ||
ignore_conflicts=True, | ||
) | ||
users_dict = {k: v for k, v in [(u.username, u) for (u, c) in [User.objects.get_or_create(username=row.get('username'), email=row.get('email')) for row in reader[i:i + batch_size]]]} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this look good? I can now use this to get users in the bulk create function as this does include the pk. Need to handle IntegrityError (specifically ran into the error when trying to add a user with an existing username, but a different email). Guessing this can be done with a try/catch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My question specifically related to error handling: what do we want to happen in this case? Ideally we'd want to skip the failing entry, right? So maybe the move is to break out the list comprehension (for readability sake) and to handle the error at the get_or_create by having a try/catch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took a pass at the handling of the error. But there still seems to be some corner cases that aren't working as I'd expect (see comment for more details). Not sure if this is relevant or needed for this work though.
for row in reader[i:i + batch_size]: | ||
try: | ||
users_list.append(User.objects.get_or_create(username=row.get('username'), email=row.get('email'))) | ||
except IntegrityError: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took a pass at handling IntegrityError here. This handles the case where there's a duplicate username with a different email. This does not handle when there is a new username with a duplicate email address. I don't know if this is an issue, but this is also something that may need to be handled in the Model/db itself so might be more cumbersome than needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this is a case we're expecting can we log something or output information about the rows that failed to create when this is run so the failure isn't silent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I can do that.
ee2fb5c
to
dafdc1a
Compare
JIRA: COSMO-234
Description: Attempting to fix the course staff role mgmt command by separating the loops and removing the atomic transaction. I am unable to replicate locally, but running this command in stage resulted in an error where the User couldn't be found.