Add fake data

This commit is contained in:
Andros Fenollosa 2021-11-12 13:21:37 +01:00
parent fe3caee6f9
commit e43ed81638
4 changed files with 49 additions and 6 deletions

View File

@ -15,15 +15,19 @@ docker.recreate.django: ## Recreate Django image
run.loaddata: ## Minimal load data run.loaddata: ## Minimal load data
# Remove database # Remove database
docker-compose -f docker-compose.dev.yaml exec -T django bash -c "python3 manage.py flush --noinput" docker-compose -f docker-compose.yaml exec -T django bash -c "python3 manage.py flush --noinput"
# Remove media # Remove media
sudo rm -rf media sudo rm -rf media
# Migrate # Migrate
docker-compose -f docker-compose.dev.yaml exec -T django bash -c "python3 manage.py migrate" docker-compose -f docker-compose.yaml exec -T django bash -c "python3 manage.py migrate"
# Add provinces, towns and categories # Add categories
docker-compose -f docker-compose.dev.yaml exec -T db bash -c "psql -h localhost -d kualitte -U postgres -p 5432 -a -q -f /usr/src/app/scripts/provincias.sql" docker-compose -f docker-compose.yaml exec -T django bash -c "python3 manage.py runscript create_categories"
# Add profiles
docker-compose -f docker-compose.yaml exec -T django bash -c "python3 manage.py runscript create_profiles"
# Add talks
docker-compose -f docker-compose.yaml exec -T django bash -c "python3 manage.py runscript create_talks"
run.server: # Run server run.server: # Run server
docker-compose -f docker-compose.dev.yaml up docker-compose -f docker-compose.yaml up

1
TODO
View File

@ -1,3 +1,2 @@
- Add script with fake data.
- List talks. - List talks.
- Single talk. - Single talk.

View File

@ -0,0 +1,9 @@
from app.website.models import Category
from faker import Faker
def run():
fake = Faker()
# 5 categories
for word in [fake.unique.sentence(nb_words=1)[0:-1] for i in range(5)]:
Category(name=word).save()

31
scripts/create_talks.py Normal file
View File

@ -0,0 +1,31 @@
from app.website.models import Category, Talk, Profile
from faker import Faker
# Get random imagen from url
from django.core.files import File
import requests
import time
from tempfile import NamedTemporaryFile
from random import randint
def run():
fake = Faker()
# 100 talks
for title in [fake.unique.sentence(nb_words=10)[0:-1] for i in range(100)]:
my_talk = Talk(
title=title,
category=Category.objects.order_by("?")[0],
author=Profile.objects.order_by("?")[0],
is_draft=False,
content=fake.text(max_nb_chars=1000),
)
my_talk.save()
# Add a image
url_random_imagen = f"https://cdn.jsdelivr.net/gh/tanrax/place-image-random/images/{randint(1, 1000)}.jpg"
r = requests.get(url_random_imagen)
img_temp = NamedTemporaryFile(delete=True)
img_temp.write(r.content)
img_temp.flush()
my_talk.image.save(f"random_{int(time.time() * 1000)}.jpg", File(img_temp))