This commit is contained in:
Jeremy Stretch 2018-10-05 18:32:07 +00:00 committed by GitHub
commit 00b694b228
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 194 additions and 207 deletions

View File

@ -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:

View File

@ -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'

View File

@ -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:

View File

@ -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:

View File

@ -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}),
})

View File

@ -1,91 +1,64 @@
{% 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 %}
<div class="panel panel-default">
<div class="panel-heading"><strong>Location</strong></div>
<div class="panel-body">
<div class="form-group">
<label class="col-md-3 control-label">Provider</label>
<div class="col-md-9">
<p class="form-control-static">{{ obj.circuit.provider }}</p>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Circuit</label>
<div class="col-md-9">
<p class="form-control-static">{{ obj.circuit.cid }}</p>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Termination</label>
<div class="col-md-9">
<p class="form-control-static">{{ form.term_side.value }}</p>
</div>
</div>
{% render_field form.site %}
{% render_field form.rack %}
{% render_field form.device %}
{% render_field form.interface %}
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading"><strong>Termination Details</strong></div>
<div class="panel-body">
<div class="form-group">
<label class="col-md-3 control-label required" for="id_port_speed">{{ form.port_speed.label }}</label>
<div class="col-md-9">
<div class="input-group">
{{ form.port_speed }}
{% include 'circuits/inc/speed_widget.html' with target_field='port_speed' %}
</div>
<span class="help-block">{{ form.port_speed.help_text }}</span>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="id_upstream_speed">{{ form.upstream_speed.label }}</label>
<div class="col-md-9">
<div class="input-group">
{{ form.upstream_speed }}
{% include 'circuits/inc/speed_widget.html' with target_field='upstream_speed' %}
</div>
<span class="help-block">{{ form.upstream_speed.help_text }}</span>
</div>
</div>
{% render_field form.xconnect_id %}
{% render_field form.pp_info %}
</div>
{% 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">
<div class="form-group">
<label class="col-md-3 control-label">Provider</label>
<div class="col-md-9">
<p class="form-control-static">{{ obj.circuit.provider }}</p>
</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 class="form-group">
<label class="col-md-3 control-label">Circuit</label>
<div class="col-md-9">
<p class="form-control-static">{{ obj.circuit.cid }}</p>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Termination</label>
<div class="col-md-9">
<p class="form-control-static">{{ form.term_side.value }}</p>
</div>
</div>
{% render_field form.site %}
{% render_field form.rack %}
{% render_field form.device %}
{% render_field form.interface %}
</div>
</form>
</div>
<div class="panel panel-default">
<div class="panel-heading"><strong>Termination Details</strong></div>
<div class="panel-body">
<div class="form-group">
<label class="col-md-3 control-label required" for="id_port_speed">{{ form.port_speed.label }}</label>
<div class="col-md-9">
<div class="input-group">
{{ form.port_speed }}
{% include 'circuits/inc/speed_widget.html' with target_field='port_speed' %}
</div>
<span class="help-block">{{ form.port_speed.help_text }}</span>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="id_upstream_speed">{{ form.upstream_speed.label }}</label>
<div class="col-md-9">
<div class="input-group">
{{ form.upstream_speed }}
{% include 'circuits/inc/speed_widget.html' with target_field='upstream_speed' %}
</div>
<span class="help-block">{{ form.upstream_speed.help_text }}</span>
</div>
</div>
{% render_field form.xconnect_id %}
{% render_field form.pp_info %}
</div>
</div>
{% endblock %}
{% block javascript %}

View File

@ -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() {

View File

@ -1,94 +1,64 @@
{% 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 %}
<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>
</div>
</div>
{% render_field form.role %}
{% render_field form.name %}
{% render_field form.userkeys %}
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading"><strong>Secret Data</strong></div>
<div class="panel-body">
{% if secret.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>
</div>
<div class="col-md-2 text-right">
<button class="btn btn-xs btn-success unlock-secret" secret-id="{{ secret.pk }}">
<i class="fa fa-lock"></i> Unlock
</button>
<button class="btn btn-xs btn-danger lock-secret collapse" secret-id="{{ secret.pk }}">
<i class="fa fa-unlock-alt"></i> Lock
</button>
</div>
</div>
{% endif %}
{% render_field form.plaintext %}
{% render_field form.plaintext2 %}
</div>
</div>
{% if form.custom_fields %}
<div class="panel panel-default">
<div class="panel-heading"><strong>Custom Fields</strong></div>
<div class="panel-body">
{% render_custom_fields form %}
</div>
</div>
{% endif %}
<div class="panel panel-default">
<div class="panel-heading"><strong>Tags</strong></div>
<div class="panel-body">
{% 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>
{% block title %}{% if obj.pk %}Editing {{ secret }}{% else %}Add a Secret{% endif %}{% endblock %}
{% include 'secrets/inc/private_key_modal.html' %}
{% 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">{{ obj.device }}</p>
</div>
</div>
{% render_field form.role %}
{% render_field form.name %}
{% render_field form.userkeys %}
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading"><strong>Secret Data</strong></div>
<div class="panel-body">
{% 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_{{ obj.pk }}">********</p>
</div>
<div class="col-md-2 text-right">
<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="{{ obj.pk }}">
<i class="fa fa-unlock-alt"></i> Lock
</button>
</div>
</div>
{% endif %}
{% render_field form.plaintext %}
{% render_field form.plaintext2 %}
</div>
</div>
{% if form.custom_fields %}
<div class="panel panel-default">
<div class="panel-heading"><strong>Custom Fields</strong></div>
<div class="panel-body">
{% render_custom_fields form %}
</div>
</div>
{% endif %}
<div class="panel panel-default">
<div class="panel-heading"><strong>Tags</strong></div>
<div class="panel-body">
{% render_field form.tags %}
</div>
</div>
{% endblock %}
{% block javascript %}
{% include 'secrets/inc/private_key_modal.html' %}
<script src="{% static 'js/secrets.js' %}?v{{ settings.VERSION }}"></script>
{% endblock %}

View File

@ -1,5 +1,4 @@
{% extends 'utilities/obj_edit.html' %}
{% load static from staticfiles %}
{% load form_helpers %}
{% block form %}

View File

@ -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,18 +32,30 @@
</div>
{% endblock %}
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3 text-right">
{% 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>
{% endif %}
<a href="{{ return_url }}" class="btn btn-default">Cancel</a>
{% endblock %}
<div class="col-md-3">
<div style="position: fixed">
{% block buttons %}
<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 %}
</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>

View File

@ -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() {

View File

@ -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()):