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

fix: handle dupe course staff mgmt command #294

Merged

Conversation

varshamenon4
Copy link
Member

@varshamenon4 varshamenon4 commented Jul 24, 2024

JIRA: COSMO-234

Description: This PR updates the course staff role management command to use ignore_conflicts to handle duplicate/already existing users and course staff roles. This is more efficient, and more readable. I did do some research, test locally and find other usages to confirm that ignore_conflicts is supported in our environment.

Merge checklist:

  • All reviewers approved
  • CI build is green
  • Changelog record added
  • Documentation updated (not only docstrings)
  • Commits are squashed

Post merge:

  • Delete working branch (if not needed anymore)

@varshamenon4 varshamenon4 changed the title Varshamenon4/fix handle dupe course staff mgmt command fix: handle dupe course staff mgmt command Jul 24, 2024
Copy link

github-actions bot commented Jul 25, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  edx_exams/apps/core/management/commands
  bulk_add_course_staff.py
  edx_exams/apps/core/management/commands/test
  test_bulk_add_course_staff.py
Project Total  

This report was generated by python-coverage-comment-action

Copy link
Member Author

@varshamenon4 varshamenon4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question about ignore_conflicts

@varshamenon4 varshamenon4 marked this pull request as ready for review July 26, 2024 14:34
@varshamenon4 varshamenon4 force-pushed the varshamenon4/fix-handle-dupe-course-staff-mgmt-command branch from 9ec3f02 to e558965 Compare July 26, 2024 14:42
(User(
username=row.get('username'),
email=row.get('email'),
) for row in reader[i:i + batch_size]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this line generate indexing errors? For example, the i+batch_size > len(reader) condition?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, let me add testing/handling for that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I did add tests to test that this still works. Also, I believe that this works because of this line: for i in range(0, len(reader), batch_size). Since this is incremented by batch_size, the line for row in reader[i:i + batch_size]) won't actually get to the index error. I actually originally repurposed this from some batching examples in the platform, so there are other examples of this pattern being used. See below:

Copy link
Member

@Zacharis278 Zacharis278 Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay this was throwing me off too. reader[i:i + batch_size] is actually slicing a subset reader into a new list. If the k in list[i:k] is outside the bounds it won't matter it only pulls the subset of indexes that actually exist up to k. eg [1,2,3,4][2:20] evaluates to [3,4]

@@ -130,3 +130,32 @@ def test_dupe_user_csv(self):
call_command(self.command, f'--csv_path={csv.name}')
self._assert_user_and_role(username, email, self.course_role, self.course_id)
self._assert_user_and_role(username, email, self.course_role, course_id_2)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test case where you set the batch size to 2 and create like 3 course_staff users?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding two cases, one where the batch size is larger than the number of roles, and one where the batch size is smaller.

@varshamenon4 varshamenon4 force-pushed the varshamenon4/fix-handle-dupe-course-staff-mgmt-command branch from ef6be73 to 973b522 Compare July 30, 2024 00:38
Copy link
Member Author

@varshamenon4 varshamenon4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some test cases!

call_command(self.command, f'--csv_path={csv.name}', '--batch_size=1')

def test_add_course_staff_with_batch_size_larger_than_list(self):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@schenedx added this test case as you'd suggested! Thanks for the feedback.

@@ -130,3 +130,32 @@ def test_dupe_user_csv(self):
call_command(self.command, f'--csv_path={csv.name}')
self._assert_user_and_role(username, email, self.course_role, self.course_id)
self._assert_user_and_role(username, email, self.course_role, course_id_2)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding two cases, one where the batch size is larger than the number of roles, and one where the batch size is smaller.

Copy link
Member

@schenedx schenedx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

@Zacharis278 Zacharis278 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit but otherwise LGTM

@varshamenon4 varshamenon4 force-pushed the varshamenon4/fix-handle-dupe-course-staff-mgmt-command branch from 1e27f1a to e763e84 Compare July 30, 2024 15:46
@varshamenon4 varshamenon4 merged commit d6143be into main Jul 30, 2024
5 checks passed
@varshamenon4 varshamenon4 deleted the varshamenon4/fix-handle-dupe-course-staff-mgmt-command branch July 30, 2024 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants