Improve inventory editing and shared equipment views
This commit is contained in:
1 parent
a85de26ee7
commit
84ed7da07f
19 files changed
+631
-206
No files matched your search
+22
-5
@@ -1,5 +1,6 @@
|
||||
from django import forms
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db.models import Q
|
||||
from django.forms.models import ModelChoiceIterator
|
||||
|
||||
from .models import (
|
||||
@@ -311,9 +312,14 @@ class VariantSelectionForm(forms.Form):
|
||||
)
|
||||
quantity = forms.IntegerField(label="Stückzahl", min_value=1, max_value=9999, initial=1)
|
||||
|
||||
def __init__(self, *args, selection=None, for_inventory=False, **kwargs):
|
||||
def __init__(self, *args, selection=None, for_inventory=False, current_variant=None, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.for_inventory = for_inventory
|
||||
self.available_variants = Variant.objects.filter(is_active=True)
|
||||
if current_variant is not None:
|
||||
self.available_variants = Variant.objects.filter(
|
||||
Q(is_active=True) | Q(pk=current_variant.pk)
|
||||
)
|
||||
if not for_inventory:
|
||||
del self.fields["spool_brand"]
|
||||
values = self.data if self.is_bound else (selection or {})
|
||||
@@ -325,7 +331,7 @@ class VariantSelectionForm(forms.Form):
|
||||
Variant.SpoolType.SPOOL,
|
||||
):
|
||||
self.initial.pop("spool_brand", None)
|
||||
active = Variant.objects.filter(is_active=True)
|
||||
active = self.available_variants
|
||||
maker = selection_id(values.get("manufacturer"))
|
||||
product = selection_id(values.get("product_type"))
|
||||
color = selection_id(values.get("color"))
|
||||
@@ -391,13 +397,15 @@ class VariantSelectionForm(forms.Form):
|
||||
cleaned.get(k)
|
||||
for k in ("manufacturer", "material", "product_type", "color", "spool_type")
|
||||
):
|
||||
variants = Variant.objects.filter(
|
||||
is_active=True,
|
||||
variants = self.available_variants.filter(
|
||||
product_type=cleaned["product_type"],
|
||||
product_type__manufacturer=cleaned["manufacturer"],
|
||||
color=cleaned["color"],
|
||||
)
|
||||
self.variant = variants.filter(spool_type=cleaned["spool_type"]).first()
|
||||
preferred = selection_id(cleaned.get("catalog_variant"))
|
||||
self.variant = variants.filter(pk=preferred).first() if preferred else None
|
||||
if self.variant is None:
|
||||
self.variant = variants.filter(spool_type=cleaned["spool_type"]).first()
|
||||
if self.variant is None and self.for_inventory:
|
||||
self.variant = variants.first()
|
||||
if self.variant is None:
|
||||
@@ -409,3 +417,12 @@ class VariantSelectionForm(forms.Form):
|
||||
else:
|
||||
self.bambu_spool = False
|
||||
return cleaned
|
||||
|
||||
|
||||
class InventoryEditForm(VariantSelectionForm):
|
||||
expected_quantity = forms.IntegerField(min_value=1, max_value=9999, widget=forms.HiddenInput)
|
||||
expected_revision = forms.UUIDField(widget=forms.HiddenInput)
|
||||
catalog_variant = forms.CharField(required=False, max_length=19, widget=forms.HiddenInput)
|
||||
|
||||
def __init__(self, *args, item, **kwargs):
|
||||
super().__init__(*args, for_inventory=True, current_variant=item.variant, **kwargs)
|
||||
Reference in new issue
Block a user