Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enable black pre-commit hook #456

Merged
merged 24 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Lint and format code

on:
pull_request:
schedule:
# run once a week on early monday mornings
- cron: '22 2 * * 1'

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- uses: actions/setup-node@v4
with:
node-version: 18
- name: Run eslint + format
run: |
npm i
npm run lint
- name: Install dependencies
run: |
python -m pip install --upgrade pip wheel
pip install pre-commit==3.6.0
- name: Run pre-commit
run: pre-commit run --all-files
11 changes: 3 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Lint and Test code
name: Test and build code

on:
pull_request:
Expand All @@ -8,7 +8,7 @@ on:

jobs:
test:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
env:
DJANGO_SETTINGS_MODULE: config.settings.test
services:
Expand All @@ -28,7 +28,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.8'
python-version: '3.12'
cache: "pip"
cache-dependency-path: "requirements/**.txt"
- uses: actions/setup-node@v4
Expand All @@ -44,11 +44,6 @@ jobs:
restore-keys: |
${{ runner.os }}-docker-
${{ runner.os }}-
- name: Run eslint + format
run: |
npm i
npm run lint
npx prettier --check .
- name: Build Docker Deploy Image
uses: docker/build-push-action@v5
with:
Expand Down
25 changes: 16 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
repos:
- repo: https://github.com/python/black
rev: 22.3.0
hooks:
- id: black
language_version: python3
- repo: https://github.com/pre-commit/mirrors-prettier
rev: "" # Use the sha or tag you want to point at
hooks:
- id: prettier
- repo: https://github.com/adamchainz/django-upgrade
rev: "1.15.0"
hooks:
- id: django-upgrade
args: [--target-version, "4.2"]
- repo: https://github.com/python/black
rev: "23.12.1"
hooks:
- id: black
language_version: python3.12
args: [--config=./pyproject.toml]
- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v3.1.0"
hooks:
- id: prettier
args: [--config=./.prettierrc]
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dear_petition/
*.yaml
*.json
*.md

.direnv
lib
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ FROM dear_frontend as static_files

RUN npm run build

FROM python:3.8-slim-bullseye as base
FROM python:3.12-slim-bookworm as base

# Install packages needed to run your application (not build deps):
# mime-support -- for mime types when serving static files
Expand Down
7 changes: 4 additions & 3 deletions bin/get_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ def get_annotations(template):
annotations = pdf.pages[0]["/Annots"]
print("Here is a list of annotations in the document.\n")
for annotation in annotations:
annotation_name = annotation['/T']
annotation_name = annotation["/T"]
print(annotation_name)

if __name__ == '__main__':
sys.exit(get_annotations())

if __name__ == "__main__":
sys.exit(get_annotations())
21 changes: 10 additions & 11 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
# https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n
USE_I18N = True
# https://docs.djangoproject.com/en/dev/ref/settings/#use-l10n
USE_L10N = True
# https://docs.djangoproject.com/en/dev/ref/settings/#use-tz
USE_TZ = True
ENVIRONMENT = "BASE"
Expand All @@ -50,7 +49,7 @@
# https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"ENGINE": "django.db.backends.postgresql",
"NAME": "dear_petition",
"USER": "",
"PASSWORD": "",
Expand Down Expand Up @@ -149,9 +148,7 @@
]
# https://docs.djangoproject.com/en/dev/ref/settings/#auth-password-validators
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.CommonPasswordValidator"},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
Expand All @@ -168,6 +165,8 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
# Add the account middleware:
"allauth.account.middleware.AccountMiddleware",
]

# STATIC
Expand Down Expand Up @@ -245,9 +244,7 @@
# EMAIL
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
EMAIL_BACKEND = env(
"DJANGO_EMAIL_BACKEND", default="django.core.mail.backends.smtp.EmailBackend"
)
EMAIL_BACKEND = env("DJANGO_EMAIL_BACKEND", default="django.core.mail.backends.smtp.EmailBackend")

