Create CommentManager to query for comments that are accessible by a given user
Review Request #12309 — Created May 30, 2022 and submitted
Currently there is no efficient way to query for comments that are
accessible by a given user when using<AnyCommentClass>.objects
. This change
creates aCommentManager
and adds a method such that
<AnyCommentClass>.objects.accessible(user)
returns a queryset of comments that
are accessible by the given user.
- Wrote unit tests for
CommentManager
and they pass.
Summary | ID | Author |
---|---|---|
7360451725a0c15e73d6b3af2c019d1cd9b22484 | Michelle |
Description | From | Last Updated |
---|---|---|
Most of my comments on the other manager change apply here as well. |
chipx86 | |
This looks great! The only thing I want to see before this ships is some more extensive pieces of test … |
chipx86 | |
F401 'reviewboard.reviews.models.GeneralComment' imported but unused |
reviewbot | |
F401 'reviewboard.reviews.models.ScreenshotComment' imported but unused |
reviewbot | |
F401 'reviewboard.reviews.models.FileAttachmentComment' imported but unused |
reviewbot | |
W292 no newline at end of file |
reviewbot | |
This should probably be isinstance(user_or_username, str). It wouldn't make sense to query for username=False. We could add assert isinstance(user_or_username, (User, … |
david | |
Same issue here. |
david | |
Rather than abbreviating, let's keep the naming consistent with what we use in the other managers. I know in this … |
chipx86 | |
We can probably get away with just testing one type, since the manager won't be affected by the model type. … |
chipx86 | |
Missing a trailing period. |
chipx86 | |
Okay I made some comments about these lines as general guidance, but I think what's ideal here is to not … |
chipx86 | |
This can be if user.is_anonymous. |
chipx86 | |
Rather than .exclude, we should be able to use .filter and check against True. It's easier to rationalize the logic … |
chipx86 | |
Same comment here. Nicer to work as a filter. |
chipx86 | |
This comment is no longer applicable. |
chipx86 | |
Do we need transform? We should be able to compare the objects directly. |
chipx86 |
- Change Summary:
-
Ready for review.
- Summary:
-
[WIP] Create CommentManager to query for comments that are accessible by a given userCreate CommentManager to query for comments that are accessible by a given user
- Description:
-
Currently there is no efficient way to query for comments that are
accessible by a given user when using <AnyCommentClass>.objects
. This change~ creates a CommentManager and adds a method such that ~ creates a CommentManager
and adds a method such that<AnyCommentClass>.objects.accessible(user)
returns a queryset of comments thatare accessible by the given user. - Testing Done:
-
~ In process of writing tests.
~ - Wrote unit tests for
CommentManager
and they pass.
- Wrote unit tests for
- Commits:
-
Summary ID Author 39d45c91a107d1c52d3caaf0e793bbb752e182c4 Michelle 7066c6c7465800ab790169ed2e7d76d768f67c74 Michelle
Checks run (2 succeeded)
- Commits:
-
Summary ID Author 7066c6c7465800ab790169ed2e7d76d768f67c74 Michelle 94cf614d89d61761fde69d776bb3424cae70b4da Michelle
Checks run (2 succeeded)
- Change Summary:
-
rebased onto release-5.0.x
- Commits:
-
Summary ID Author 94cf614d89d61761fde69d776bb3424cae70b4da Michelle 1175eb716551fa5b739359585b0dbe36241838b8 Michelle
Checks run (2 succeeded)
- Change Summary:
-
cleaned up query code, added asserts for
user_or_username
's type - Commits:
-
Summary ID Author 1175eb716551fa5b739359585b0dbe36241838b8 Michelle d17ecd432c23aca206029f45f8eeec4c6c7a730b Michelle
Checks run (2 succeeded)
-
-
-
Rather than abbreviating, let's keep the naming consistent with what we use in the other managers. I know in this case it's likely to be for line length limits, but you can have the value in
Q(field=
) wrap to the next line as needed. -
We can probably get away with just testing one type, since the manager won't be affected by the model type.
If we wanted to ensure we're testing with each comment type, what we'd want to do is create a mixin class that takes the comment model we'd test with, have all unit tests operate on that model, and then have subclasses for each comment type. But it's probably not worth it, given the logic isn't sensitive to the type of model.
The docstrings can also just be
CommentManager
instead of worrying about the.objects
convention.
- Commits:
-
Summary ID Author d17ecd432c23aca206029f45f8eeec4c6c7a730b Michelle 8ac623e6a1b5e24415f38d2885732a0c618b166b Michelle
Checks run (2 succeeded)
-
-
-
Okay I made some comments about these lines as general guidance, but I think what's ideal here is to not have two places that are building queryset logic.
_query()
is already considering quite a lot of options for building up the queryset. It's easy to rationalize what's happening when reading that logic, but harder when we then have to mentally combine those built queries with this block. We should aim to have one and only one block of logic building any query involving access controls.I'd suggest we figure out what
public
should be up-front and pass that in above._query()
should also be responsible already for the user access (anonymous vs. authed vs. superuser). It looks like it's trying to at least, and that'd be the end goal so that any other wrappers around_query()
get the right behavior without duplicating this block. -
-
Rather than
.exclude
, we should be able to use.filter
and check againstTrue
. It's easier to rationalize the logic if we're not having to inverse. -
-
-
- Change Summary:
-
- Moved access logic for removing comments from drafts that do not belong to the user into the
_query
method
- Moved access logic for removing comments from drafts that do not belong to the user into the
- Commits:
-
Summary ID Author 8ac623e6a1b5e24415f38d2885732a0c618b166b Michelle f61ed5ed07fa5930d922f28de4fa13b5104fad37 Michelle
Checks run (2 succeeded)
- Change Summary:
-
- Added
assertNumQueries
to tests
- Added
- Commits:
-
Summary ID Author f61ed5ed07fa5930d922f28de4fa13b5104fad37 Michelle 23d757993c1a98e26b6d1c8e49a65a78e7f6f67e Michelle
Checks run (2 succeeded)
- Change Summary:
-
Takes advantage of the cache to reduce the number of queries in one test
- Commits:
-
Summary ID Author 23d757993c1a98e26b6d1c8e49a65a78e7f6f67e Michelle 2d1a08709b4488d06d6ad0af80974d9e71bee47f Michelle
Checks run (2 succeeded)
- Change Summary:
-
forgot to reduce the number of queries in the assert from the last change's optimizations
- Commits:
-
Summary ID Author 2d1a08709b4488d06d6ad0af80974d9e71bee47f Michelle f78e10ab6311b3c0afc4800efbc091a0f8c3866f Michelle
Checks run (2 succeeded)
-
-
This looks great! The only thing I want to see before this ships is some more extensive pieces of test data. When dealing with queries or access checks specifically, we should register objects that should not be queried, in order to ensure we don't see them in results.
For access-related tests, that means any combination of access controls that should fail should be included. For example, if testing Local Site functionality, we should have objects on the global site and a different Local Site registered, and they shouldn't be in the results.
- Change Summary:
-
- Added more extensive test coverage
- Commits:
-
Summary ID Author f78e10ab6311b3c0afc4800efbc091a0f8c3866f Michelle 137ae4c93fcd5372e55536c3be04b8fa00b01fc2 Michelle