Added pages for editing categories

- category-detail
- category-delete
- category-edit
- category-new
This commit is contained in:
Oliver 2018-04-15 11:40:03 +10:00
parent 5b5b8f4d12
commit 17b9f4ec8c
11 changed files with 249 additions and 35 deletions

View File

@ -2,7 +2,7 @@ from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from .models import Part
from .models import Part, PartCategory
class EditPartForm(forms.ModelForm):
@ -28,4 +28,26 @@ class EditPartForm(forms.ModelForm):
'URL',
'minimum_stock',
'trackable',
]
class EditCategoryForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(EditCategoryForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'id-edit-part-form'
self.helper.form_class = 'blueForms'
self.helper.form_method = 'post'
#self.helper.form_action = 'submit'
self.helper.add_input(Submit('submit', 'Submit'))
class Meta:
model = PartCategory
fields = [
'parent',
'name',
'description'
]

View File

@ -0,0 +1,50 @@
{% extends 'base.html' %}
{% block content %}
<div class="panel panel-danger">
<div class="panel-heading">Are you sure you want to delete category '{{ category.name }}'?</div>
<div class='panel-body'>
<p><b>Deleting this category is a permanent action and cannot be undone.</b></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
{% if category.parent %}
the '{{ category.parent.name }}' category.
{% else %}
the top level 'Parts' category.
{% endif %}
</p>
<ul class='list-group'>
{% for cat in category.children.all %}
<li class='list-group-item'>{{ cat.name }} - {{ cat.description }}</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'>{{ part.name }} - {{ part.description }}</li>
{% endfor %}
</ul>
{% endif %}
<form action="" method="post">{% csrf_token %}
<input type="submit" name='confirm' value="Confirm" />
<input type="submit" name="cancel" value="Cancel" />
</form>
</div>
</div>
{% endblock %}

View File

@ -0,0 +1,31 @@
{% extends "base.html" %}
{% load static %}
{% block content %}
{% include "part/cat_link.html" with category=category %}
{% include "part/category_subcategories.html" with children=category.children.all %}
{% include "part/category_parts.html" with parts=category.parts.all %}
<div class='container-fluid'>
<a href="{% url 'category-create' %}?category={{ category.id }}">
<button class="btn btn-default">New Category</button>
</a>
<a href="{% url 'part-create' %}?category={{ category.id }}">
<button class="btn btn-default">New Part</button>
</a>
<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="button btn-danger">Delete Category</button>
</a>
</div>
{% endblock %}

View File

@ -0,0 +1,16 @@
{% extends "base.html" %}
{% block content %}
{% include "part/cat_link.html" with category=category %}
<div class='panel panel-primary'>
<div class='panel-heading'>Edit details for category '{{ category.name }}'</div>
<div class='panel-body'>
{% load crispy_forms_tags %}
{% crispy form %}
</div>
</div>
{% endblock %}

View File

@ -0,0 +1,16 @@
{% extends "base.html" %}
{% load static %}
{% block content %}
{% include "part/cat_link.html" with category=category %}
<div class='panel panel-primary'>
<div class='panel-heading'>Create a new part category{% if category %} in category '{{ category.name }}'{% endif %}</div>
<div class='panel-body'>
{% load crispy_forms_tags %}
{% crispy form %}
{% endblock %}

View File

@ -0,0 +1,10 @@
{% if parts|length > 0 %}
Parts:
<ul class="list-group">
{% for part in parts %}
<li class="list-group-item">
<a href="{% url 'part-detail' part.id %}">{{ part.name }}</a> - {{ part.description }}
</li>
{% endfor %}
</ul>
{% endif %}

View File

@ -0,0 +1,11 @@
{% if children|length > 0 %}
Subcategories:
<ul class="list-group">
{% for child in children %}
<li class="list-group-item">
<a href="{% url 'category-detail' child.id %}">{{ child.name }}</a> - {{ child.description }}
<span class='badge'>{{ child.partcount }}</span>
</li>
{% endfor %}
</ul>
{% endif %}

View File

@ -2,10 +2,14 @@
{% block details %}
Edit part information:
<div class='panel panel-primary'>
<div class='panel-heading'>Edit details for part '{{ part.name }}'</div>
<div class='panel-body'>
{% load crispy_forms_tags %}
{% crispy form %}
</div>
{% endblock %}

View File

@ -5,31 +5,17 @@
{% include "part/cat_link.html" with category=category %}
{% if children|length > 0 %}
Subcategories:
<ul class="list-group">
{% for child in children %}
<li class="list-group-item">
<a href="/part/list/?category={{ child.id }}">{{ child.name }}</a> - {{ child.description }}
<span class='badge'>{{ child.partcount }}</span>
</li>
{% endfor %}
</ul>
{% endif %}
{% include "part/category_subcategories.html" with children=children %}
{% if parts|length > 0 %}
Parts:
<ul class="list-group">
{% for part in parts %}
<li class="list-group-item">
<a href="{% url 'part-detail' part.id %}">{{ part.name }}</a> - {{ part.description }}
</li>
{% endfor %}
</ul>
{% endif %}
{% include "part/category_parts.html" with parts=parts %}
<a href="/part/create/{% if category %}?category={{ category.id }}{% endif %}">
<div class='container-fluid'>
<a href="{% url 'category-create' %}">
<button class="btn btn-default">New Category</button>
</a>
<a href="{% url 'part-create' %}">
<button class="btn btn-default">New Part</button>
</a>
</div>
{% endblock %}

View File

@ -52,16 +52,30 @@ part_detail_urls = [
url(r'^.*$', views.PartDetail.as_view(), name='part-detail'),
]
part_category_urls = [
url(r'^edit/?', views.CategoryEdit.as_view(), name='category-edit'),
url(r'^delete/?', views.CategoryDelete.as_view(), name='category-delete'),
url('^.*$', views.CategoryDetail.as_view(), name='category-detail'),
]
# URL list for part web interface
part_urls = [
# Create a new category
url(r'^new_category/?', views.CategoryCreate.as_view(), name='category-create'),
# Create a new part
url(r'^create/?', views.PartCreate.as_view(), name='part-create'),
url(r'^new/?', views.PartCreate.as_view(), name='part-create'),
# Individual
url(r'^(?P<pk>\d+)/', include(part_detail_urls)),
url('list', views.PartIndex.as_view(), name='part-index'),
# ex: /part/5/
# Part category
url(r'^category/(?P<pk>\d+)/', include(part_category_urls)),
# Top level part list (display top level parts and categories)
url('', views.PartIndex.as_view(), name='part-index'),
url(r'^.*$', RedirectView.as_view(url='list', permanent=False), name='part-index'),
]

View File

@ -8,7 +8,7 @@ from django.urls import reverse
from django.views.generic import DetailView, ListView
from django.views.generic.edit import UpdateView, DeleteView, CreateView
from .forms import EditPartForm
from .forms import EditPartForm, EditCategoryForm
class PartIndex(ListView):
model = Part
@ -16,21 +16,17 @@ class PartIndex(ListView):
context_object_name = 'parts'
def get_queryset(self):
self.category = self.request.GET.get('category', None)
return Part.objects.filter(category=self.category)
return Part.objects.filter(category=None)
def get_context_data(self, **kwargs):
context = super(PartIndex, self).get_context_data(**kwargs)
children = PartCategory.objects.filter(parent=self.category)
# View top-level categories
children = PartCategory.objects.filter(parent=None)
context['children'] = children
if self.category:
context['category'] = get_object_or_404(PartCategory, pk=self.category)
return context
@ -92,3 +88,61 @@ class PartDelete(DeleteView):
else:
return HttpResponseRedirect(self.get_object().get_absolute_url())
class CategoryDetail(DetailView):
model = PartCategory
context_object_name = 'category'
queryset = PartCategory.objects.all()
template_name = 'part/category_detail.html'
class CategoryEdit(UpdateView):
model = PartCategory
template_name = 'part/category_edit.html'
form_class = EditCategoryForm
def get_context_data(self, **kwargs):
context = super(CategoryEdit, self).get_context_data(**kwargs).copy()
context['category'] = get_object_or_404(PartCategory, pk=self.kwargs['pk'])
return context
class CategoryDelete(DeleteView):
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(CreateView):
model = PartCategory
template_name = 'part/category_new.html'
form_class = EditCategoryForm
def get_context_data(self, **kwargs):
context = super(CategoryCreate, self).get_context_data(**kwargs).copy()
parent_id = self.request.GET.get('category', None)
if parent_id:
context['category'] = get_object_or_404(PartCategory, pk=parent_id)
return context
def get_initial(self):
initials = super(CategoryCreate, self).get_initial().copy()
parent_id = self.request.GET.get('category', None)
if parent_id:
initials['parent'] = get_object_or_404(PartCategory, pk=parent_id)
return initials