Added container copy function

This commit is contained in:
Dirk Jahnke 2022-05-20 20:42:54 +02:00
parent 8b3199813d
commit 9adcee728a
3 changed files with 39 additions and 1 deletions

View File

@ -32,6 +32,7 @@
<td>
<a class="btn btn-outline-primary btn-sm" href="{% url 'container:delete' container.id %}" role="button">Delete</a>
<a class="btn btn-outline-primary btn-sm" href="{% url 'container:detail' container.id %}" role="button">View</a>
<a class="btn btn-outline-primary btn-sm" href="{% url 'container:copy' container.id %}" role="button">Copy</a>
<a class="btn btn-outline-primary btn-sm" href="{% url 'container:print_label' container.id %}" role="button">Label</a>
</td>
</tr>

View File

@ -1,5 +1,5 @@
from django.urls import include, path
from container.views import ContainerListView, ContainerUpdateView, ContainerCreateView, ContainerDetailView, ContainerDeleteView, ContainerPrintLabelView, ContainerImportView
from container.views import ContainerListView, ContainerUpdateView, ContainerCreateView, ContainerCopyView, ContainerDetailView, ContainerDeleteView, ContainerPrintLabelView, ContainerImportView
from container.views import ContainerTypeListView, ContainerTypeDetailView, ContainerTypeCreateView, ContainerTypeUpdateView, ContainerTypeDeleteView, ContainerTypeImportView
@ -8,6 +8,7 @@ urlpatterns = [
path('', ContainerListView.as_view(), name='list'),
path('<int:pk>/', ContainerDetailView.as_view(), name='detail'),
path('add/', ContainerCreateView.as_view(), name='add'),
path('copy/<int:pk>/', ContainerCopyView.as_view(), name='copy'),
path('import/', ContainerImportView.as_view(), name='container_import'),
path('edit/<int:pk>/', ContainerUpdateView.as_view(), name='update'),
path('label/<int:pk>/', ContainerPrintLabelView.as_view(), name='print_label'),

View File

@ -36,6 +36,42 @@ class ContainerCreateView(LoginRequiredMixin, generic.CreateView):
return super().form_valid(form)
def suggest_named_id(existing_id):
result = re.search(r'\d{0,9}$', existing_id)
number_part = int(result.group()) + 1
number_digits = result.span()[1] - result.span()[0]
prefix_part = existing_id[0:result.span()[0]]
id_candidate = prefix_part + str(number_part).zfill(number_digits)
while Container.objects.filter(named_id=id_candidate).exists():
number_part = number_part + 1
id_candidate = prefix_part + str(number_part).zfill(number_digits)
return id_candidate
class ContainerCopyView(LoginRequiredMixin, generic.CreateView):
model = Container
fields = ['named_id', 'description', 'color', 'container_type']
success_url = '/container/'
def form_valid(self, form):
if not form.instance.named_id.startswith('C-'):
form.instance.named_id = 'C-' + form.instance.named_id
form.instance.changed_by = self.request.user
form.instance.created_by = self.request.user
return super().form_valid(form)
def get_initial(self, *args, **kwargs):
initial = super(ContainerCopyView, self).get_initial(**kwargs)
copy_source = Container.objects.get(pk=self.kwargs['pk'])
initial['container_type'] = copy_source.container_type
initial['color'] = copy_source.color
initial['description'] = copy_source.description
# strategy to find a new named_id by using the numbers at the end and trying to increment
initial['named_id'] = suggest_named_id(existing_id=copy_source.named_id)
return initial
class ContainerImportView(LoginRequiredMixin, generic.TemplateView):
template_name = 'container_import.html'