-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.py
More file actions
41 lines (30 loc) 路 1.16 KB
/
Copy pathcommands.py
File metadata and controls
41 lines (30 loc) 路 1.16 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
import os
import django
from pathlib import Path
from django.core.management import call_command
from django.conf import settings
import shutil
# Set up Django settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'E_Shop_config.settings')
django.setup()
def copy_images():
# Define source and target directories
fixtures_dir = Path('My_fixtures/photos')
target_dir = Path(settings.MEDIA_ROOT) / 'photos'
# Create target directory if it doesn't exist
target_dir.mkdir(parents=True, exist_ok=True)
# Iterate over files in the source directory
for image_path in fixtures_dir.iterdir():
if image_path.is_file():
# Construct the target path
target_path = target_dir / image_path.name
# Copy the file to the target path
shutil.copy(str(image_path), str(target_path))
def load_fixture(fixture_name):
# Load fixture data using Django management command
call_command('loaddata', f'My_fixtures/{fixture_name}.json')
# Copy images from fixtures to the media directory
copy_images()
# Load fixture data for products and users
load_fixture('my_products_data')
load_fixture('my_users_data')