Skip to content

Commit

Permalink
Feature: lint python (#574)
Browse files Browse the repository at this point in the history
* install Black linter

* lint entire backend with Black

* install flake8 formatter

* configure flake8

* install isort

* run isort to sort imports

* fixed name pattern

* added /api/healthcheck endpoint

* install Black linter

* lint entire backend with Black

* install flake8 formatter

* configure flake8

* install isort

* run isort to sort imports

* install pre-commit

* comment out admin and tests to pass flake8 linter rules

* init pre-commit

* remove trailing spaces

* configure flake8 settings in pre-commit

* delete obsolete poetry lock file

* configure isort settings

* remove precommit hook

---------

Co-authored-by: Kevin <[email protected]>
Co-authored-by: Kevin Yu <[email protected]>
  • Loading branch information
3 people authored Sep 26, 2024
1 parent ad7903e commit e3ec821
Show file tree
Hide file tree
Showing 25 changed files with 570 additions and 287 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dev.env
stage.env
.env
__pycache__
.pytest_cache
.vscode/
*venv/
app/frontend/static/frontend/*
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ migrations:
docker compose exec django python manage.py makemigrations

migrate:
docker compose exec django python manage.py migrate
docker compose exec django python manage.py migrate

db-shell:
docker compose exec django python manage.py shell
Expand Down
10 changes: 10 additions & 0 deletions backend/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[flake8]
extend-ignore = E203, W503
exclude =
migrations
__pycache__
manage.py
settings.py
env
.pytest_cache
.vscode
3 changes: 3 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
.idea
DCIM
data

__pycache__
.pytest_cache
41 changes: 41 additions & 0 deletions backend/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
default_language_version:
# default language version for each language used in the repository
python: python3.11
repos:
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
args:
- "--profile=black"
- "--thirdparty=django"
exclude: |
(?x)^(
.*/(migrations)/(.)*|
.*/manage\.py$|
.*/settings\.py$
)$
language: python
files: ^backend/.*\.py$

- repo: https://github.com/psf/black
rev: 24.8.0
hooks:
- id: black
language: python
files: ^backend/.*\.py$

- repo: https://github.com/PyCQA/flake8
rev: 7.1.1
hooks:
- id: flake8
args:
- "--extend-ignore=E203,W503" # Ignore conflicts with Black
exclude: |
(?x)^(
.*/(migrations)/(.)*|
.*/manage\.py$|
.*/settings\.py$
)$
language: python
files: ^backend/.*\.py$
2 changes: 1 addition & 1 deletion backend/backend/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', 'backend.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings")

application = get_asgi_application()
112 changes: 59 additions & 53 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
"""

from pathlib import Path

from decouple import config

VERSION = '1.0.0'
VERSION = "1.0.0"

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config('SECRET_KEY')
SECRET_KEY = config("SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config('DEBUG', default=False, cast=bool)
DEBUG = config("DEBUG", default=False, cast=bool)

ALLOWED_HOSTS = config("DJANGO_ALLOWED_HOSTS", default="localhost").split(" ")

# Application definition

INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ctj_api.apps.CtjApiConfig',
'rest_framework',
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"ctj_api.apps.CtjApiConfig",
"rest_framework",
"django_vite",
]

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 = 'backend.urls'
ROOT_URLCONF = "backend.urls"

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'frontend/templates/frontend'],
'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": [BASE_DIR / "frontend/templates/frontend"],
"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 = 'backend.wsgi.application'
WSGI_APPLICATION = "backend.wsgi.application"


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

DATABASES = {
'default': {
'ENGINE': config('SQL_ENGINE'),
'NAME': config('SQL_DATABASE'),
'USER': config('SQL_USER'),
'PASSWORD': config('SQL_PASSWORD'),
'HOST': config('SQL_HOST'),
'PORT': config('SQL_PORT'),
"default": {
"ENGINE": config("SQL_ENGINE"),
"NAME": config("SQL_DATABASE"),
"USER": config("SQL_USER"),
"PASSWORD": config("SQL_PASSWORD"),
"HOST": config("SQL_HOST"),
"PORT": config("SQL_PORT"),
}
}

Expand All @@ -93,26 +94,26 @@

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.0/topics/i18n/

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

TIME_ZONE = 'UTC'
TIME_ZONE = "UTC"

USE_I18N = True

Expand All @@ -122,27 +123,32 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

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

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

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"


# django-vite settings
# https://github.com/MrBin99/django-vite
DJANGO_VITE = {
"default": {
# enable vite HMR in dev mode
"dev_mode": config("DEBUG", default=False, cast=bool),
"dev_server_port": 5175,
# resolve static asset paths in production
"manifest_path": Path(BASE_DIR / "frontend" / "static" / "vite_assets_dist" / ".vite" / "manifest.json").resolve()
}
"default": {
# enable vite HMR in dev mode
"dev_mode": config("DEBUG", default=False, cast=bool),
"dev_server_port": 5175,
# resolve static asset paths in production
"manifest_path": Path(
BASE_DIR
/ "frontend"
/ "static"
/ "vite_assets_dist"
/ ".vite"
/ "manifest.json"
).resolve(),
}
}
# Add the build.outDir from vite.config.js to STATICFILES_DIRS
# so that collectstatic can collect your compiled vite assets.
STATICFILES_DIRS = [
BASE_DIR / 'frontend/static/vite_assets_dist'
]
STATICFILES_DIRS = [BASE_DIR / "frontend/static/vite_assets_dist"]
29 changes: 17 additions & 12 deletions backend/backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,33 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
# from django.contrib import admin
from django.urls import path, re_path, include

from django.http import JsonResponse

# from django.contrib import admin
from django.urls import include, path, re_path
from django.views.generic import TemplateView


# Custom handler for incorrect API routes
def api_not_found(request, exception=None):
return JsonResponse({
'error': 'API endpoint not found',
'status_code': 404,
'message': 'The requested API endpoint does not exist'
}, status=404)
return JsonResponse(
{
"error": "API endpoint not found",
"status_code": 404,
"message": "The requested API endpoint does not exist",
},
status=404,
)


urlpatterns = [
path('api/', include('ctj_api.urls'), name="api"),
# Custom error handler for invalid API routes
re_path(r'^api/.*$', api_not_found), # Catch-all for incorrect API routes

path("api/", include("ctj_api.urls"), name="api"),
# Catch-all for incorrect API routes
re_path(r"^api/.*$", api_not_found),
# Catch-all for frontend (React)
re_path(
r'^.*$',
r"^.*$",
TemplateView.as_view(template_name="index.html"),
name="index",
),
Expand Down
2 changes: 1 addition & 1 deletion backend/backend/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', 'backend.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings")

application = get_wsgi_application()
2 changes: 1 addition & 1 deletion backend/ctj_api/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 backend/ctj_api/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@


class CtjApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'ctj_api'
default_auto_field = "django.db.models.BigAutoField"
name = "ctj_api"
Loading

0 comments on commit e3ec821

Please sign in to comment.