Expand possibilities for variant conversion

- Ref get_conversion_options
This commit is contained in:
Oliver Walters 2021-05-27 12:35:55 +10:00
parent 3a36e800c0
commit c2fe5e08b4
4 changed files with 60 additions and 2 deletions

View File

@ -1861,6 +1861,59 @@ class Part(MPTTModel):
return self.get_descendants(include_self=False)
@property
def can_convert(self):
"""
Check if this Part can be "converted" to a different variant:
It can be converted if:
a) It has non-virtual variant parts underneath it
b) It has non-virtual template parts above it
c) It has non-virtual sibling variants
"""
return self.get_conversion_options().count() > 0
def get_conversion_options(self):
"""
Return options for converting this part to a "variant" within the same tree
a) Variants underneath this one
b) Immediate parent
c) Siblings
"""
parts = []
# Child parts
children = self.get_descendants(include_self=False)
for child in children:
parts.append(child)
# Immediate parent
if self.variant_of:
parts.append(self.variant_of)
siblings = self.get_siblings(include_self=False)
for sib in siblings:
parts.append(sib)
filtered_parts = Part.objects.filter(pk__in=[part.pk for part in parts])
# Ensure this part is not in the queryset, somehow
filtered_parts = filtered_parts.exclude(pk=self.pk)
filtered_parts = filtered_parts.filter(
active=True,
virtual=False,
)
return filtered_parts
def get_related_parts(self):
""" Return list of tuples for all related parts:
- first value is PartRelated object

View File

@ -102,6 +102,11 @@
</div>
<div class='info-messages'>
{% if part.virtual %}
<div class='alert alert-warning alert-block'>
{% trans "This is a virtual part" %}
</div>
{% endif %}
{% if part.variant_of %}
<div class='alert alert-info alert-block'>
{% object_link 'part-variants' part.variant_of.id part.variant_of.full_name as link %}

View File

@ -139,7 +139,7 @@
<div class='btn-group'>
<button id='stock-edit-actions' title='{% trans "Stock actions" %}' class='btn btn-default dropdown-toggle' type='button' data-toggle='dropdown'><span class='fas fa-tools'></span> <span class='caret'></span></button>
<ul class='dropdown-menu' role='menu'>
{% if item.part.has_variants %}
{% if item.part.can_convert %}
<li><a href='#' id='stock-convert' title='{% trans "Convert to variant" %}'><span class='fas fa-screwdriver'></span> {% trans "Convert to variant" %}</a></li>
{% endif %}
{% if roles.stock.add %}

View File

@ -1373,7 +1373,7 @@ class StockItemConvert(AjaxUpdateView):
form = super().get_form()
item = self.get_object()
form.fields['part'].queryset = item.part.get_all_variants()
form.fields['part'].queryset = item.part.get_conversion_options()
return form