Modal forms for stock app

This commit is contained in:
Oliver 2018-04-27 22:59:08 +10:00
parent b705f3c62a
commit 4d6e2aca2c
11 changed files with 232 additions and 90 deletions

View File

@ -33,7 +33,11 @@ $(document).ready(function () {
$('#part-list').footable(); $('#part-list').footable();
$("#create-cat").click(function() { $("#create-cat").click(function() {
launchModalForm("#modal-form", "{% url 'category-create' %}"); launchModalForm("#modal-form",
"{% url 'category-create' %}",
{
follow: true
});
}); });
$("#create-part").click(function() { $("#create-part").click(function() {

View File

@ -1,25 +1,32 @@
{% extends "part/part_base.html" %} {% extends "part/part_base.html" %}
{% load static %}
{% block details %} {% block details %}
{% include 'part/tabs.html' with tab='stock' %} {% include 'part/tabs.html' with tab='stock' %}
<h3>Part Stock</h3> <h3>Part Stock</h3>
<table class="table table-striped"> <table class="table table-striped" id='stock-table' data-sorting='true'>
<thead>
<tr> <tr>
<th>Link</th> <th data-sortable='false'>Link</th>
<th>Quantity</th> <th>Quantity</th>
<th>Location</th> <th>Location</th>
<th>Supplier part</th> <th>Supplier part</th>
<th>Stocktake</th> <th>Stocktake</th>
<th>Notes</th> <th>Notes</th>
</tr> </tr>
</thead>
<tbody>
{% for stock in part.locations.all %} {% for stock in part.locations.all %}
<tr> <tr>
<td><a href="{% url 'stock-item-detail' stock.id %}">Click</a></td> <td><a href="{% url 'stock-item-detail' stock.id %}">Click</a></td>
<td>{{ stock.quantity }}</td> <td>{{ stock.quantity }}</td>
<td><a href="{% url 'stock-location-detail' stock.location.id %}">{{ stock.location.name }}</a></td> <td>
{% if stock.location %}
<a href="{% url 'stock-location-detail' stock.location.id %}">{{ stock.location.name }}</a>
{% endif %}
</td>
<td> <td>
{% if stock.supplier_part %} {% if stock.supplier_part %}
<a href="{% url 'supplier-part-detail' stock.supplier_part.id %}"> <a href="{% url 'supplier-part-detail' stock.supplier_part.id %}">
@ -31,12 +38,38 @@
<td>{{ stock.notes }}</td> <td>{{ stock.notes }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody>
</table </table
<div class='container-fluid'> <div class='container-fluid'>
<a href="{% url 'stock-item-create' %}?part={{ part.id }}"> <button class='btn btn-success' id='add-stock-item'>Add new Stock Item</button>
<button class='btn btn-success'>Add new Stock Item</button>
</a>
</div> </div>
{% include 'modals.html' %}
{% endblock %}
{% block javascript %}
<script type='text/javascript' src="{% static 'script/footable.js' %}"></script>
<script type='text/javascript' src="{% static 'script/modal_form.js' %}"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#stock-table').footable();
$('#add-stock-item').click(function () {
launchModalForm("#modal-form",
"{% url 'stock-item-create' %}",
{
reload: true,
data: {
part: {{ part.id }}
}
});
});
});
</script>
{% endblock %} {% endblock %}

View File

@ -14,11 +14,7 @@ class EditStockLocationForm(forms.ModelForm):
super(EditStockLocationForm, self).__init__(*args, **kwargs) super(EditStockLocationForm, self).__init__(*args, **kwargs)
self.helper = FormHelper() self.helper = FormHelper()
self.helper.form_id = 'id-edit-part-form' self.helper.form_tag = False
self.helper.form_class = 'blueForms'
self.helper.form_method = 'post'
self.helper.add_input(Submit('submit', 'Submit'))
class Meta: class Meta:
model = StockLocation model = StockLocation
@ -35,11 +31,7 @@ class EditStockItemForm(forms.ModelForm):
super(EditStockItemForm, self).__init__(*args, **kwargs) super(EditStockItemForm, self).__init__(*args, **kwargs)
self.helper = FormHelper() self.helper = FormHelper()
self.helper.form_id = 'id-edit-part-form' self.helper.form_tag = False
self.helper.form_class = 'blueForms'
self.helper.form_method = 'post'
self.helper.add_input(Submit('submit', 'Submit'))
class Meta: class Meta:
model = StockItem model = StockItem

View File

@ -1,5 +1,5 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load static %}
{% block content %} {% block content %}
{% include "stock/loc_link.html" with location=None %} {% include "stock/loc_link.html" with location=None %}
@ -9,14 +9,65 @@
{% include "stock/location_list.html" with locations=locations %} {% include "stock/location_list.html" with locations=locations %}
{% endif %} {% endif %}
{% if items|length > 0 %} <table class="table table-striped" id='stock-table' data-sorting='true'>
{% include "stock/stock_table.html" with items=items %} <thead>
{% endif %} <tr>
<th>Part</th>
<th>Location</th>
<th>Stock</th>
<th>Status</th>
<th>Stocktake</th>
<th data-sortable='false'></th>
</tr>
</thead>
<tbody>
{% for item in items.all %}
<tr>
<td><a href="{% url 'part-stock' item.part.id %}">{{ item.part.name }}</a></td>
<td>
{% if item.location %}
<a href="{% url 'stock-location-detail' item.location.id %}">
{{ item.location.pathstring }}
</a>
{% endif %}
</td>
<td>{{ item.quantity }}</td>
<td>{{ item.status }}</td>
<td>{{ item.stocktake_date }}</td>
<td><a href="{% url 'stock-item-detail' item.id %}">Click</a></td>
</tr>
{% endfor %}
</tbody>
</table>
<div class='container-fluid'> <div class='container-fluid'>
<a href="{% url 'stock-location-create' %}"> <button class="btn btn-success" id='location-create'>New Stock Location</button>
<button class="btn btn-success">New Stock Location</button>
</a>
</div> </div>
{% include 'modals.html' %}
{% endblock %}
{% block javascript %}
<script type='text/javascript' src="{% static 'script/footable.js' %}"></script>
<script type='text/javascript' src="{% static 'script/modal_form.js' %}"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#stock-table').footable();
$('#location-create').click(function () {
launchModalForm("#modal-form",
"{% url 'stock-location-create' %}",
{
follow: true
});
});
});
</script>
{% endblock %} {% endblock %}

View File

@ -1,5 +1,5 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load static %}
{% block content %} {% block content %}
{% include "stock/loc_link.html" with location=item.location %} {% include "stock/loc_link.html" with location=item.location %}
@ -94,12 +94,38 @@
{% endif %} {% endif %}
<div class='container-fluid'> <div class='container-fluid'>
<a href="{% url 'stock-item-edit' item.id %}"> <button class='btn btn-info' id='edit-item'>Edit Stock Item</button>
<button class='btn btn-info'>Edit Stock Item</button> <button class='btn btn-danger' id='delete-item'>Delete Stock Item</button>
</a>
<a href="{% url 'stock-item-delete' item.id %}">
<button class='btn btn-danger'>Delete Stock Item</button>
</a>
</div> </div>
{% include 'modals.html' %}
{% endblock %}
{% block javascript %}
<script type='text/javascript' src="{% static 'script/footable.js' %}"></script>
<script type='text/javascript' src="{% static 'script/modal_form.js' %}"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#edit-item").click(function () {
launchModalForm("#modal-form",
"{% url 'stock-item-edit' item.id %}",
{
reload: true
});
});
$("#delete-item").click(function () {
launchDeleteForm("#modal-delete",
"{% url 'stock-item-delete' item.id %}",
{
redirect: "{% url 'part-stock' item.part.id %}"
});
});
});
</script>
{% endblock %} {% endblock %}

