From 2c2db4fffb8e6b484a4d285428b81e2e9b7934ff Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 15 Apr 2018 10:08:44 +1000 Subject: [PATCH] Added "new part" feature - Create a new part - Button provided on the "category" list page --- InvenTree/part/templates/part/create.html | 18 ++++++++++++ InvenTree/part/templates/part/index.html | 3 ++ InvenTree/part/urls.py | 3 ++ InvenTree/part/views.py | 34 +++++++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 InvenTree/part/templates/part/create.html diff --git a/InvenTree/part/templates/part/create.html b/InvenTree/part/templates/part/create.html new file mode 100644 index 0000000000..35155906fe --- /dev/null +++ b/InvenTree/part/templates/part/create.html @@ -0,0 +1,18 @@ +{% extends "base.html" %} + +{% load static %} + +{% block content %} + +{% include "part/cat_link.html" with category=category %} + +
+
Create a new part{% if category %} in category '{{ category.name }}'{% endif %}
+
+ +{% load crispy_forms_tags %} +{% crispy form %} + +
+ +{% endblock %} diff --git a/InvenTree/part/templates/part/index.html b/InvenTree/part/templates/part/index.html index 962db4ab2a..5710605fa2 100644 --- a/InvenTree/part/templates/part/index.html +++ b/InvenTree/part/templates/part/index.html @@ -28,5 +28,8 @@ Parts: {% endif %} + + + {% endblock %} diff --git a/InvenTree/part/urls.py b/InvenTree/part/urls.py index 6bc95369da..6f2275c174 100644 --- a/InvenTree/part/urls.py +++ b/InvenTree/part/urls.py @@ -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\d+)/', include(part_detail_urls)), diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 4e0c314037..995bbcdeda 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -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()