Skip to content

Commit

Permalink
Configure linting tools
Browse files Browse the repository at this point in the history
  • Loading branch information
mp-octo committed Dec 6, 2024
1 parent 8c09248 commit d291240
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 65 deletions.
10 changes: 10 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[flake8]
ignore = D203, W503
exclude =
.git,
__pycache__,
*/migrations/*,
max-complexity = 10
ban-relative-imports = true
per-file-ignores =
*/__init__.py: F401,E501
15 changes: 8 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ usage:
@echo "build....................Builds the django docker image"
@echo "chown....................Change ownership of files to own user"
@echo "clear_pyc................Remove all pyc files"
@echo "down.....................Stop the Django server"
@echo "help.....................Display available commands"
@echo "isort....................Sort Python imports"
@echo "isort_check..............Checks Python import are sorted correctly without making changes"
Expand All @@ -44,9 +45,6 @@ usage:
@echo "up.......................Start the Django server"
@echo "usage....................Display available commands"

build:
COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose build django

black:
@docker-compose run --rm django black src ${ARGS}
ifneq ($(OS),Darwin)
Expand All @@ -56,6 +54,9 @@ endif
black_check:
$(MAKE) black ARGS="--check"

build:
COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose build django

chown:
@docker-compose run --rm django chown -R "`id -u`:`id -u`" "/usr/src/app/${ARGS}"

Expand Down Expand Up @@ -98,11 +99,11 @@ PG_DB_NAME=postgres
PG_DB_USER=postgres
PG_DB_PASSWORD=postgres

db:
@docker-compose run --rm -e PGPASSWORD=$(PG_DB_PASSWORD) db psql -h $(PG_DB_HOST) -p $(PG_DB_PORT) -U $(PG_DB_USER) -d $(PG_DB_NAME) $(ARGS)

prep:
@docker-compose run --no-deps --rm django /bin/sh -c "isort src && black src && mypy --cache-dir=/dev/null src && flake8 src"
@docker-compose run --no-deps --rm django /bin/sh -c "isort src && black src && flake8 src && mypy --cache-dir=/dev/null src"

psql:
@docker-compose run --rm -e PGPASSWORD=$(PG_DB_PASSWORD) db psql -h $(PG_DB_HOST) -p $(PG_DB_PORT) -U $(PG_DB_USER) -d $(PG_DB_NAME) $(ARGS)

