-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_consumable_categories.py
More file actions
81 lines (75 loc) · 3.31 KB
/
Copy pathfix_consumable_categories.py
File metadata and controls
81 lines (75 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
Move consumable items from old generic categories to correct sub-categories.
Run: python fix_consumable_categories.py
"""
import os, django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
django.setup()
from apps.consumables.models import ConsumableCategory, Consumable
print("Fixing consumable item → sub-category assignments...\n")
# Map: consumable name keywords → correct sub-category name
item_to_subcategory = {
# HP Toners
"HP 85A": "HP 85A Black Toner (CE285A)",
"HP 12A": "HP 12A Black Toner (Q2612A)",
"HP 88A": "HP 88A Black Toner (CC388A)",
"HP Drum": "HP CF219A Drum Unit",
# Canon Toners
"Canon 328": "Canon 328 Black Toner (CRG-328)",
"Canon PG-745 Black Ink": "Canon PG-745 Black Ink Cartridge",
"Canon CL-746 Colour": "Canon CL-746 Colour Ink Cartridge",
# Paper
"A4 Paper Ream": "A4 Paper Ream (500 sheets, 75 GSM)",
"A4 Paper Box": "A4 Paper Box (5 reams)",
"Ballpoint Pen (Blue)": "Ballpoint Pen Blue (Box of 10)",
"Ballpoint Pen (Black)": "Ballpoint Pen Black (Box of 10)",
"Sticky Notes": "Sticky Notes 76x76mm (Pack of 5)",
"Stapler Pins": "Stapler Pins Box of 1000",
# Cables
"USB-A to USB-B": "USB-A to USB-B Cable 1.5m (Printer Cable)",
"USB-A to USB-C": "USB-A to USB-C Cable 1m",
"HDMI Cable": "HDMI Cable 1.5m",
"LAN Cable": "LAN Cable Cat6 3m",
"VGA Cable": "VGA Cable 1.8m",
"Power Extension": "Power Extension Board 4 Socket",
# Storage
"USB Flash Drive 16": "USB Flash Drive 16GB",
"USB Flash Drive 32": "USB Flash Drive 32GB",
"DVD-R": "DVD-R Blank Disc (Pack of 10)",
# Cleaning
"Screen Cleaning": "Screen Cleaning Kit (Spray + Cloth)",
"Compressed Air": "Compressed Air Can 400ml",
"Microfiber": "Microfiber Cleaning Cloth",
"Keyboard Cleaning": "Keyboard Cleaning Brush",
# Batteries
"AA Batteries": "AA Batteries (Pack of 4)",
"AAA Batteries": "AAA Batteries (Pack of 4)",
"UPS Battery": "UPS Battery 12V 7.2Ah",
}
fixed = 0
for item in Consumable.objects.select_related('category', 'sub_category').all():
# Find the right sub-category for this item
matched_sub = None
for keyword, sub_name in item_to_subcategory.items():
if keyword.lower() in item.name.lower():
sub = ConsumableCategory.objects.filter(name=sub_name).first()
if sub:
matched_sub = sub
break
if matched_sub and item.sub_category != matched_sub:
old_sub = item.sub_category.name if item.sub_category else 'None'
item.sub_category = matched_sub
item.category = matched_sub.parent # also set the parent category
item.save()
print(f" ✓ {item.name}")
print(f" Sub: {old_sub} → {matched_sub.name}")
fixed += 1
elif not matched_sub:
print(f" ⚠️ No match found for: {item.name} (category: {item.category})")
print(f"\n✅ Fixed {fixed} items.")
print("\nFinal check:")
for item in Consumable.objects.select_related('category', 'sub_category').all().order_by('category__name', 'name'):
cat = item.category.name if item.category else '—'
sub = item.sub_category.name if item.sub_category else '—'
print(f" {item.name}")
print(f" Category: {cat} | Sub: {sub}")