View File

@ -1,9 +1,5 @@
{% extends "delete_obj.html" %}
{% block del_title %}
Are you sure you want to delete this stock item? Are you sure you want to delete this stock item?
{% endblock %}
{% block del_body %} <br>
This will remove <b>{{ item.quantity }}</b> units of <b>'{{ item.part.name }}'</b> from stock.
{% endblock %} This will remove <b>{{ item.quantity }}</b> units of <b>{{ item.part.name }}</b> from stock.

View File

@ -1,5 +1,5 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load static %}
{% block content %} {% block content %}
{% include "stock/loc_link.html" with location=location %} {% include "stock/loc_link.html" with location=location %}
@ -12,27 +12,70 @@
{% include "stock/location_list.html" with locations=location.children %} {% include "stock/location_list.html" with locations=location.children %}
{% endif %} {% endif %}
{% if location.has_items %}
<h4>Stock Items</h4> <h4>Stock Items</h4>
{% include "stock/stock_table.html" with items=location.items %} {% include "stock/stock_table.html" with items=location.items %}
{% endif %}
<div class='container-fluid'> <div class='container-fluid'>
<a href="{% url 'stock-location-create' %}?location={{ location.id }}"> <button class='btn btn-success' id='location-create'>New Stock Location</button>
<button class='btn btn-success'>New Stock Location</button> <button class='btn btn-success' id='new-item'>New Stock Item</button>
</a> <button class="btn btn-info" id='location-edit'>Edit Location</button>
<a href="{% url 'stock-item-create' %}?location={{ location.id }}"> <button class="btn btn-danger" id='location-delete'>Delete Location</button>
<button class='btn btn-success'>New Stock Item</button>
</a>
<a href="{% url 'stock-location-edit' location.id %}">
<button class="btn btn-info">Edit Location</button>
</a>
<a href="{% url 'stock-location-delete' location.id %}">
<button class="btn btn-danger">Delete Location</button>
</a>
</div> </div>
{% endblock %} {% include 'modals.html' %}
{% endblock %}
{% block javascript %}
<script type='text/javascript' src="{% static 'script/footable.js' %}"></script>
<script type='text/javascript' src="{% static 'script/modal_form.js' %}"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#stock-table').footable();
$('#location-create').click(function () {
launchModalForm("#modal-form",
"{% url 'stock-location-create' %}",
{
data: {
location: {{ location.id }}
},
follow: true
});
});
$('#location-edit').click(function() {
launchModalForm("#modal-form",
"{% url 'stock-location-edit' location.id %}",
{
reload: true
});
});
$('#location-delete').click(function() {
launchDeleteForm("#modal-delete",
"{% url 'stock-location-delete' location.id %}",
{
redirect: "{% url 'stock-index' %}"
});
})
$('#new-item').click(function () {
launchModalForm("#modal-form",
"{% url 'stock-item-create' %}",
{
reload: true,
data: {
location: {{ location.id }}
}
});
});
});
</script>
{% endblock %}