# ADMIN
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -315,8 +312,10 @@
# the same domain.
AUTH_COOKIE_SAMESITE = "Strict" # or 'Lax' or None
CSRF_COOKIE_SAMESITE = "Strict" # or 'Lax' or None
CSRF_TRUSTED_ORIGINS = [
"http://localhost:3000",
"http://localhost:8000",
]

SENDGRID_ALLOWED_SENDERS = env.list("SENDGRID_ALLOWED_SENDERS", default=[])
SENDGRID_WEBHOOK_LISTENERS = (
"dear_petition.petition.etl.email.sendgrid_webhook_listener",
)
SENDGRID_WEBHOOK_LISTENERS = ("dear_petition.petition.etl.email.sendgrid_webhook_listener",)
3 changes: 1 addition & 2 deletions config/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@
"disable_existing_loggers": False,
"formatters": {
"verbose": {
"format": "%(levelname)s %(asctime)s %(name)s "
"%(process)d %(thread)d %(message)s"
"format": "%(levelname)s %(asctime)s %(name)s " "%(process)d %(thread)d %(message)s"
}
},
"handlers": {
Expand Down
25 changes: 12 additions & 13 deletions config/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,11 @@
# TODO: set this to 60 seconds first and then to 518400 once you prove the former works
SECURE_HSTS_SECONDS = 60
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-include-subdomains
SECURE_HSTS_INCLUDE_SUBDOMAINS = env.bool(
"DJANGO_SECURE_HSTS_INCLUDE_SUBDOMAINS", default=True
)
SECURE_HSTS_INCLUDE_SUBDOMAINS = env.bool("DJANGO_SECURE_HSTS_INCLUDE_SUBDOMAINS", default=True)
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-preload
SECURE_HSTS_PRELOAD = env.bool("DJANGO_SECURE_HSTS_PRELOAD", default=True)
# https://docs.djangoproject.com/en/dev/ref/middleware/#x-content-type-options-nosniff
SECURE_CONTENT_TYPE_NOSNIFF = env.bool(
"DJANGO_SECURE_CONTENT_TYPE_NOSNIFF", default=True
)
SECURE_CONTENT_TYPE_NOSNIFF = env.bool("DJANGO_SECURE_CONTENT_TYPE_NOSNIFF", default=True)

# STORAGES
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -103,7 +99,14 @@

# STATIC
# ------------------------
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STORAGES = {
"default": {
"BACKEND": "config.settings.production.MediaRootS3Boto3Storage",
},
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}

# MEDIA
# ------------------------------------------------------------------------------
Expand All @@ -121,7 +124,6 @@ class MediaRootS3Boto3Storage(S3Boto3Storage):


# endregion
DEFAULT_FILE_STORAGE = "config.settings.production.MediaRootS3Boto3Storage"
MEDIA_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/media/"

# TEMPLATES
Expand All @@ -140,9 +142,7 @@ class MediaRootS3Boto3Storage(S3Boto3Storage):
# EMAIL
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#default-from-email
DEFAULT_FROM_EMAIL = env(
"DJANGO_DEFAULT_FROM_EMAIL", default=f"DEAR Petition <{ALLOWED_HOSTS[0]}>"
)
DEFAULT_FROM_EMAIL = env("DJANGO_DEFAULT_FROM_EMAIL", default=f"DEAR Petition <{ALLOWED_HOSTS[0]}>")
# https://docs.djangoproject.com/en/dev/ref/settings/#server-email
SERVER_EMAIL = env("DJANGO_SERVER_EMAIL", default=DEFAULT_FROM_EMAIL)
# https://docs.djangoproject.com/en/dev/ref/settings/#email-subject-prefix
Expand Down Expand Up @@ -177,8 +177,7 @@ class MediaRootS3Boto3Storage(S3Boto3Storage):
"disable_existing_loggers": True,
"formatters": {
"verbose": {
"format": "%(levelname)s %(asctime)s %(name)s "
"%(process)d %(thread)d %(message)s"
"format": "%(levelname)s %(asctime)s %(name)s " "%(process)d %(thread)d %(message)s"
}
},
"handlers": {
Expand Down
4 changes: 1 addition & 3 deletions config/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

# This allows easy placement of apps within the interior
# dear_petition directory.
app_path = os.path.abspath(
os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir)
)
app_path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir))
sys.path.append(os.path.join(app_path, "dear_petition"))
# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
Expand Down
5 changes: 1 addition & 4 deletions dear_petition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

