wedding-site-backend/wedding_site/primary/utils.py

44 lines
1 KiB
Python
Raw Normal View History

2024-02-17 18:34:27 +00:00
import argparse
2024-02-18 09:57:34 +00:00
from pathlib import Path
import logging
2024-02-17 18:34:27 +00:00
2024-02-18 09:57:34 +00:00
import django
from django.core.files import File
django.setup()
from primary.models import Image
def upload_image(path: Path):
logging.info(f"Uploading: {path}")
filename = path.stem
img = Image(name=filename, alt_text=filename)
with path.open(mode='rb') as f:
img.content = File(f, name=path.name)
img.save()
def upload_images(path: Path):
print("checking: " + str(path))
for dir_entry in path.iterdir():
print(dir_entry)
if dir_entry.is_file():
upload_image(dir_entry)
2024-02-17 18:34:27 +00:00
if __name__ == "__main__":
2024-02-18 09:57:34 +00:00
print("entering")
2024-02-17 18:34:27 +00:00
parser = argparse.ArgumentParser(description='Django utils for wedding site.')
parser.add_argument('action', default = "upload_images",
help='Action to perform')
parser.add_argument('--source', help='Path to working items')
args = parser.parse_args()
2024-02-18 09:57:34 +00:00
if args.action == "upload_images":
upload_images(Path(args.source))