View File

@ -1,10 +1,7 @@
{% extends 'delete_obj.html' %}
{% block del_title %}
Are you sure you want to delete stock location '{{ location.name }}'? Are you sure you want to delete stock location '{{ location.name }}'?
{% endblock %}
{% block del_body %} <br>
{% if location.children.all|length > 0 %} {% if location.children.all|length > 0 %}
<p>This location contains {{ location.children.all|length }} child locations.<br> <p>This location contains {{ location.children.all|length }} child locations.<br>
If this location is deleted, these child locations will be moved to If this location is deleted, these child locations will be moved to
@ -37,5 +34,3 @@ If this location is deleted, these items will be moved to the top level 'Stock'
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}
{% endblock %}

View File

@ -1,5 +1,5 @@
<ul class="list-group"> <ul class="list-group">
{% for child in locations.all %} {% for child in locations.all %}
<li class="list-group-item"><a href="{% url 'stock-location-detail' child.id %}">{{ child.name }}</a></li> <li class="list-group-item"><a href="{% url 'stock-location-detail' child.id %}">{{ child.name }}</a> - <i>{{ child.description }}</li>
{% endfor %} {% endfor %}
</ul> </ul>

View File

@ -1,11 +1,14 @@
<table class="table table-striped"> <table class="table table-striped" id='stock-table' data-sorting='true'>
<thead>
<tr> <tr>
<th>Part</th> <th>Part</th>
<th>Stock</th> <th>Stock</th>
<th>Status</th> <th>Status</th>
<th>Stocktake</th> <th>Stocktake</th>
<th></th> <th data-sortable='false'></th>
</tr> </tr>
</thead>
<tbody>
{% for item in items.all %} {% for item in items.all %}
<tr> <tr>
<td><a href="{% url 'part-stock' item.part.id %}">{{ item.part.name }}</a></td> <td><a href="{% url 'part-stock' item.part.id %}">{{ item.part.name }}</a></td>
@ -15,4 +18,5 @@
<td><a href="{% url 'stock-item-detail' item.id %}">Click</a></td> <td><a href="{% url 'stock-item-detail' item.id %}">Click</a></td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody>
</table> </table>

View File

