Skip to content

Conversation

@joshuadavidthomas
Copy link
Member

The cast needed to make static type checkers happy fails if django.contrib.auth is installed but there is no user attached to a request.

This can happen for a couple of reasons:

  • in tests (look at this repo's test suite for all the times I had to attach a fake user to the request)
  • in a single file Django application using with a library that requires django.contrib.auth, but in the demo you don't need or care about needing an actual user.

Moving the check to before the cast allows the function to early if there is no user attached. We also make the assumption that if the item has any permissions defined and there is no request user, that the item should be hidden.

The `cast` needed to make static type checkers happy fails if
`django.contrib.auth` is installed but there is no user attached to a
request.

This can happen for a couple of reasons:

- in tests (look at this repo's test suite for all the times I had to
attach a fake user to the request)
- in a single file Django application using with a library that requires
`django.contrib.auth`, but in the demo you don't need or care about
needing an actual user.

Moving the check to before the cast allows the function to early if
there is no user attached. We also make the assumption that if the item
has *any* permissions defined and there is no request user, that the
item should be hidden.
@joshuadavidthomas joshuadavidthomas requested a review from a team as a code owner April 25, 2024 20:04
Copy link
Contributor

@jefftriplett jefftriplett left a comment

Choose a reason for hiding this comment

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

One "future Josh will wonder why this exists" comment which is totally optional.

@joshuadavidthomas joshuadavidthomas merged commit 6ad22a3 into main Apr 25, 2024
@joshuadavidthomas joshuadavidthomas deleted the move-request-user-check branch April 25, 2024 21:55
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