mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Adds custom delete routine for PartCategory model
This commit is contained in:
parent
24af134a87
commit
82840b0590
@ -20,7 +20,7 @@ from django.db.models.functions import Coalesce
|
|||||||
from django.core.validators import MinValueValidator
|
from django.core.validators import MinValueValidator
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.db.models.signals import pre_delete, post_save
|
from django.db.models.signals import post_save
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
|
|
||||||
from jinja2 import Template
|
from jinja2 import Template
|
||||||
@ -76,6 +76,35 @@ class PartCategory(InvenTreeTree):
|
|||||||
default_keywords: Default keywords for parts created in this category
|
default_keywords: Default keywords for parts created in this category
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def delete(self, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Custom model deletion routine, which updates any child categories or parts.
|
||||||
|
This must be handled within a transaction.atomic(), otherwise the tree structure is damaged
|
||||||
|
"""
|
||||||
|
|
||||||
|
with transaction.atomic():
|
||||||
|
|
||||||
|
parent = self.parent
|
||||||
|
tree_id = self.tree_id
|
||||||
|
|
||||||
|
# Update each part in this category to point to the parent category
|
||||||
|
for part in self.parts.all():
|
||||||
|
part.category = self.parent
|
||||||
|
part.save()
|
||||||
|
|
||||||
|
# Update each child category
|
||||||
|
for child in self.children.all():
|
||||||
|
child.parent = self.parent
|
||||||
|
child.save()
|
||||||
|
|
||||||
|
super().delete(*args, **kwargs)
|
||||||
|
|
||||||
|
if parent is not None:
|
||||||
|
# Partially rebuild the tree (cheaper than a complete rebuild)
|
||||||
|
PartCategory.objects.partial_rebuild(tree_id)
|
||||||
|
else:
|
||||||
|
PartCategory.objects.rebuild()
|
||||||
|
|
||||||
default_location = TreeForeignKey(
|
default_location = TreeForeignKey(
|
||||||
'stock.StockLocation', related_name="default_categories",
|
'stock.StockLocation', related_name="default_categories",
|
||||||
null=True, blank=True,
|
null=True, blank=True,
|
||||||
@ -260,27 +289,6 @@ class PartCategory(InvenTreeTree):
|
|||||||
).delete()
|
).delete()
|
||||||
|
|
||||||
|
|
||||||
@receiver(pre_delete, sender=PartCategory, dispatch_uid='partcategory_delete_log')
|
|
||||||
def before_delete_part_category(sender, instance, using, **kwargs):
|
|
||||||
""" Receives before_delete signal for PartCategory object
|
|
||||||
|
|
||||||
Before deleting, update child Part and PartCategory objects:
|
|
||||||
|
|
||||||
- For each child category, set the parent to the parent of *this* category
|
|
||||||
- For each part, set the 'category' to the parent of *this* category
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Update each part in this category to point to the parent category
|
|
||||||
for part in instance.parts.all():
|
|
||||||
part.category = instance.parent
|
|
||||||
part.save()
|
|
||||||
|
|
||||||
# Update each child category
|
|
||||||
for child in instance.children.all():
|
|
||||||
child.parent = instance.parent
|
|
||||||
child.save()
|
|
||||||
|
|
||||||
|
|
||||||
def rename_part_image(instance, filename):
|
def rename_part_image(instance, filename):
|
||||||
""" Function for renaming a part image file
|
""" Function for renaming a part image file
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ class StockLocation(InvenTreeTree):
|
|||||||
parent = self.parent
|
parent = self.parent
|
||||||
tree_id = self.tree_id
|
tree_id = self.tree_id
|
||||||
|
|
||||||
# Update each part in the stock location
|
# Update each stock item in the stock location
|
||||||
for item in self.stock_items.all():
|
for item in self.stock_items.all():
|
||||||
item.location = self.parent
|
item.location = self.parent
|
||||||
item.save()
|
item.save()
|
||||||
|
Loading…
Reference in New Issue
Block a user