__version__ = "0.1.0"
__version_info__ = tuple(
[
int(num) if num.isdigit() else num
for num in __version__.replace("-", ".", 1).split(".")
]
[int(num) if num.isdigit() else num for num in __version__.replace("-", ".", 1).split(".")]
)

__all__ = ("celery_app",)
1 change: 0 additions & 1 deletion dear_petition/contrib/sites/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = []

operations = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


class Migration(migrations.Migration):

dependencies = [("sites", "0001_initial")]

operations = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def update_site_backward(apps, schema_editor):


class Migration(migrations.Migration):

dependencies = [("sites", "0002_alter_domain_unique")]

operations = [migrations.RunPython(update_site_forward, update_site_backward)]
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("sites", "0003_set_site_domain_and_name"),
]
Expand Down
9 changes: 1 addition & 8 deletions dear_petition/petition/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

@admin.register(models.CIPRSRecord)
class CIPRSRecordAdmin(admin.ModelAdmin):

list_display = (
"pk",
"label",
Expand Down Expand Up @@ -165,7 +164,6 @@ class ContactAdmin(ImportExportModelAdmin):

@admin.register(models.Comment)
class CommentAdmin(admin.ModelAdmin):

list_display = ("pk", "user", "batch", "time")
search_fields = ("batch__label",)
date_hierarchy = "time"
Expand All @@ -175,7 +173,6 @@ class CommentAdmin(admin.ModelAdmin):

@admin.register(models.Petition)
class PetitionAdmin(admin.ModelAdmin):

list_display = ("pk", "batch", "form_type", "county", "jurisdiction")
search_fields = ("batch__label",)
list_filter = ("form_type", "county", "jurisdiction")
Expand All @@ -185,7 +182,6 @@ class PetitionAdmin(admin.ModelAdmin):

@admin.register(models.PetitionDocument)
class PetitionDocumentAdmin(admin.ModelAdmin):

list_display = (
"id",
"batch",
Expand All @@ -203,9 +199,7 @@ class PetitionDocumentAdmin(admin.ModelAdmin):

def get_queryset(self, request):
queryset = super().get_queryset(request)
queryset = queryset.annotate(
_offense_record_count=Count("offense_records", distinct=True)
)
queryset = queryset.annotate(_offense_record_count=Count("offense_records", distinct=True))
return queryset

def offense_record_count(self, obj):
Expand All @@ -222,7 +216,6 @@ def is_an_attachment(self, obj):

@admin.register(models.GeneratedPetition)
class GeneratedPetitionAdmin(admin.ModelAdmin):

date_hierarchy = "created"
list_display = constants.GENERATED_PETITION_ADMIN_FIELDS
list_filter = ("form_type", "created", "username")
Expand Down
14 changes: 11 additions & 3 deletions dear_petition/petition/api/authentication.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework.authentication import CSRFCheck, SessionAuthentication as DRFSessionAuthentication
from rest_framework.authentication import (
CSRFCheck,
SessionAuthentication as DRFSessionAuthentication,
)
from rest_framework.exceptions import NotAuthenticated
from django.conf import settings

Expand All @@ -8,7 +11,11 @@ def enforce_csrf(request):
"""
Enforce CSRF validation. From drf source, authentication.py
"""
check = CSRFCheck()

def dummy_get_response(request): # pragma: no cover
return None

check = CSRFCheck(dummy_get_response)
check.process_request(request)
reason = check.process_view(request, None, (), {})
if reason:
Expand Down Expand Up @@ -55,5 +62,6 @@ class SessionAuthentication(DRFSessionAuthentication):
We do set authenticate_header function in SessionAuthentication, so that a value for the WWW-Authenticate
header can be retrieved and the response code is automatically set to 401 in case of unauthenticated requests.
"""

def authenticate_header(self, request):
return 'Session'
return "Session"
Loading