mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-30 20:55:42 +00:00 
			
		
		
		
	Data migration for PurchaseOrder and SalesOrder models
This commit is contained in:
		
							
								
								
									
										80
									
								
								InvenTree/order/migrations/0052_auto_20211014_0631.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								InvenTree/order/migrations/0052_auto_20211014_0631.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,80 @@ | ||||
| # Generated by Django 3.2.5 on 2021-10-14 06:31 | ||||
|  | ||||
| import re | ||||
|  | ||||
| from django.db import migrations | ||||
|  | ||||
| def build_refs(apps, schema_editor): | ||||
|     """ | ||||
|     Rebuild the integer "reference fields" for existing Build objects | ||||
|     """ | ||||
|  | ||||
|     print("\n - Rebuilding reference field for PurchaseOrder model...") | ||||
|  | ||||
|     PurchaseOrder = apps.get_model('order', 'purchaseorder') | ||||
|  | ||||
|     n = PurchaseOrder.objects.count() | ||||
|  | ||||
|     for order in PurchaseOrder.objects.all(): | ||||
|  | ||||
|         ref = 0 | ||||
|  | ||||
|         result = re.match(r"^(\d+)", order.reference) | ||||
|  | ||||
|         if result and len(result.groups()) == 1: | ||||
|             try: | ||||
|                 ref = int(result.groups()[0]) | ||||
|             except: | ||||
|                 ref = 0 | ||||
|  | ||||
|         order.reference_int = ref | ||||
|         order.save() | ||||
|  | ||||
|     print(f" - Updated {n} PurchaseOrder objects") | ||||
|  | ||||
|     print("\n - Rebuilding reference field for SalesOrder model...") | ||||
|  | ||||
|     SalesOrder = apps.get_model('order', 'salesorder') | ||||
|  | ||||
|     n = SalesOrder.objects.count() | ||||
|  | ||||
|     for order in SalesOrder.objects.all(): | ||||
|  | ||||
|         ref = 0 | ||||
|  | ||||
|         result = re.match(r"^(\d+)", order.reference) | ||||
|  | ||||
|         if result and len(result.groups()) == 1: | ||||
|             try: | ||||
|                 ref = int(result.groups()[0]) | ||||
|             except: | ||||
|                 ref = 0 | ||||
|  | ||||
|         order.reference_int = ref | ||||
|         order.save() | ||||
|  | ||||
|     print(f" - Updated {n} SalesOrder objects") | ||||
|  | ||||
|     print(f" - COMPLETE! -") | ||||
|  | ||||
|  | ||||
| def unbuild_refs(apps, schema_editor): | ||||
|     """ | ||||
|     Provided only for reverse migration compatibility | ||||
|     """ | ||||
|     pass | ||||
|  | ||||
|  | ||||
| class Migration(migrations.Migration): | ||||
|  | ||||
|     dependencies = [ | ||||
|         ('order', '0051_auto_20211014_0623'), | ||||
|     ] | ||||
|  | ||||
|  | ||||
|     operations = [ | ||||
|         migrations.RunPython( | ||||
|             build_refs, | ||||
|             reverse_code=unbuild_refs | ||||
|         ) | ||||
|     ] | ||||
		Reference in New Issue
	
	Block a user