Skip to content

Commit 51ef4fb

Browse files
committed
Closes #13962: Add a copy-to-clipboard button to the key field of the API token creation form
1 parent 7983c25 commit 51ef4fb

File tree

6 files changed

+14
-8
lines changed

6 files changed

+14
-8
lines changed

netbox/project-static/dist/netbox.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

netbox/project-static/dist/netbox.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

netbox/project-static/src/clipboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Clipboard from 'clipboard';
22
import { getElements } from './util';
33

44
export function initClipboard(): void {
5-
for (const element of getElements('a.copy-content')) {
5+
for (const element of getElements('.copy-content')) {
66
new Clipboard(element);
77
}
88
}

netbox/templates/account/token.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
{% block content %}
1616
<div class="row">
1717
<div class="col col-md-12">
18-
{% if key and not settings.ALLOW_TOKEN_RETRIEVAL %}
19-
<div class="alert alert-danger" role="alert">
20-
<i class="mdi mdi-alert"></i> Tokens cannot be retrieved at a later time. You must <a href="#" class="copy-content" data-clipboard-target="#token_id" title="Copy to clipboard">copy the token value</a> below and store it securely.
21-
</div>
22-
{% endif %}
2318
<div class="card">
2419
<h5 class="card-header">{% trans "Token" %}</h5>
2520
<div class="card-body">

netbox/users/forms/model_forms.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ class UserTokenForm(BootstrapMixin, forms.ModelForm):
114114
help_text=_(
115115
'Keys must be at least 40 characters in length. <strong>Be sure to record your key</strong> prior to '
116116
'submitting this form, as it may no longer be accessible once the token has been created.'
117+
),
118+
widget=forms.TextInput(
119+
attrs={'data-clipboard': 'true'}
117120
)
118121
)
119122
allowed_ips = SimpleArrayField(

netbox/utilities/templates/form_helpers/render_field.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
{{ label }}
3030
</label>
3131
</div>
32+
{# Include a copy-to-clipboard button #}
33+
{% elif 'data-clipboard' in field.field.widget.attrs %}
34+
<div class="input-group">
35+
{{ field }}
36+
<button type="button" title="{% trans "Copy to clipboard" %}" class="btn btn-outline-dark border-input copy-content" data-clipboard-target="#{{ field.id_for_label }}">
37+
<i class="mdi mdi-content-copy"></i>
38+
</button>
39+
</div>
3240
{# Default field rendering #}
3341
{% else %}
3442
{{ field }}

0 commit comments

Comments
 (0)