Skip to content

Commit d3ed7d8

Browse files
committed
Add images to mock data script
1 parent e81399f commit d3ed7d8

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ backend/target
22
backend/database.db
33
backend/.env
44
backend/screen-frontend-build
5+
backend/slide_images/*
56
screen-frontend/node_modules
67
screen-frontend/build
78
# admin-frontend has its own .gitignore

Diff for: backend/.env

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
# Paths relative to /backend
12
DATABASE_URL=database.db
3+
IMAGE_PATH=slide_images

Diff for: backend/scripts/mock_data.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,40 @@
22
import uuid
33
import random
44
from datetime import datetime, timedelta
5+
import requests
6+
from dotenv import load_dotenv
7+
import sys
8+
import os
9+
10+
load_dotenv()
511

612
def create_mock_slides(db_path: str, count: int):
713
conn = sqlite3.connect(db_path)
814
cursor = conn.cursor()
915

10-
filetypes = ["jpg", "png", "gif", "mp4"]
1116
now = datetime.utcnow()
1217

1318
slides = []
1419
for i in range(count):
1520
slide_id = str(uuid.uuid4())
21+
22+
try:
23+
image_path = f"../{os.environ['IMAGE_PATH']}/{slide_id}.jpg"
24+
except KeyError:
25+
sys.exit("Error: IMAGE_DIR environment variable is not set.")
26+
27+
# Get random image from picsum.photos
28+
image = requests.get(f"https://picsum.photos/1920/1080").content
29+
30+
with open(image_path, "wb") as image_file:
31+
image_file.write(image)
32+
1633
caption = f"Slide nr. {i}!"
1734
start_date = now + timedelta(days=random.randint(-10, 10))
1835
end_date = start_date + timedelta(days=random.randint(1, 10))
1936
active = random.choice([True, False])
20-
filetype = random.choice(filetypes)
2137

22-
slides.append((slide_id, caption, start_date, end_date, active, filetype))
38+
slides.append((slide_id, caption, start_date, end_date, active, "jpg"))
2339

2440
cursor.executemany("""
2541
INSERT INTO slides (id, caption, start_date, end_date, active, filetype)

0 commit comments

Comments
 (0)