Skip to content

Commit eb9481b

Browse files
committed
Add migrations
1 parent 7e20e88 commit eb9481b

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{% for location in locations %}
2+
{% if location.children_list %}
3+
<div class="form-check" style="margin-left: {{ depth }}rem;">
4+
<input class="form-check-input location-parent-checkbox location-checkbox" type="checkbox" name="location" value="{{ location.id }}"
5+
id="location-parent-{{ location.id }}"
6+
data-location-id="{{ location.id }}"
7+
data-group-id="{{ group_id }}"
8+
{% if parent_id %}data-parent-id="{{ parent_id }}"{% endif %}
9+
{% if location.id|stringformat:"s" in current_filters.locations %}checked{% endif %}
10+
onchange="toggleLocationParent({{ location.id }}, {{ group_id }})">
11+
<label class="form-check-label small fw-semibold" for="location-parent-{{ location.id }}">
12+
{{ location.name }}
13+
{% if location.total_asset_count > 0 %}
14+
<span class="text-muted">({{ location.total_asset_count }})</span>
15+
{% endif %}
16+
</label>
17+
</div>
18+
{% include "partials/location_tree.html" with locations=location.children_list group_id=group_id depth=depth|add:1 parent_id=location.id %}
19+
{% else %}
20+
<div class="form-check" style="margin-left: {{ depth }}rem;">
21+
<input class="form-check-input location-checkbox" type="checkbox" name="location" value="{{ location.id }}"
22+
id="location-{{ location.id }}"
23+
data-group-id="{{ group_id }}"
24+
{% if parent_id %}data-parent-id="{{ parent_id }}"{% endif %}
25+
{% if location.id|stringformat:"s" in current_filters.locations %}checked{% endif %}
26+
onchange="updateFilters(); updateLocationHierarchyState({{ location.id }}, {{ group_id }})">
27+
<label class="form-check-label small" for="location-{{ location.id }}">
28+
{{ location.name }}
29+
{% if location.asset_count > 0 %}
30+
<span class="text-muted">({{ location.asset_count }})</span>
31+
{% endif %}
32+
</label>
33+
</div>
34+
{% endif %}
35+
{% endfor %}

0 commit comments

Comments
 (0)