Repository navigation
Fix inefficient checking of teacher status - #1550
Merged
Merged
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The GraderUser guard is unreachable, and the new permission paths lack tests.
2 open findings
What changed in this PR
Optimizes teacher-status checks by replacing per-course queries with a single enrollment query.
Changes:
- Uses
CourseInstanceManager.get_teaching().exists(). - Adds guards for unsupported user types.
| File | Description |
|---|---|
userprofile/permissions.py |
Optimizes teacher permission resolution and adds user-type handling. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| return False | ||
| # Check with a single query if the user is a teacher on any course | ||
| # instance, instead of looping over every course in the database. | ||
| return CourseInstance.objects.get_teaching(user.userprofile).exists() |
Contributor
There was a problem hiding this comment.
We literally don't care, thanks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #1549
Description
Resolve the teacher status of a user in a single DB call.
Why?
The old implementation was marked as "FIXME" due to the inefficient query.
How?
Instead of looping through every course instance, we use the existing get_teaching method of the CourseInstanceManager. This implementation's cost is proportional to the user's enrolment count, not the total amount of courses. We also add a guard for anonymous users and the GraderUser, so checking teacher status of those does not hit the DB at all.
Testing
Remember to add or update unit tests for new features and changes.
What type of test did you run?
[ADD A DESCRIPTION ABOUT WHAT YOU TESTED MANUALLY]
Did you test the changes in
Think of what is affected by these changes and could become broken
Translation
Programming style
Have you updated the README or other relevant documentation?
Is it Done?
Clean up your git commit history before submitting the pull request!