71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
import logging
|
|
import json
|
|
|
|
import requests
|
|
|
|
from django.core.management.base import BaseCommand
|
|
from django.contrib.gis.geos import GEOSGeometry, MultiPolygon
|
|
|
|
from faker import Faker
|
|
|
|
from companies.factories import CompanyFactory
|
|
from products.factories import ProductFactory
|
|
|
|
|
|
logging.basicConfig(
|
|
filename='logs/addtestdata.log',
|
|
filemode='w',
|
|
format='%(levelname)s:%(message)s',
|
|
level=logging.INFO,
|
|
)
|
|
|
|
class Command(BaseCommand):
|
|
|
|
logo_url = "https://picsum.photos/200/300"
|
|
help = 'Creates fake companies and related products in database'
|
|
|
|
def handle(self, *args, **kwargs):
|
|
print("Creating fake data to populate database")
|
|
|
|
|
|
# start faker
|
|
fake = Faker()
|
|
Faker.seed(0)
|
|
|
|
# companies created
|
|
new_companies = []
|
|
logging.info("Creating fake companies")
|
|
print("Creating fake companies")
|
|
for i in range(10):
|
|
name = f"{fake.company()} {fake.company_suffix()}"
|
|
company = CompanyFactory(company_name=name)
|
|
new_companies.append(company)
|
|
logging.debug(f"New Company {company.company_name} created")
|
|
print(".", end = '')
|
|
print('')
|
|
|
|
logging.info("Creating fake products")
|
|
print("Creating fake products")
|
|
# create and assign products to companies
|
|
for company in new_companies:
|
|
logging.info(f"Products for {company.company_name}")
|
|
for i in range(100):
|
|
name = fake.last_name_nonbinary()
|
|
description = fake.paragraph(nb_sentences=5)
|
|
# TODO: apply tags from tag list
|
|
|
|
"""
|
|
# TODO: add dynamic image to product
|
|
response = requests.get(self.logo_url)
|
|
if response.status_code == 200:
|
|
response.raw.decode_content = True
|
|
image = response.raw
|
|
else:
|
|
image= None
|
|
"""
|
|
product = ProductFactory(name=name, description=description,)
|
|
logging.debug(f"New Product {product.name} created")
|
|
print(".", end = '')
|
|
print('')
|
|
|
|
print("Dataset creation finished") |