#37021: Add a helper function to return the permission string representation to
be
used with `user.has_perm()`
------------------------------+--------------------------------------
Reporter: Mariatta | Owner: (none)
Type: New feature | Status: new
Component: contrib.auth | Version: 6.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Description changed by Mariatta:
Old description:
> When checking user's permission using `user.has_perm()`, I need to pass a
> string in the format of
> `{Permission.content_type.app_label}.{Permission.codename}`
>
> However, the `Permission` object itself does not provide an easy way to
> return this string.
> The `__str__` method of the `Permission` class returns a string of
> `content_type | name`.
>
> I think it would be great if the `Permission` class can provide the
> string representation that will allow us to check the permission without
> having to manually construct it ourselves.
>
> My desired outcome is to be able to do the following.
>
> ```
> user_permissions = Permission.objects.filter(...)
> for perm in user_permissions:
> if user.has_perm(perm.perm_string()):
> # let them do stuff
>
> ```
>
> Currently I am doing it as follows:
>
> ```
> if user.has_perm(f"{perm.content_type.app_label}.{perm.codename}"):
> # let them do stuff
> ```
>
> Proposal:
>
> To add a function within `Permission` class:
>
> ```
> def perm_string(self): # feel free to suggest other name
> return f"{self.content_type.app_label}.{self.codename}"
>
> ```
>
> If you agree, I would like to try to create the PR for this.
New description:
When checking user's permission using `user.has_perm()`, I need to pass a
string in the format of
`{Permission.content_type.app_label}.{Permission.codename}`
However, the `Permission` object itself does not provide an easy way to
return this string.
The `__str__` method of the `Permission` class returns a string of
`content_type | name`.
I think it would be great if the `Permission` class can provide the string
representation that will allow us to check the permission without having
to manually construct it ourselves.
My desired outcome is to be able to do the following.
{{{
user_permissions = Permission.objects.filter(...)
for perm in user_permissions:
if user.has_perm(perm.perm_string()):
# let them do stuff
}}}
Currently I am doing it as follows:
{{{
if user.has_perm(f"{perm.content_type.app_label}.{perm.codename}"):
# let them do stuff
}}}
Proposal:
To add a function within `Permission` class:
{{{
def perm_string(self): # feel free to suggest other name
return f"{self.content_type.app_label}.{self.codename}"
}}}
If you agree, I would like to try to create the PR for this.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/37021#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/0107019d4f5897cd-13a98e6a-d201-47e8-bee6-1565251a2c3f-000000%40eu-central-1.amazonses.com.