Fix parent error when deleting category

- Any child categories have their parent set to the parent of the deleted category
This commit is contained in:
Oliver 2018-04-15 11:25:57 +10:00
parent 2c2db4fffb
commit 5b5b8f4d12
4 changed files with 65 additions and 1 deletions

View File

@ -4,6 +4,8 @@ from django.db import models
from django.contrib.contenttypes.models import ContentType
from rest_framework.exceptions import ValidationError
from django.db.models.signals import pre_delete
from django.dispatch import receiver
class Company(models.Model):
""" Abstract model representing an external company
@ -40,9 +42,12 @@ class InvenTreeTree(models.Model):
unique_together = ('name', 'parent')
name = models.CharField(max_length=100, unique=True)
description = models.CharField(max_length=250, blank=True)
# When a category is deleted, graft the children onto its parent
parent = models.ForeignKey('self',
on_delete=models.CASCADE,
on_delete=models.DO_NOTHING,
blank=True,
null=True,
related_name='children')
@ -179,6 +184,15 @@ class InvenTreeTree(models.Model):
return self.pathstring
@receiver(pre_delete, sender=InvenTreeTree, dispatch_uid='tree_pre_delete_log')
def before_delete_tree_item(sender, intance, using, **kwargs):
# Update each tree item below this one
for child in instance.children.all():
child.parent = instance.parent
child.save()
def FilterChildren(queryset, parent):
""" Filter a queryset, limit to only objects that are a child of the given parent
Filter is passed in the URL string, e.g. '/?parent=123'

View File

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2018-04-15 01:07
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('part', '0013_auto_20180414_2238'),
]
operations = [
migrations.AlterField(
model_name='partcategory',
name='parent',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='children', to='part.PartCategory'),
),
]

View File

@ -16,6 +16,9 @@ class PartCategory(InvenTreeTree):
""" PartCategory provides hierarchical organization of Part objects.
"""
def get_absolute_url(self):
return '/part/category/{id}/'.format(id=self.id)
class Meta:
verbose_name = "Part Category"
verbose_name_plural = "Part Categories"
@ -48,6 +51,11 @@ def before_delete_part_category(sender, instance, using, **kwargs):
part.category = instance.parent
part.save()
# Update each child category
for child in instance.children.all():
child.parent = instance.parent
child.save()
# Function to automatically rename a part image on upload
# Format: part_pk.<img>

View File

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2018-04-15 01:07
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('stock', '0004_auto_20180414_1032'),
]
operations = [
migrations.AlterField(
model_name='stocklocation',
name='parent',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='children', to='stock.StockLocation'),
),
]