mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Merge pull request #136 from SchrodingersGat/duplicate-part
Duplicate part
This commit is contained in:
commit
2f268311b8
@ -4,6 +4,15 @@ from wsgiref.util import FileWrapper
|
||||
from django.http import StreamingHttpResponse
|
||||
|
||||
|
||||
def str2bool(text, test=True):
|
||||
""" Test if a string 'looks' like a boolean value
|
||||
"""
|
||||
if test:
|
||||
return str(text).lower() in ['1', 'y', 'yes', 't', 'true', 'ok', ]
|
||||
else:
|
||||
return str(text).lower() in ['0', 'n', 'no', 'none', 'f', 'false', ]
|
||||
|
||||
|
||||
def WrapWithQuotes(text):
|
||||
# TODO - Make this better
|
||||
if not text.startswith('"'):
|
||||
|
@ -3,6 +3,7 @@ from __future__ import unicode_literals
|
||||
from django.db import models
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from rest_framework.exceptions import ValidationError
|
||||
from .helpers import str2bool
|
||||
|
||||
from django.db.models.signals import pre_delete
|
||||
from django.dispatch import receiver
|
||||
@ -183,7 +184,7 @@ def FilterChildren(queryset, parent):
|
||||
|
||||
if not parent:
|
||||
return queryset
|
||||
elif str(parent).lower() in ['none', 'false', 'null', 'top', '0']:
|
||||
elif str2bool(parent, False):
|
||||
return queryset.filter(parent=None)
|
||||
else:
|
||||
try:
|
||||
|
@ -32,7 +32,7 @@
|
||||
<td><a href="{{ part.URL }}">{{ part.URL }}</a></td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
|
||||
<button class='btn btn-success' id='duplicate-part'>Copy Part</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -95,4 +95,17 @@
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
$("#duplicate-part").click(function() {
|
||||
launchModalForm(
|
||||
"{% url 'part-create' %}",
|
||||
{
|
||||
follow: true,
|
||||
data: {
|
||||
copy: {{ part.id }},
|
||||
},
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
{% endblock %}
|
@ -5,6 +5,7 @@ from django.shortcuts import get_object_or_404
|
||||
|
||||
from django.urls import reverse_lazy
|
||||
from django.views.generic import DetailView, ListView
|
||||
from django.forms.models import model_to_dict
|
||||
|
||||
from company.models import Company
|
||||
from .models import PartCategory, Part, BomItem
|
||||
@ -49,7 +50,6 @@ class PartCreate(AjaxCreateView):
|
||||
"""
|
||||
model = Part
|
||||
form_class = EditPartForm
|
||||
template_name = 'part/create.html'
|
||||
|
||||
ajax_form_title = 'Create new part'
|
||||
ajax_template_name = 'modal_form.html'
|
||||
@ -77,7 +77,19 @@ class PartCreate(AjaxCreateView):
|
||||
# Pre-fill the category field if a valid category is provided
|
||||
def get_initial(self):
|
||||
|
||||
initials = super(PartCreate, self).get_initial().copy()
|
||||
# Is the client attempting to copy an existing part?
|
||||
part_to_copy = self.request.GET.get('copy', None)
|
||||
|
||||
if part_to_copy:
|
||||
try:
|
||||
original = Part.objects.get(pk=part_to_copy)
|
||||
initials = model_to_dict(original)
|
||||
self.ajax_form_title = "Copy Part '{p}'".format(p=original.name)
|
||||
except Part.DoesNotExist:
|
||||
initials = super(PartCreate, self).get_initial()
|
||||
|
||||
else:
|
||||
initials = super(PartCreate, self).get_initial()
|
||||
|
||||
if self.get_category_id():
|
||||
initials['category'] = get_object_or_404(PartCategory, pk=self.get_category_id())
|
||||
|
@ -13,6 +13,7 @@
|
||||
<h3>
|
||||
<div style='float: right;'>
|
||||
<div class="dropdown" style="float: right;">
|
||||
<button class='btn btn-primary' type='button' id='duplicate-item'>Copy Stock Item</button>
|
||||
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Options
|
||||
<span class="caret"></span></button>
|
||||
<ul class="dropdown-menu">
|
||||
@ -140,6 +141,19 @@
|
||||
{% endblock %}
|
||||
{% block js_ready %}
|
||||
{{ block.super }}
|
||||
|
||||
$("#duplicate-item").click(function() {
|
||||
launchModalForm(
|
||||
"{% url 'stock-item-create' %}",
|
||||
{
|
||||
follow: true,
|
||||
data: {
|
||||
copy: {{ item.id }},
|
||||
},
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
$("#stock-edit").click(function () {
|
||||
launchModalForm(
|
||||
"{% url 'stock-item-edit' item.id %}",
|
||||
|
@ -4,6 +4,7 @@ from __future__ import unicode_literals
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from django.views.generic import DetailView, ListView
|
||||
from django.forms.models import model_to_dict
|
||||
|
||||
from InvenTree.views import AjaxUpdateView, AjaxDeleteView, AjaxCreateView
|
||||
|
||||
@ -126,7 +127,20 @@ class StockItemCreate(AjaxCreateView):
|
||||
ajax_form_title = 'Create new Stock Item'
|
||||
|
||||
def get_initial(self):
|
||||
initials = super(StockItemCreate, self).get_initial().copy()
|
||||
|
||||
# Is the client attempting to copy an existing stock item?
|
||||
item_to_copy = self.request.GET.get('copy', None)
|
||||
|
||||
if item_to_copy:
|
||||
try:
|
||||
original = StockItem.objects.get(pk=item_to_copy)
|
||||
initials = model_to_dict(original)
|
||||
self.ajax_form_title = "Copy Stock Item"
|
||||
except StockItem.DoesNotExist:
|
||||
initials = super(StockItemCreate, self).get_initial().copy()
|
||||
|
||||
else:
|
||||
initials = super(StockItemCreate, self).get_initial().copy()
|
||||
|
||||
part_id = self.request.GET.get('part', None)
|
||||
loc_id = self.request.GET.get('location', None)
|
||||
|
Loading…
Reference in New Issue
Block a user