Skip to content

Commit

Permalink
initial commit ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
theognis1002 committed Sep 6, 2021
0 parents commit 3739658
Show file tree
Hide file tree
Showing 21 changed files with 661 additions and 0 deletions.
131 changes: 131 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
*.sqlite3

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Django Playground

- Test sandbox to practice and experiment with Django ORM queries

### Setup

1. `virtualenv venv && source venv/bin/activate`
1. `pip install -r requirements.txt`
1. `python manage.py migrate`
1. `python manage.py seed_data`
- custom command to load dummy data
1. `python manage.py runserver`
Empty file added apps/__init__.py
Empty file.
Empty file added apps/products/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions apps/products/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions apps/products/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ProductsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "products"
93 changes: 93 additions & 0 deletions apps/products/management/commands/seed_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import random

from django.core.management.base import BaseCommand, CommandError
from faker import Faker
from products.models import Customer, LineItem, Order, Product

fake = Faker()


def add_customers():
for _ in range(100):
customer = Customer(
first_name=fake.first_name(),
last_name=fake.last_name(),
address=fake.street_address(),
city=fake.city(),
postcode=fake.postcode(),
email=fake.email(),
)
print(f"New customer: {customer}")
customer.save()


def add_orders():
customers = Customer.objects.all()

for _ in range(5000):
# choose a random customer
customer = random.choice(customers)

ordered_date = fake.date_time_this_year()
shipped_date = random.choices(
[None, fake.date_time_between(start_date=ordered_date)], [10, 90]
)[0]

# choose either random None or random date for delivered and shipped
delivered_date = None
if shipped_date:
delivered_date = random.choices(
[None, fake.date_time_between(start_date=shipped_date)], [50, 50]
)[0]

# choose either random None or one of three coupon codes
coupon_code = random.choices(
[None, "50OFF", "FREESHIPPING", "BUYONEGETONE"], [80, 10, 5, 5]
)[0]

order = Order(
customer_id=customer.id,
order_date=ordered_date,
shipped_date=shipped_date,
delivered_date=delivered_date,
coupon_code=coupon_code,
)
print(f"New order: {order}")
order.save()


def add_products():
for _ in range(10):
product = Product(name=fake.catch_phrase(), price=random.randint(10, 100))
print(f"New product: {product}")
product.save()


def add_order_products():
orders = Order.objects.all()
products = Product.objects.all()

for order in orders:
# select random k
k = random.randint(1, 3)
# select random products
purchased_products = random.sample(list(products), k)
# order.products.add(*purchased_products)
for product in purchased_products:
line_item = LineItem(
order=order, product=product, quantity=random.randint(1, 5)
)
print(f"New line item: {line_item}")
line_item.save()


def create_random_data():
add_customers()
add_orders()
add_products()
add_order_products()


class Command(BaseCommand):
def handle(self, *args, **options):
create_random_data()
110 changes: 110 additions & 0 deletions apps/products/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Generated by Django 3.2.7 on 2021-09-06 01:51

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Customer",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("first_name", models.CharField(max_length=50)),
("last_name", models.CharField(max_length=50)),
("address", models.CharField(max_length=500)),
("city", models.CharField(max_length=50)),
("postcode", models.CharField(max_length=50)),
("email", models.CharField(max_length=50, unique=True)),
],
),
migrations.CreateModel(
name="LineItem",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("quantity", models.IntegerField()),
],
),
migrations.CreateModel(
name="Product",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=50, unique=True)),
("price", models.IntegerField()),
],
),
migrations.CreateModel(
name="Order",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("order_date", models.DateField()),
("shipped_date", models.DateField(null=True)),
("delivered_date", models.DateField(null=True)),
("coupon_code", models.CharField(max_length=50, null=True)),
(
"customer",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="products.customer",
),
),
(
"products",
models.ManyToManyField(
through="products.LineItem", to="products.Product"
),
),
],
),
migrations.AddField(
model_name="lineitem",
name="order",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="products.order"
),
),
migrations.AddField(
model_name="lineitem",
name="product",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="products.product"
),
),
]
Empty file.
Loading

0 comments on commit 3739658

Please sign in to comment.