Skip to content

Commit 80376ab

Browse files
Closes #13309: Introduce the account app (#13310)
* Introduce 'accounts' app for user-specific views & resources * Move UserTokenTable to account app * Move login & logout views to account app
1 parent 9c6c3d3 commit 80376ab

21 files changed

+435
-417
lines changed

netbox/account/__init__.py

Whitespace-only changes.

netbox/users/migrations/0005_usertoken.py renamed to netbox/account/migrations/0001_initial.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# Generated by Django 4.1.10 on 2023-07-25 15:19
1+
# Generated by Django 4.1.10 on 2023-07-30 17:49
22

33
from django.db import migrations
44

55

66
class Migration(migrations.Migration):
77

8+
initial = True
9+
810
dependencies = [
911
('users', '0004_netboxgroup_netboxuser'),
1012
]
@@ -15,10 +17,10 @@ class Migration(migrations.Migration):
1517
fields=[
1618
],
1719
options={
20+
'verbose_name': 'token',
1821
'proxy': True,
1922
'indexes': [],
2023
'constraints': [],
21-
'verbose_name': 'token',
2224
},
2325
bases=('users.token',),
2426
),

netbox/account/migrations/__init__.py

Whitespace-only changes.

netbox/account/models.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from django.urls import reverse
2+
3+
from users.models import Token
4+
5+
6+
class UserToken(Token):
7+
"""
8+
Proxy model for users to manage their own API tokens.
9+
"""
10+
class Meta:
11+
proxy = True
12+
verbose_name = 'token'
13+
14+
def get_absolute_url(self):
15+
return reverse('account:usertoken', args=[self.pk])

netbox/account/tables.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from django.utils.translation import gettext as _
2+
3+
from account.models import UserToken
4+
from netbox.tables import NetBoxTable, columns
5+
6+
__all__ = (
7+
'UserTokenTable',
8+
)
9+
10+
11+
TOKEN = """<samp><span id="token_{{ record.pk }}">{{ record }}</span></samp>"""
12+
13+
ALLOWED_IPS = """{{ value|join:", " }}"""
14+
15+
COPY_BUTTON = """
16+
{% if settings.ALLOW_TOKEN_RETRIEVAL %}
17+
{% copy_content record.pk prefix="token_" color="success" %}
18+
{% endif %}
19+
"""
20+
21+
22+
class UserTokenTable(NetBoxTable):
23+
"""
24+
Table for users to manager their own API tokens under account views.
25+
"""
26+
key = columns.TemplateColumn(
27+
verbose_name=_('Key'),
28+
template_code=TOKEN,
29+
)
30+
write_enabled = columns.BooleanColumn(
31+
verbose_name=_('Write Enabled')
32+
)
33+
created = columns.DateColumn(
34+
verbose_name=_('Created'),
35+
)
36+
expires = columns.DateColumn(
37+
verbose_name=_('Expires'),
38+
)
39+
last_used = columns.DateTimeColumn(
40+
verbose_name=_('Last Used'),
41+
)
42+
allowed_ips = columns.TemplateColumn(
43+
verbose_name=_('Allowed IPs'),
44+
template_code=ALLOWED_IPS
45+
)
46+
actions = columns.ActionsColumn(
47+
actions=('edit', 'delete'),
48+
extra_buttons=COPY_BUTTON
49+
)
50+
51+
class Meta(NetBoxTable.Meta):
52+
model = UserToken
53+
fields = (
54+
'pk', 'id', 'key', 'description', 'write_enabled', 'created', 'expires', 'last_used', 'allowed_ips',
55+
)

netbox/users/account_urls.py renamed to netbox/account/urls.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from django.urls import path
1+
from django.urls import include, path
22

3+
from utilities.urls import get_model_urls
34
from . import views
45

56
app_name = 'account'
@@ -12,8 +13,6 @@
1213
path('password/', views.ChangePasswordView.as_view(), name='change_password'),
1314
path('api-tokens/', views.UserTokenListView.as_view(), name='usertoken_list'),
1415
path('api-tokens/add/', views.UserTokenEditView.as_view(), name='usertoken_add'),
15-
path('api-tokens/<int:pk>/', views.UserTokenView.as_view(), name='usertoken'),
16-
path('api-tokens/<int:pk>/edit/', views.UserTokenEditView.as_view(), name='usertoken_edit'),
17-
path('api-tokens/<int:pk>/delete/', views.UserTokenDeleteView.as_view(), name='usertoken_delete'),
16+
path('api-tokens/<int:pk>/', include(get_model_urls('account', 'usertoken'))),
1817

1918
]

0 commit comments

Comments
 (0)