Modal for edit and delete part category

This commit is contained in:
Oliver 2018-04-27 20:42:12 +10:00
parent 5162c1d11f
commit bc3dca3aba
4 changed files with 61 additions and 58 deletions

View File

@ -1,41 +1,33 @@
{% extends 'delete_obj.html' %}
{% block del_title %}
Are you sure you want to delete category '{{ category.name }}'?
{% endblock %}
{% block del_body %}
{% if category.children.all|length > 0 %}
<p>This category contains {{ category.children.all|length }} child categories.<br>
If this category is deleted, these child categories will be moved to
{% if category.parent %}
the '{{ category.parent.name }}' category.
{% else %}
the top level 'Parts' category.
{% endif %}
</p>
{% if category.children.all|length > 0 %}
<p>This category contains {{ category.children.all|length }} child categories.<br>
If this category is deleted, these child categories will be moved to
<ul class='list-group'>
{% for cat in category.children.all %}
<li class='list-group-item'><b>{{ cat.name }}</b> - <i>{{ cat.description }}</i></li>
{% endfor %}
</ul>
{% endif %}
{% if category.parts.all|length > 0 %}
<p>This category contains {{ category.parts.all|length }} parts.<br>
{% if category.parent %}
the '{{ category.parent.name }}' category.
If this category is deleted, these parts will be moved to the parent category '{{ category.parent.pathstring }}'
{% else %}
the top level 'Parts' category.
If this category is deleted, these parts will be moved to the top-level category 'Parts'
{% endif %}
</p>
<ul class='list-group'>
{% for cat in category.children.all %}
<li class='list-group-item'><b>{{ cat.name }}</b> - <i>{{ cat.description }}</i></li>
{% endfor %}
</ul>
{% endif %}
{% if category.parts.all|length > 0 %}
<p>This category contains {{ category.parts.all|length }} parts.<br>
{% if category.parent %}
If this category is deleted, these parts will be moved to the parent category '{{ category.parent.pathstring }}'
{% else %}
If this category is deleted, these parts will be moved to the top-level category 'Parts'
{% endif %}
</p>
<ul class='list-group'>
{% for part in category.parts.all %}
<li class='list-group-item'><b>{{ part.name }}</b> - <i>{{ part.description }}</i></li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}
</p>
<ul class='list-group'>
{% for part in category.parts.all %}
<li class='list-group-item'><b>{{ part.name }}</b> - <i>{{ part.description }}</i></li>
{% endfor %}
</ul>
{% endif %}

View File

@ -25,19 +25,14 @@
<button type='button' class='btn btn-primary' id='create-cat'>
New Category
</button>
<button class="btn btn-success" id='create-part'>New Part</button>
<a href="{% url 'category-edit' category.id %}">
<button class="btn btn-info">Edit Category</button>
</a>
<a href="{% url 'category-delete' category.id %}">
<button class="btn btn-danger">Delete Category</button>
</a>
<button class="btn btn-info" id='edit-category'>Edit Category</button>
<button class="btn btn-success" id='create-part'>New Part</button>
<button class="btn btn-danger" id='delete-category'>Delete Category</button>
</div>
{% include 'modal.html' %}
{% include 'modal_delete.html' %}
{% endblock %}
@ -49,6 +44,19 @@
$(document).ready(function (){
$('#part-list').footable();
$("#edit-category").click(function () {
launchModalForm("#modal-form",
"{% url 'category-edit' category.id %}",
{reload: true},
);
});
$("#delete-category").click(function() {
launchDeleteForm("#modal-delete",
"{% url 'category-delete' category.id %}",
{redirect: "{% url 'part-index' %}"});
});
$("#create-cat").click(function() {
launchModalForm("#modal-form",
"{% url 'category-create' %}",

View File

@ -116,10 +116,12 @@ class CategoryDetail(DetailView):
template_name = 'part/category_detail.html'
class CategoryEdit(UpdateView):
class CategoryEdit(AjaxUpdateView):
model = PartCategory
template_name = 'part/category_edit.html'
form_class = EditCategoryForm
ajax_template_name = 'modal_form.html'
ajax_form_title = 'Edit Part Category'
def get_context_data(self, **kwargs):
context = super(CategoryEdit, self).get_context_data(**kwargs).copy()
@ -129,18 +131,12 @@ class CategoryEdit(UpdateView):
return context
class CategoryDelete(DeleteView):
class CategoryDelete(AjaxDeleteView):
model = PartCategory
template_name = 'part/category_delete.html'
context_object_name = 'category'
success_url = '/part/'
def post(self, request, *args, **kwargs):
if 'confirm' in request.POST:
return super(CategoryDelete, self).post(request, *args, **kwargs)
else:
return HttpResponseRedirect(self.get_object().get_absolute_url())
class CategoryCreate(AjaxCreateView):
model = PartCategory

View File

@ -51,9 +51,15 @@ function launchDeleteForm(modal, url, options = {}) {
success: function (response) {
$(modal).modal('hide');
if (options.redirect) {
if (options.success) {
options.success();
}
else if (options.redirect) {
window.location.href = options.redirect;
}
else if (options.reload) {
location.reload();
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Error deleting item:\n' + thrownError);
@ -123,14 +129,15 @@ function launchModalForm(modal, url, options = {}) {
$(modal).modal('hide');
if (options.redirect) {
window.location.href = options.redirect;
}
if (options.success) {
options.success();
}
else if (options.redirect) {
window.location.href = options.redirect;
}
else if (options.reload) {
location.reload();
}
}
else if (response.html_form) {
var target = modal + ' .modal-form-content';