Skip to content

Fix inefficient checking of teacher status - #1550

Merged
ihalaij1 merged 1 commit into
apluslms:masterfrom
sayravai:fix_1549
Oct 9, 2026
Merged

ihalaij1 merged 1 commit into
apluslms:masterfrom
sayravai:fix_1549

Conversation

@sayravai

@sayravai sayravai commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

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?

  • Accessibility test using the WAVE extension.
  • Django unit tests.
  • Playwright tests.
  • Other test. (Add a description below)
  • Manual testing.

[ADD A DESCRIPTION ABOUT WHAT YOU TESTED MANUALLY]

Did you test the changes in

  • Chrome
  • Firefox
  • This pull request cannot be tested in the browser.

Think of what is affected by these changes and could become broken

Translation

Programming style

  • Did you follow our style guides?
  • Did you use Python type hinting in all functions that you added or edited? (type hints for function parameters and return values)

Have you updated the README or other relevant documentation?

  • documents inside the doc directory.
  • README.md.
  • Aplus Manual.
  • Other documentation (mention below which documentation).

Is it Done?

  • Reviewer has finished the code review
  • After the review, the developer has made changes accordingly
  • Customer/Teacher has accepted the implementation of the feature

Clean up your git commit history before submitting the pull request!

@sayravai sayravai removed this from A+ sprints Oct 5, 2026
@ihalaij1 ihalaij1 self-assigned this Oct 9, 2026
@ihalaij1
ihalaij1 requested review from ihalaij1 and a balanced review from Copilot October 9, 2026 10:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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.

Comment thread userprofile/permissions.py
Comment thread userprofile/permissions.py

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The profile lookup causes an additional database query that the cached test fixture conceals.

1 open finding
2 resolved since last review

🧠 Review effort: Balanced

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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We literally don't care, thanks.

@ihalaij1
ihalaij1 merged commit 74fd8b3 into apluslms:master Oct 9, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Teacher status is checked inefficiently

4 participants