mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-09 09:08:15 -06:00
Merge d6443f7b37
into 841db3b0c2
This commit is contained in:
commit
00b694b228
@ -5,7 +5,7 @@ from django.contrib.auth.decorators import permission_required
|
||||
from django.contrib.auth.mixins import PermissionRequiredMixin
|
||||
from django.db import transaction
|
||||
from django.db.models import Count
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.shortcuts import get_object_or_404, redirect, render, reverse
|
||||
from django.views.generic import View
|
||||
|
||||
from extras.models import Graph, GRAPH_TYPE_PROVIDER
|
||||
@ -261,6 +261,7 @@ class CircuitTerminationCreateView(PermissionRequiredMixin, ObjectEditView):
|
||||
model = CircuitTermination
|
||||
model_form = forms.CircuitTerminationForm
|
||||
template_name = 'circuits/circuittermination_edit.html'
|
||||
enable_add_another = False
|
||||
|
||||
def alter_obj(self, obj, request, url_args, url_kwargs):
|
||||
if 'circuit' in url_kwargs:
|
||||
|
@ -452,6 +452,9 @@ class RackReservationCreateView(PermissionRequiredMixin, ObjectEditView):
|
||||
def get_return_url(self, request, obj):
|
||||
return obj.rack.get_absolute_url()
|
||||
|
||||
def get_add_url(self, obj):
|
||||
return reverse('dcim:rack_add_reservation', kwargs={'rack': obj.rack.pk})
|
||||
|
||||
|
||||
class RackReservationEditView(RackReservationCreateView):
|
||||
permission_required = 'dcim.change_rackreservation'
|
||||
|
@ -197,6 +197,7 @@ class ImageAttachmentEditView(PermissionRequiredMixin, ObjectEditView):
|
||||
permission_required = 'extras.change_imageattachment'
|
||||
model = ImageAttachment
|
||||
model_form = ImageAttachmentForm
|
||||
enable_add_another = False
|
||||
|
||||
def alter_obj(self, imageattachment, request, args, kwargs):
|
||||
if not imageattachment.pk:
|
||||
|
@ -983,6 +983,7 @@ class ServiceCreateView(PermissionRequiredMixin, ObjectEditView):
|
||||
model = Service
|
||||
model_form = forms.ServiceForm
|
||||
template_name = 'ipam/service_edit.html'
|
||||
enable_add_another = False
|
||||
|
||||
def alter_obj(self, obj, request, url_args, url_kwargs):
|
||||
if 'device' in url_kwargs:
|
||||
|
@ -132,7 +132,7 @@ def secret_add(request, pk):
|
||||
form = forms.SecretForm(instance=secret)
|
||||
|
||||
return render(request, 'secrets/secret_edit.html', {
|
||||
'secret': secret,
|
||||
'obj': secret,
|
||||
'form': form,
|
||||
'return_url': device.get_absolute_url(),
|
||||
})
|
||||
@ -185,7 +185,7 @@ def secret_edit(request, pk):
|
||||
form = forms.SecretForm(instance=secret)
|
||||
|
||||
return render(request, 'secrets/secret_edit.html', {
|
||||
'secret': secret,
|
||||
'obj': secret,
|
||||
'form': form,
|
||||
'return_url': reverse('secrets:secret', kwargs={'pk': secret.pk}),
|
||||
})
|
||||
|
@ -1,24 +1,10 @@
|
||||
{% extends '_base.html' %}
|
||||
{% extends 'utilities/obj_edit.html' %}
|
||||
{% load staticfiles %}
|
||||
{% load form_helpers %}
|
||||
|
||||
{% block content %}
|
||||
<form action="." method="post" class="form form-horizontal">
|
||||
{% csrf_token %}
|
||||
{% for field in form.hidden_fields %}
|
||||
{{ field }}
|
||||
{% endfor %}
|
||||
<div class="row">
|
||||
<div class="col-md-6 col-md-offset-3">
|
||||
<h3>{% block title %}Circuit {{ obj.circuit }} - {{ form.term_side.value }} Side{% endblock %}</h3>
|
||||
{% if form.non_field_errors %}
|
||||
<div class="panel panel-danger">
|
||||
<div class="panel-heading"><strong>Errors</strong></div>
|
||||
<div class="panel-body">
|
||||
{{ form.non_field_errors }}
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% block title %}Circuit {{ obj.circuit }} - {{ form.term_side.value }} Side{% endblock %}
|
||||
|
||||
{% block form %}
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading"><strong>Location</strong></div>
|
||||
<div class="panel-body">
|
||||
@ -73,19 +59,6 @@
|
||||
{% render_field form.pp_info %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6 col-md-offset-3 text-right">
|
||||
{% if obj.pk %}
|
||||
<button type="submit" name="_update" class="btn btn-primary">Update</button>
|
||||
{% else %}
|
||||
<button type="submit" name="_create" class="btn btn-primary">Create</button>
|
||||
{% endif %}
|
||||
<a href="{{ return_url }}" class="btn btn-default">Cancel</a>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
||||
{% block javascript %}
|
||||
|
@ -30,17 +30,6 @@
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block buttons %}
|
||||
{% if obj.pk %}
|
||||
<button type="submit" name="_update" class="btn btn-primary">Update</button>
|
||||
<button type="submit" formaction="?return_url={% url 'dcim:interface_edit' pk=obj.pk %}" class="btn btn-primary">Update and Continue Editing</button>
|
||||
{% else %}
|
||||
<button type="submit" name="_create" class="btn btn-primary">Create</button>
|
||||
<button type="submit" name="_addanother" class="btn btn-primary">Create and Add Another</button>
|
||||
{% endif %}
|
||||
<a href="{{ return_url }}" class="btn btn-default">Cancel</a>
|
||||
{% endblock %}
|
||||
|
||||
{% block javascript %}
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function() {
|
||||
|
@ -1,29 +1,17 @@
|
||||
{% extends '_base.html' %}
|
||||
{% extends 'utilities/obj_edit.html' %}
|
||||
{% load static from staticfiles %}
|
||||
{% load form_helpers %}
|
||||
|
||||
{% block content %}
|
||||
<form action="." method="post" class="form form-horizontal">
|
||||
{% csrf_token %}
|
||||
{{ form.private_key }}
|
||||
<div class="row">
|
||||
<div class="col-md-6 col-md-offset-3">
|
||||
<h3>{% block title %}{% if secret.pk %}Editing {{ secret }}{% else %}Add a Secret{% endif %}{% endblock %}</h3>
|
||||
{% if form.non_field_errors %}
|
||||
<div class="panel panel-danger">
|
||||
<div class="panel-heading"><strong>Errors</strong></div>
|
||||
<div class="panel-body">
|
||||
{{ form.non_field_errors }}
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% block title %}{% if obj.pk %}Editing {{ secret }}{% else %}Add a Secret{% endif %}{% endblock %}
|
||||
|
||||
{% block form %}
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading"><strong>Secret Attributes</strong></div>
|
||||
<div class="panel-body">
|
||||
<div class="form-group">
|
||||
<label class="col-md-3 control-label required">Device</label>
|
||||
<div class="col-md-9">
|
||||
<p class="form-control-static">{{ secret.device }}</p>
|
||||
<p class="form-control-static">{{ obj.device }}</p>
|
||||
</div>
|
||||
</div>
|
||||
{% render_field form.role %}
|
||||
@ -34,17 +22,17 @@
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading"><strong>Secret Data</strong></div>
|
||||
<div class="panel-body">
|
||||
{% if secret.pk %}
|
||||
{% if obj.pk %}
|
||||
<div class="form-group">
|
||||
<label class="col-md-3 control-label required">Current Plaintext</label>
|
||||
<div class="col-md-7">
|
||||
<p class="form-control-static" id="secret_{{ secret.pk }}">********</p>
|
||||
<p class="form-control-static" id="secret_{{ obj.pk }}">********</p>
|
||||
</div>
|
||||
<div class="col-md-2 text-right">
|
||||
<button class="btn btn-xs btn-success unlock-secret" secret-id="{{ secret.pk }}">
|
||||
<button class="btn btn-xs btn-success unlock-secret" secret-id="{{ obj.pk }}">
|
||||
<i class="fa fa-lock"></i> Unlock
|
||||
</button>
|
||||
<button class="btn btn-xs btn-danger lock-secret collapse" secret-id="{{ secret.pk }}">
|
||||
<button class="btn btn-xs btn-danger lock-secret collapse" secret-id="{{ obj.pk }}">
|
||||
<i class="fa fa-unlock-alt"></i> Lock
|
||||
</button>
|
||||
</div>
|
||||
@ -68,27 +56,9 @@
|
||||
{% render_field form.tags %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="form-group">
|
||||
<div class="col-md-12 text-center">
|
||||
{% if secret.pk %}
|
||||
<button type="submit" name="_update" class="btn btn-primary">Update</button>
|
||||
<a href="{% url 'secrets:secret' pk=secret.pk %}" class="btn btn-default">Cancel</a>
|
||||
{% else %}
|
||||
<button type="submit" name="_create" class="btn btn-primary">Create</button>
|
||||
<button type="submit" name="_addanother" class="btn btn-primary">Create and Add Another</button>
|
||||
<a href="{{ return_url }}" class="btn btn-default">Cancel</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
{% include 'secrets/inc/private_key_modal.html' %}
|
||||
{% endblock %}
|
||||
|
||||
{% block javascript %}
|
||||
{% include 'secrets/inc/private_key_modal.html' %}
|
||||
<script src="{% static 'js/secrets.js' %}?v{{ settings.VERSION }}"></script>
|
||||
{% endblock %}
|
||||
|
@ -1,5 +1,4 @@
|
||||
{% extends 'utilities/obj_edit.html' %}
|
||||
{% load static from staticfiles %}
|
||||
{% load form_helpers %}
|
||||
|
||||
{% block form %}
|
||||
|
@ -10,6 +10,10 @@
|
||||
<div class="row">
|
||||
<div class="col-md-6 col-md-offset-3">
|
||||
<h3>{% block title %}{% if obj.pk %}Editing {{ obj_type }} {{ obj }}{% else %}Add a new {{ obj_type }}{% endif %}{% endblock %}</h3>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6 col-md-offset-3">
|
||||
{% block tabs %}{% endblock %}
|
||||
{% if form.non_field_errors %}
|
||||
<div class="panel panel-danger">
|
||||
@ -28,19 +32,31 @@
|
||||
</div>
|
||||
{% endblock %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6 col-md-offset-3 text-right">
|
||||
<div class="col-md-3">
|
||||
<div style="position: fixed">
|
||||
{% block buttons %}
|
||||
{% if obj.pk %}
|
||||
<button type="submit" name="_update" class="btn btn-primary">Update</button>
|
||||
{% else %}
|
||||
<button type="submit" name="_create" class="btn btn-primary">Create</button>
|
||||
<button type="submit" name="_addanother" class="btn btn-primary">Create and Add Another</button>
|
||||
<div class="btn-group">
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<i class="fa fa-save"></i> Save
|
||||
</button>
|
||||
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
||||
<span class="caret"></span>
|
||||
</button>
|
||||
<ul class="dropdown-menu">
|
||||
<li>
|
||||
<button type="submit" name="_continue" class="btn btn-link">Save and Continue Editing</button>
|
||||
{% if enable_add_another %}
|
||||
<button type="submit" name="_addanother" class="btn btn-link">Save and Add Another</button>
|
||||
{% endif %}
|
||||
<a href="{{ return_url }}" class="btn btn-default">Cancel</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div style="margin-top: 12px">
|
||||
<a href="{{ return_url }}" class="btn btn-default"><i class="fa fa-ban"></i> Cancel</a>
|
||||
</div>
|
||||
{% endblock %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
@ -27,17 +27,6 @@
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block buttons %}
|
||||
{% if obj.pk %}
|
||||
<button type="submit" name="_update" class="btn btn-primary">Update</button>
|
||||
<button type="submit" formaction="?return_url={% url 'virtualization:interface_edit' pk=obj.pk %}" class="btn btn-primary">Update and Continue Editing</button>
|
||||
{% else %}
|
||||
<button type="submit" name="_create" class="btn btn-primary">Create</button>
|
||||
<button type="submit" name="_addanother" class="btn btn-primary">Create and Add Another</button>
|
||||
{% endif %}
|
||||
<a href="{{ return_url }}" class="btn btn-default">Cancel</a>
|
||||
{% endblock %}
|
||||
|
||||
{% block javascript %}
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function() {
|
||||
|
@ -15,7 +15,7 @@ from django.http import HttpResponseServerError
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.template import loader
|
||||
from django.template.exceptions import TemplateDoesNotExist, TemplateSyntaxError
|
||||
from django.urls import reverse
|
||||
from django.urls import NoReverseMatch, reverse
|
||||
from django.utils.html import escape
|
||||
from django.utils.http import is_safe_url
|
||||
from django.utils.safestring import mark_safe
|
||||
@ -171,10 +171,14 @@ class ObjectEditView(GetReturnURLMixin, View):
|
||||
model: The model of the object being edited
|
||||
model_form: The form used to create or edit the object
|
||||
template_name: The name of the template
|
||||
enable_continue_editing: Enable the option to save and continue editing the object
|
||||
enable_add_another: Enable the option to save the object and create another
|
||||
"""
|
||||
model = None
|
||||
model_form = None
|
||||
template_name = 'utilities/obj_edit.html'
|
||||
enable_continue_editing = True
|
||||
enable_add_another = True
|
||||
|
||||
def get_object(self, kwargs):
|
||||
# Look up object by slug or PK. Return None if neither was provided.
|
||||
@ -189,6 +193,36 @@ class ObjectEditView(GetReturnURLMixin, View):
|
||||
# given some parameter from the request URL.
|
||||
return obj
|
||||
|
||||
def get_edit_url(self, obj):
|
||||
"""
|
||||
Return the URL for editing an existing object (used for "save and continue editing" button).
|
||||
"""
|
||||
url_name = '{}:{}_edit'.format(
|
||||
self.model._meta.app_label,
|
||||
self.model._meta.model_name
|
||||
)
|
||||
if hasattr(obj, 'slug'):
|
||||
kwargs = {'slug': obj.slug}
|
||||
else:
|
||||
kwargs = {'pk': obj.pk}
|
||||
try:
|
||||
return reverse(url_name, kwargs=kwargs)
|
||||
except NoReverseMatch:
|
||||
return None
|
||||
|
||||
def get_add_url(self, obj):
|
||||
"""
|
||||
Return the URL for creating a new object (used for "save and add another" button).
|
||||
"""
|
||||
url_name = '{}:{}_add'.format(
|
||||
self.model._meta.app_label,
|
||||
self.model._meta.model_name
|
||||
)
|
||||
try:
|
||||
return reverse(url_name)
|
||||
except NoReverseMatch:
|
||||
return None
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
|
||||
obj = self.get_object(kwargs)
|
||||
@ -201,6 +235,8 @@ class ObjectEditView(GetReturnURLMixin, View):
|
||||
'obj': obj,
|
||||
'obj_type': self.model._meta.verbose_name,
|
||||
'form': form,
|
||||
'enable_continue_editing': self.enable_continue_editing,
|
||||
'enable_add_another': self.enable_add_another,
|
||||
'return_url': self.get_return_url(request, obj),
|
||||
})
|
||||
|
||||
@ -224,8 +260,17 @@ class ObjectEditView(GetReturnURLMixin, View):
|
||||
msg = '{} {}'.format(msg, escape(obj))
|
||||
messages.success(request, mark_safe(msg))
|
||||
|
||||
if '_addanother' in request.POST:
|
||||
return redirect(request.get_full_path())
|
||||
# Continue editing the current object
|
||||
if '_continue' in request.POST and self.enable_continue_editing:
|
||||
edit_url = self.get_edit_url(obj)
|
||||
if edit_url is not None:
|
||||
return redirect(edit_url)
|
||||
|
||||
# Create another object of the same type
|
||||
elif '_addanother' in request.POST and self.enable_add_another:
|
||||
add_url = self.get_add_url(obj)
|
||||
if add_url is not None:
|
||||
return redirect(add_url)
|
||||
|
||||
return_url = form.cleaned_data.get('return_url')
|
||||
if return_url is not None and is_safe_url(url=return_url, host=request.get_host()):
|
||||
|
Loading…
Reference in New Issue
Block a user