Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can_add() template filter raises exception when passing a model #15025

Closed
jeremystretch opened this issue Feb 2, 2024 · 0 comments · Fixed by #15026
Closed

can_add() template filter raises exception when passing a model #15025

jeremystretch opened this issue Feb 2, 2024 · 0 comments · Fixed by #15026
Assignees
Labels
severity: low Does not significantly disrupt application functionality, or a workaround is available status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@jeremystretch
Copy link
Member

Deployment Type

NetBox Cloud

NetBox Version

v3.7.1

Python Version

3.11

Steps to Reproduce

There is an issue with how the can_add() filter evaluates permissions. It should expect to receive a model rather than a specific instance (as an existing instance obviously cannot be create again). The following template code fails:

{% load perms %}
{% if request.user|can_add:model %}

Below is a reconstruction using the Python shell:

>>> from utilities.templatetags.perms import can_add
>>> user=User.objects.get(username='bob')
>>> user.has_perm('dcim.add_poweroutlet')
True
>>> can_add(user, PowerOutlet)
Traceback (most recent call last):
  File "/home/jstretch/projects/netbox/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 2053, in get_prep_value
    return int(value)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'property'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  ...
  File "/home/jstretch/projects/netbox/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 2055, in get_prep_value
    raise e.__class__(
TypeError: Field 'id' expected a number but got <property object at 0x7fa360779760>.

Expected Behavior

Passing a model to the can_add() filter should successfully evaluate the user's permission to create a new instance of that model.

Observed Behavior

A TypeError exception is raised.

@jeremystretch jeremystretch added type: bug A confirmed report of unexpected behavior in the application status: accepted This issue has been accepted for implementation severity: low Does not significantly disrupt application functionality, or a workaround is available labels Feb 2, 2024
@jeremystretch jeremystretch self-assigned this Feb 2, 2024
jeremystretch added a commit that referenced this issue Feb 2, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
severity: low Does not significantly disrupt application functionality, or a workaround is available status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
1 participant