|
| 1 | +# Generated by Django 6.0 on 2025-12-31 12:01 |
| 2 | + |
| 3 | +import django.db.models.deletion |
| 4 | +from django.db import migrations, models |
| 5 | + |
| 6 | + |
| 7 | +def convert_location_groups_to_parents(apps, schema_editor): |
| 8 | + """Convert LocationGroups to parent Locations.""" |
| 9 | + Location = apps.get_model("inventory", "Location") |
| 10 | + LocationGroup = apps.get_model("inventory", "LocationGroup") |
| 11 | + |
| 12 | + # Create a mapping of location_group_id to parent Location |
| 13 | + group_to_parent = {} |
| 14 | + |
| 15 | + # Get all existing location groups |
| 16 | + all_groups = list(LocationGroup.objects.all()) |
| 17 | + if not all_groups: |
| 18 | + return # No groups to convert |
| 19 | + |
| 20 | + # Use the first group as a temporary placeholder for new parent locations |
| 21 | + temp_group = all_groups[0] |
| 22 | + |
| 23 | + # For each LocationGroup, create a parent Location |
| 24 | + for group in all_groups: |
| 25 | + parent_location = Location.objects.create( |
| 26 | + name=group.name, |
| 27 | + order=group.order, |
| 28 | + display_as_root=True, |
| 29 | + location_group=temp_group, # Temporary assignment |
| 30 | + ) |
| 31 | + group_to_parent[group.id] = parent_location |
| 32 | + |
| 33 | + # Update all existing Locations to point to their parent |
| 34 | + for location in Location.objects.exclude( |
| 35 | + id__in=[p.id for p in group_to_parent.values()] |
| 36 | + ): |
| 37 | + if location.location_group_id and location.location_group_id in group_to_parent: |
| 38 | + location.parent = group_to_parent[location.location_group_id] |
| 39 | + location.save(update_fields=["parent"]) |
| 40 | + |
| 41 | + |
| 42 | +def reverse_convert(apps, schema_editor): |
| 43 | + """Reverse the conversion - not fully reversible but best effort.""" |
| 44 | + Location = apps.get_model("inventory", "Location") |
| 45 | + LocationGroup = apps.get_model("inventory", "LocationGroup") |
| 46 | + |
| 47 | + # Find all locations that are display_as_root (these were groups) |
| 48 | + for location in Location.objects.filter(display_as_root=True): |
| 49 | + # Create a LocationGroup |
| 50 | + group = LocationGroup.objects.create( |
| 51 | + name=location.name, |
| 52 | + order=location.order or 0, |
| 53 | + ) |
| 54 | + # Update all children to point to this group |
| 55 | + location.children.all().update(location_group=group) |
| 56 | + # Delete the parent location |
| 57 | + location.delete() |
| 58 | + |
| 59 | + |
| 60 | +class Migration(migrations.Migration): |
| 61 | + |
| 62 | + dependencies = [ |
| 63 | + ("inventory", "0024_update_status_colors_to_hex"), |
| 64 | + ] |
| 65 | + |
| 66 | + operations = [ |
| 67 | + migrations.AlterUniqueTogether( |
| 68 | + name="assetonjournaldocumentline", |
| 69 | + unique_together=None, |
| 70 | + ), |
| 71 | + migrations.RemoveField( |
| 72 | + model_name="assetonjournaldocumentline", |
| 73 | + name="asset", |
| 74 | + ), |
| 75 | + migrations.RemoveField( |
| 76 | + model_name="assetonjournaldocumentline", |
| 77 | + name="document_line", |
| 78 | + ), |
| 79 | + migrations.AlterUniqueTogether( |
| 80 | + name="assetonrecurringsalesinvoicedocumentline", |
| 81 | + unique_together=None, |
| 82 | + ), |
| 83 | + migrations.RemoveField( |
| 84 | + model_name="assetonrecurringsalesinvoicedocumentline", |
| 85 | + name="asset", |
| 86 | + ), |
| 87 | + migrations.RemoveField( |
| 88 | + model_name="assetonrecurringsalesinvoicedocumentline", |
| 89 | + name="document_line", |
| 90 | + ), |
| 91 | + migrations.AlterModelOptions( |
| 92 | + name="location", |
| 93 | + options={ |
| 94 | + "ordering": ["order", "pk"], |
| 95 | + "verbose_name": "location", |
| 96 | + "verbose_name_plural": "locations", |
| 97 | + }, |
| 98 | + ), |
| 99 | + migrations.AddField( |
| 100 | + model_name="location", |
| 101 | + name="display_as_root", |
| 102 | + field=models.BooleanField( |
| 103 | + default=False, |
| 104 | + help_text="Display this location as a root location (don't show parents in name)", |
| 105 | + verbose_name="display as root", |
| 106 | + ), |
| 107 | + ), |
| 108 | + migrations.AddField( |
| 109 | + model_name="location", |
| 110 | + name="parent", |
| 111 | + field=models.ForeignKey( |
| 112 | + blank=True, |
| 113 | + null=True, |
| 114 | + on_delete=django.db.models.deletion.SET_NULL, |
| 115 | + related_name="children", |
| 116 | + to="inventory.location", |
| 117 | + verbose_name="parent location", |
| 118 | + ), |
| 119 | + ), |
| 120 | + migrations.RunPython(convert_location_groups_to_parents, reverse_convert), |
| 121 | + migrations.RemoveField( |
| 122 | + model_name="location", |
| 123 | + name="location_group", |
| 124 | + ), |
| 125 | + migrations.DeleteModel( |
| 126 | + name="AssetOnEstimateDocumentLine", |
| 127 | + ), |
| 128 | + migrations.DeleteModel( |
| 129 | + name="AssetOnJournalDocumentLine", |
| 130 | + ), |
| 131 | + migrations.DeleteModel( |
| 132 | + name="AssetOnRecurringSalesInvoiceDocumentLine", |
| 133 | + ), |
| 134 | + migrations.DeleteModel( |
| 135 | + name="LocationGroup", |
| 136 | + ), |
| 137 | + ] |
0 commit comments