mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 07:05:41 +00:00 
			
		
		
		
	Adds option to delete existing data when importing records
This commit is contained in:
		
							
								
								
									
										10
									
								
								tasks.py
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								tasks.py
									
									
									
									
									
								
							@@ -6,6 +6,7 @@ import sys
 | 
				
			|||||||
import pathlib
 | 
					import pathlib
 | 
				
			||||||
import re
 | 
					import re
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
try:
 | 
					try:
 | 
				
			||||||
    from invoke import ctask as task
 | 
					    from invoke import ctask as task
 | 
				
			||||||
except:
 | 
					except:
 | 
				
			||||||
@@ -380,8 +381,8 @@ def export_records(c, filename='data.json'):
 | 
				
			|||||||
    print("Data export completed")
 | 
					    print("Data export completed")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@task(help={'filename': 'Input filename'}, post=[rebuild_models, rebuild_thumbnails])
 | 
					@task(help={'filename': 'Input filename', 'clear': 'Clear existing data before import'}, post=[rebuild_models, rebuild_thumbnails])
 | 
				
			||||||
def import_records(c, filename='data.json'):
 | 
					def import_records(c, filename='data.json', clear=False):
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    Import database records from a file
 | 
					    Import database records from a file
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
@@ -394,6 +395,9 @@ def import_records(c, filename='data.json'):
 | 
				
			|||||||
        print(f"Error: File '{filename}' does not exist")
 | 
					        print(f"Error: File '{filename}' does not exist")
 | 
				
			||||||
        sys.exit(1)
 | 
					        sys.exit(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if clear:
 | 
				
			||||||
 | 
					        delete_data(c, force=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    print(f"Importing database records from '{filename}'")
 | 
					    print(f"Importing database records from '{filename}'")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Pre-process the data, to remove any "permissions" specified for a user or group
 | 
					    # Pre-process the data, to remove any "permissions" specified for a user or group
 | 
				
			||||||
@@ -432,6 +436,8 @@ def delete_data(c, force=False):
 | 
				
			|||||||
    Warning: This will REALLY delete all records in the database!!
 | 
					    Warning: This will REALLY delete all records in the database!!
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    print(f"Deleting all data from InvenTree database...")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if force:
 | 
					    if force:
 | 
				
			||||||
        manage(c, 'flush --noinput')
 | 
					        manage(c, 'flush --noinput')
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user