@ -4,6 +4,8 @@ from django.http import HttpResponseRedirect
from django.views.generic import DetailView, ListView from django.views.generic import DetailView, ListView
from django.views.generic.edit import UpdateView, DeleteView, CreateView from django.views.generic.edit import UpdateView, DeleteView, CreateView
from InvenTree.views import AjaxUpdateView, AjaxDeleteView, AjaxCreateView
from part.models import Part from part.models import Part
from .models import StockItem, StockLocation from .models import StockItem, StockLocation
@ -14,18 +16,16 @@ from .forms import EditStockItemForm
class StockIndex(ListView): class StockIndex(ListView):
model = StockItem model = StockItem
template_name = 'stock/index.html' template_name = 'stock/index.html'
context_obect_name = 'items' context_obect_name = 'locations'
paginate_by = 50
def get_queryset(self):
return StockItem.objects.filter(location=None)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(StockIndex, self).get_context_data(**kwargs).copy() context = super(StockIndex, self).get_context_data(**kwargs).copy()
# Return all top-level locations
locations = StockLocation.objects.filter(parent=None) locations = StockLocation.objects.filter(parent=None)
context['locations'] = locations context['locations'] = locations
context['items'] = StockItem.objects.all()
return context return context
@ -44,25 +44,31 @@ class StockItemDetail(DetailView):
model = StockItem model = StockItem
class StockLocationEdit(UpdateView): class StockLocationEdit(AjaxUpdateView):
model = StockLocation model = StockLocation
form_class = EditStockLocationForm form_class = EditStockLocationForm
template_name = 'stock/location_edit.html' template_name = 'stock/location_edit.html'
context_object_name = 'location' context_object_name = 'location'
ajax_template_name = 'modal_form.html'
ajax_form_title = 'Edit Stock Location'
class StockItemEdit(UpdateView): class StockItemEdit(AjaxUpdateView):
model = StockItem model = StockItem
form_class = EditStockItemForm form_class = EditStockItemForm
template_name = 'stock/item_edit.html' template_name = 'stock/item_edit.html'
context_object_name = 'item' context_object_name = 'item'
ajax_template_name = 'modal_form.html'
ajax_form_title = 'Edit Stock Item'
class StockLocationCreate(CreateView): class StockLocationCreate(AjaxCreateView):
model = StockLocation model = StockLocation
form_class = EditStockLocationForm form_class = EditStockLocationForm
template_name = 'stock/location_create.html' template_name = 'stock/location_create.html'
context_object_name = 'location' context_object_name = 'location'
ajax_template_name = 'modal_form.html'
ajax_form_title = 'Create new Stock Location'
def get_initial(self): def get_initial(self):
initials = super(StockLocationCreate, self).get_initial().copy() initials = super(StockLocationCreate, self).get_initial().copy()
@ -75,11 +81,13 @@ class StockLocationCreate(CreateView):
return initials return initials
class StockItemCreate(CreateView): class StockItemCreate(AjaxCreateView):
model = StockItem model = StockItem
form_class = EditStockItemForm form_class = EditStockItemForm
template_name = 'stock/item_create.html' template_name = 'stock/item_create.html'
context_object_name = 'item' context_object_name = 'item'
ajax_template_name = 'modal_form.html'
ajax_form_title = 'Create new Stock Item'
def get_initial(self): def get_initial(self):
initials = super(StockItemCreate, self).get_initial().copy() initials = super(StockItemCreate, self).get_initial().copy()
@ -100,27 +108,17 @@ class StockItemCreate(CreateView):
return initials return initials
class StockLocationDelete(DeleteView): class StockLocationDelete(AjaxDeleteView):
model = StockLocation model = StockLocation
success_url = '/stock' success_url = '/stock'
template_name = 'stock/location_delete.html' template_name = 'stock/location_delete.html'
context_object_name = 'location' context_object_name = 'location'
ajax_form_title = 'Delete Stock Location'
def post(self, request, *args, **kwargs):
if 'confirm' in request.POST:
return super(StockLocationDelete, self).post(request, *args, **kwargs)
else:
return HttpResponseRedirect(self.get_object().get_absolute_url())
class StockItemDelete(DeleteView): class StockItemDelete(AjaxDeleteView):
model = StockItem model = StockItem
success_url = '/stock/' success_url = '/stock/'
template_name = 'stock/item_delete.html' template_name = 'stock/item_delete.html'
context_object_name = 'item' context_object_name = 'item'
ajax_form_title = 'Delete Stock Item'
def post(self, request, *args, **kwargs):
if 'confirm' in request.POST:
return super(StockItemDelete, self).post(request, *args, **kwargs)
else:
return HttpResponseRedirect(self.get_object().get_absolute_url())