Misc cleanup

This commit is contained in:
Jeremy Stretch 2024-11-14 13:20:27 -05:00
parent 284cf8f82c
commit 06705595b6
9 changed files with 34 additions and 30 deletions

View File

@ -51,6 +51,7 @@ class ProviderAccountForm(NetBoxModelForm):
provider = DynamicModelChoiceField(
label=_('Provider'),
queryset=Provider.objects.all(),
selector=True,
quick_add=True
)
comments = CommentField()
@ -66,6 +67,7 @@ class ProviderNetworkForm(NetBoxModelForm):
provider = DynamicModelChoiceField(
label=_('Provider'),
queryset=Provider.objects.all(),
selector=True,
quick_add=True
)
comments = CommentField()

View File

@ -239,6 +239,8 @@ class ObjectEditView(GetReturnURLMixin, BaseObjectView):
'form': form,
}
# If the form is being displayed within a "quick add" widget,
# use the appropriate template
if request.GET.get('_quickadd'):
return render(request, 'htmx/quick_add.html', context)
@ -262,6 +264,7 @@ class ObjectEditView(GetReturnURLMixin, BaseObjectView):
"""
logger = logging.getLogger('netbox.views.ObjectEditView')
obj = self.get_object(**kwargs)
model = self.queryset.model
# Take a snapshot for change logging (if editing an existing object)
if obj.pk and hasattr(obj, 'snapshot'):
@ -334,14 +337,15 @@ class ObjectEditView(GetReturnURLMixin, BaseObjectView):
logger.debug("Form validation failed")
context = {
'model': model,
'object': obj,
'form': form,
'return_url': self.get_return_url(request, obj),
**self.get_extra_context(request, obj),
}
# Form was submitted via a "quick add" widget
if '_quickadd' in request.POST:
context['model'] = self.queryset.model
return render(request, 'htmx/quick_add.html', context)
return render(request, self.template_name, context)

Binary file not shown.

View File

@ -22,13 +22,9 @@ function slugify(slug: string, chars: number): string {
export function initReslug(): void {
for (const slugButton of getElements<HTMLButtonElement>('button#reslug')) {
const form = slugButton.form;
if (form == null) {
continue;
}
if (form == null) continue;
const slugField = form.querySelector('#id_slug') as HTMLInputElement;
if (slugField == null) {
continue;
}
if (slugField == null) continue;
const sourceId = slugField.getAttribute('slug-source');
const sourceField = form.querySelector(`#id_${sourceId}`) as HTMLInputElement;

View File

@ -2,34 +2,24 @@ import { Modal } from 'bootstrap';
function handleQuickAddObject(): void {
const quick_add = document.getElementById('quick-add-object');
if (quick_add == null) {
return;
}
if (quick_add == null) return;
const object_id = quick_add.getAttribute('data-object-id');
if (object_id == null) {
return;
}
if (object_id == null) return;
const object_repr = quick_add.getAttribute('data-object-repr');
if (object_repr == null) {
return;
}
if (object_repr == null) return;
const target_id = quick_add.getAttribute('data-target-id');
if (target_id == null) {
return;
}
if (target_id == null) return;
const target = document.getElementById(target_id);
if (target == null) {
return;
}
if (target == null) return;
//@ts-ignore
//@ts-expect-error tomselect added on init
target.tomselect.addOption({
id: object_id,
display: object_repr,
});
//@ts-ignore
//@ts-expect-error tomselect added on init
target.tomselect.addItem(object_id);
const modal_element = document.getElementById('htmx-modal');
@ -39,7 +29,6 @@ function handleQuickAddObject(): void {
modal.hide();
}
}
}
export function initQuickAdd(): void {

View File

@ -3,15 +3,23 @@
{% load i18n %}
<div class="modal-header">
<h5 class="modal-title">{% trans "Quick Add" %} {{ model|meta:"verbose_name"|bettertitle }}</h5>
<h2 class="modal-title">
{% trans "Quick Add" %} {{ model|meta:"verbose_name"|bettertitle }}
</h2>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body row">
<form hx-post="{% url model|viewname:"add" %}?_quickadd=True&target={{ request.GET.target }}" hx-target="#htmx-modal-content" enctype="multipart/form-data">
<form
hx-post="{% url model|viewname:"add" %}?_quickadd=True&target={{ request.GET.target }}"
hx-target="#htmx-modal-content"
enctype="multipart/form-data"
>
{% csrf_token %}
{% include 'htmx/form.html' %}
<div class="text-end">
<button type="button" class="btn btn-outline-secondary btn-float" data-bs-dismiss="modal" aria-label="Cancel">{% trans "Cancel" %}</button>
<button type="button" class="btn btn-outline-secondary btn-float" data-bs-dismiss="modal" aria-label="Cancel">
{% trans "Cancel" %}
</button>
<button type="submit" name="_quickadd" class="btn btn-primary">
{% trans "Create" %}
</button>

View File

@ -3,10 +3,13 @@
{% load i18n %}
<div class="modal-header">
<h2 class="modal-title">{{ object|meta:"verbose_name"|bettertitle }} {% trans "Created" %}</h2>
<h2 class="modal-title">
{{ object|meta:"verbose_name"|bettertitle }} {% trans "Created" %}
</h2>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body row">
{# This content is intended to be scraped and populated in the targeted selection field. #}
<p id="quick-add-object"
data-object-repr="{{ object }}"
data-object-id="{{ object.pk }}"

View File

@ -66,7 +66,7 @@ class DynamicModelChoiceMixin:
choice (DEPRECATED: pass `context={'disabled': '$fieldname'}` instead)
context: A mapping of <option> template variables to their API data keys (optional; see below)
selector: Include an advanced object selection widget to assist the user in identifying the desired object
quick_add: Include a button to quickly create a new related object for assignment
quick_add: Include a widget to quickly create a new related object for assignment
Context keys:
value: The name of the attribute which contains the option's value (default: 'id')

View File

@ -2,6 +2,7 @@
<div class="d-flex">
{% include 'django/forms/widgets/select.html' %}
{% if widget.attrs.selector and not widget.attrs.disabled %}
{# Opens the object selector modal #}
<button
type="button"
title="{% trans "Open selector" %}"
@ -15,6 +16,7 @@
</button>
{% endif %}
{% if widget.attrs.quick_add and not widget.attrs.disabled %}
{# Opens the quick add modal #}
<button
type="button"
title="{% trans "Quick add" %}"