Add ability to 'copy' a part

- Launch a PartCreate form
- initialize form data with the existing part (if it exists!)
- use model_to_dict to pass data around
This commit is contained in:
Oliver Walters 2019-04-18 23:12:29 +10:00
parent 590dedce13
commit deaaa48b7d

View File

@ -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
@ -77,7 +78,18 @@ 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_part', None)
if part_to_copy:
try:
original = Part.objects.get(pk=part_to_copy)
initials = model_to_dict(original)
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())