vbv/server/vbv_lernwelt/media_files/create_default_images.py

64 lines
1.8 KiB
Python

import os
from django.conf import settings
from django.core.files import File
from vbv_lernwelt.media_files.models import ContentImage, UserImage
def delete_default_images():
"""deletes all images"""
if "prod" in settings.APP_ENVIRONMENT:
raise Exception("This command must not be used in production environment")
ContentImage.objects.all().delete()
UserImage.objects.all().delete()
def create_default_images():
create_default_content_images()
create_default_user_images()
def create_default_content_images():
path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "./tests/test_images/"
)
images = [
("bike_accident.jpg", "Bike Accident"),
("car_accident.jpg", "Car Accident"),
]
for filename, title in images:
file_path = os.path.join(path, filename)
with open(file_path, "rb") as f:
image, _ = ContentImage.objects.get_or_create(
title=title,
file=File(f, name=filename),
focal_point_x=600,
focal_point_y=600,
focal_point_width=300,
focal_point_height=300,
)
image.tags.set(("Fahrzeug", "Unfall", "Vermittler"))
image.save()
def create_default_user_images():
path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "./tests/test_images/"
)
filename, title = ("user1_profile.jpg", "User1 Profile")
file_path = os.path.join(path, filename)
with open(file_path, "rb") as f:
image, _ = UserImage.objects.get_or_create(
title=title,
file=File(f, name=filename),
focal_point_x=600,
focal_point_y=600,
focal_point_width=300,
focal_point_height=300,
)
image.save()