Added "new part" feature

- Create a new part
- Button provided on the "category" list page
This commit is contained in:
Oliver 2018-04-15 10:08:44 +10:00
parent ff2082b156
commit 2c2db4fffb
4 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,18 @@
{% 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{% if category %} in category '{{ category.name }}'{% endif %}</div>
<div class='panel-body'>
{% load crispy_forms_tags %}
{% crispy form %}
</div>
{% endblock %}

View File

@ -28,5 +28,8 @@ Parts:
</ul>
{% endif %}
<a href="/part/create/{% if category %}?category={{ category.id }}{% endif %}">
<button class="btn btn-default">New Part</button>
</a>
{% endblock %}

View File

@ -54,6 +54,9 @@ part_detail_urls = [
# URL list for part web interface
part_urls = [
# Create a new part
url(r'^create/?', views.PartCreate.as_view(), name='part-create'),
# Individual
url(r'^(?P<pk>\d+)/', include(part_detail_urls)),

View File

@ -34,6 +34,40 @@ class PartIndex(ListView):
return context
class PartCreate(CreateView):
""" Create a new part
- Optionally provide a category object as initial data
"""
model = Part
form_class = EditPartForm
template_name = 'part/create.html'
def get_category_id(self):
return self.request.GET.get('category', None)
# If a category is provided in the URL, pass that to the page context
def get_context_data(self, **kwargs):
context = super(PartCreate, self).get_context_data(**kwargs)
# Add category information to the page
cat_id = self.get_category_id()
if cat_id:
context['category'] = get_object_or_404(PartCategory, pk=cat_id)
return context
# Pre-fill the category field if a valid category is provided
def get_initial(self):
initials = super(PartCreate, self).get_initial().copy()
if self.get_category_id():
initials['category'] = get_object_or_404(PartCategory, pk=self.get_category_id())
return initials
class PartDetail(DetailView):
context_object_name = 'part'
queryset = Part.objects.all()