Skip to content

Commit

Permalink
Add pre-commit config + linting + update README
Browse files Browse the repository at this point in the history
  • Loading branch information
theognis1002 committed Jan 30, 2023
1 parent 089c432 commit 4570433
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 22 deletions.
50 changes: 50 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
exclude: "docs|migrations|.git"
default_stages: [commit]
fail_fast: true

repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
exclude: ^.*\b(migrations)\b.*$
args: ["--target-version", "py310"]

- repo: https://github.com/asottile/pyupgrade
rev: v3.1.0
hooks:
- id: pyupgrade

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-merge-conflict
- id: debug-statements
- id: mixed-line-ending
- id: requirements-txt-fixer

- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort

- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
args: ["--ignore=E501,W503,F403,F405,E203"]
additional_dependencies: [flake8-isort]

- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade

# sets up .pre-commit-ci.yaml to ensure pre-commit dependencies stay up to date
ci:
autoupdate_schedule: weekly
skip: []
submodules: false
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
## Django Playground

- Test sandbox to practice and experiment with Django ORM queries
- Test sandbox to practice and experiment with Django ORM queries, third party packages, and miscellaneous items

### Setup (OSX/Linux)

1. `chmod +x setup.sh`
1. `./setup.sh`

### Testing

1. `pytest`
20 changes: 6 additions & 14 deletions apps/products/management/commands/seed_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import random

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

Expand All @@ -24,26 +24,20 @@ def add_customers():
def add_orders():
customers = Customer.objects.all()

for _ in range(5000):
for _ in range(500):
# 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]
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]
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]
coupon_code = random.choices([None, "50OFF", "FREESHIPPING", "BUYONEGETONE"], [80, 10, 5, 5])[0]

order = Order(
customer_id=customer.id,
Expand Down Expand Up @@ -74,9 +68,7 @@ def add_order_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)
)
line_item = LineItem(order=order, product=product, quantity=random.randint(1, 5))
print(f"New line item: {line_item}")
line_item.save()

Expand Down
8 changes: 2 additions & 6 deletions apps/products/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ def test_does_not_make_unnecessary_queries(client, django_assert_max_num_queries


class TestPerfViews:
def test_does_not_make_unnecessary_queries(
self, client, django_assert_max_num_queries
):
def test_does_not_make_unnecessary_queries(self, client, django_assert_max_num_queries):
with django_assert_max_num_queries(2):
client.get(reverse("users-list"))

Expand Down Expand Up @@ -77,9 +75,7 @@ def test_post_request_users_view(self, api_client):
class TestCasePytest(TestCase):
def setUp(self):
self.instance = 3
User.objects.create(
username="test", email="[email protected]", password="password"
)
User.objects.create(username="test", email="[email protected]", password="password")

def test_setup_works(self):
assert self.instance == 3
Expand Down
17 changes: 17 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[tool.black]
line-length = 150
exclude = '''
/(
| migrations
| venv
)/
'''

[tool.isort]
profile = "black"
atomic = true

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "django_playground.settings"
minversion = "6.0"
addopts = "--reuse-db"
10 changes: 9 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
asgiref==3.4.1
attrs==22.1.0
black==21.8b0
cfgv==3.3.1
click==8.0.1
distlib==0.3.6
Django==3.2.7
django-debug-toolbar==3.2.2
django-extensions==3.1.5
djangorestframework==3.13.1
Faker==8.12.1
filelock==3.9.0
graphviz==0.20
identify==2.5.16
iniconfig==1.1.1
isort==5.9.3
mypy-extensions==0.4.3
nodeenv==1.7.0
packaging==21.3
pathspec==0.9.0
platformdirs==2.3.0
platformdirs==2.6.2
pluggy==1.0.0
pre-commit==3.0.2
py==1.11.0
pyparsing==3.0.9
pytest==7.1.3
pytest-django==4.5.2
python-dateutil==2.8.2
pytz==2021.1
PyYAML==6.0
regex==2021.8.28
six==1.16.0
sqlparse==0.4.1
text-unidecode==1.3
tomli==1.2.1
typing-extensions==3.10.0.2
virtualenv==20.17.1
1 change: 1 addition & 0 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ source venv/bin/activate
pip install -r requirements.txt
python manage.py migrate
python manage.py createsuperuser --no-input --username admin --email [email protected]
python manage.py seed_data
python manage.py runserver

0 comments on commit 4570433

Please sign in to comment.