shell:
$(MAKE) manage ARGS="shell ${ARGS}"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Additional arguments may be provided to some commands by providing the `ARGS=""`
### How do I run the project locally?
* Run `make build` to build the container(s) needed.
* Run `make up` to start the Django application and any dependent services defined in the compose file.
* Run `make manage ARGS="migrate"` to prepare the database (this runs Django's `python manage.py ${ARGS}` command inside the container).
* Run `make manage ARGS="migrate"` to prepare the database (this runs Django's `python manage.py ${ARGS}` command inside the container). This can be run in another shell window if you didn't run `make up` with the `ARGS="--detach"` flag.
* You'll need a user to log into the admin console. To create an admin user for the project run `make manage ARGS="createsuperuser"`.
* You should now be able to access the Django admin console from `http://localhost:8000/admin`, and a "Hello, world" page at `http://localhost:8000/task`

Expand All @@ -40,6 +40,6 @@ Additional arguments may be provided to some commands by providing the `ARGS=""`
- `make prep` - run code formatting and linting tools: `black`, `isort`, `mypy`, `lint`. These can also be run individually with the corresponding make command.

### Examples
- Instead of `make test ARGS="path/to/test.py"`
- Instead of running `make test ARGS="-k test_specific_test_case"`
- Instead of `make manage ARGS="createsuperuser`
- Rebuild and re-run the application in detached mode: `make up ARGS="--build --detach"`
- Running a specific test file: `make test ARGS="path/to/test.py"`
- Running tests matching a keyword: `make test ARGS="-k test_specific_test_case"`
9 changes: 9 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[mypy]
plugins = mypy_django_plugin.main
ignore_missing_imports = True

[mypy-*.migrations.*]
ignore_errors = True

[mypy.plugins.django-stubs]
django_settings_module = "src.octotest.settings"
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[tool.black]
line-length = 79
skip-numeric-underscore-normalization = 1

[tool.isort]
multi_line_output = 3
include_trailing_comma = "True"
force_grid_wrap = 0
use_parentheses = "True"
line_length = 79
skip_glob="**/migrations/**"
10 changes: 6 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
asgiref==3.8.1
black==22.3.0
Django==5.1.3
psycopg2-binary>=2.8
sqlparse==0.5.2
typing_extensions==4.12.2
django-stubs==1.8.0
django-stubs-ext==0.3.1
flake8==7.0.0
isort==5.6.4
mypy==1.10.0
mypy-extensions==1.0.0
psycopg2-binary>=2.8
pytest>=7.2.1
pytest-django>=4.5.2
pytest-django>=4.5.2
sqlparse==0.5.2
typing_extensions==4.12.2
2 changes: 1 addition & 1 deletion src/octotest/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'src.octotest.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.octotest.settings")

application = get_asgi_application()
96 changes: 55 additions & 41 deletions src/octotest/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import os
from pathlib import Path
from typing import Iterable

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -21,67 +22,69 @@
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-8c#%3-*$+ifulrb*$786w@xb$!vg2*db98%g2d+(kh!h-1su(('
SECRET_KEY = (
"django-insecure-8c#%3-*$+ifulrb*$786w@xb$!vg2*db98%g2d+(kh!h-1su(("
)

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS: Iterable[str] = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = 'src.octotest.urls'
ROOT_URLCONF = "src.octotest.urls"

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]

WSGI_APPLICATION = 'src.octotest.wsgi.application'
WSGI_APPLICATION = "src.octotest.wsgi.application"


# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('POSTGRES_NAME'),
'USER': os.environ.get('POSTGRES_USER'),
'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
'HOST': 'db',
'PORT': 5432,
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.environ.get("POSTGRES_NAME"),
"USER": os.environ.get("POSTGRES_USER"),
"PASSWORD": os.environ.get("POSTGRES_PASSWORD"),
"HOST": "db",
"PORT": 5432,
}
}

Expand All @@ -91,26 +94,37 @@

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
"NAME": (
"django.contrib.auth.password_validation."
"UserAttributeSimilarityValidator"
),
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
"NAME": (
"django.contrib.auth.password_validation." "MinimumLengthValidator"
),
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
"NAME": (
"django.contrib.auth.password_validation."
"CommonPasswordValidator"
),
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
"NAME": (
"django.contrib.auth.password_validation."
"NumericPasswordValidator"
),
},
]


# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/

LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = "en-us"

TIME_ZONE = 'UTC'
TIME_ZONE = "UTC"

USE_I18N = True

Expand All @@ -120,9 +134,9 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = 'static/'
STATIC_URL = "static/"

# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
2 changes: 1 addition & 1 deletion src/octotest/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@

urlpatterns = [
path("task/", include("src.task.urls")),
path('admin/', admin.site.urls),
path("admin/", admin.site.urls),
]
2 changes: 1 addition & 1 deletion src/octotest/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'src.octotest.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.octotest.settings")

application = get_wsgi_application()
2 changes: 1 addition & 1 deletion src/task/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from django.contrib import admin
# from django.contrib import admin

# Register your models here.
4 changes: 2 additions & 2 deletions src/task/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@


class TaskConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'src.task'
default_auto_field = "django.db.models.BigAutoField"
name = "src.task"
2 changes: 1 addition & 1 deletion src/task/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from django.db import models
# from django.db import models

# Create your models here.
2 changes: 1 addition & 1 deletion src/task/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from django.test import TestCase
# import pytest

# Create your tests here.
2 changes: 1 addition & 1 deletion src/task/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

urlpatterns = [
path("", views.index, name="index"),
]
]

0 comments on commit d291240

Please sign in to comment.