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 all 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
26 changes: 26 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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: Install dependencies
run: |
npm i
python -m pip install --upgrade pip wheel
pip install pre-commit==3.6.2
- name: Run pre-commit
run: pre-commit run --all-files
9 changes: 2 additions & 7 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 Down Expand Up @@ -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
27 changes: 18 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ repos:
hooks:
- id: django-upgrade
args: [--target-version, "4.2"]
# - 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: "v3.1.0"
- repo: https://github.com/python/black
rev: "23.12.1"
hooks:
- id: prettier
args: [--config=./.prettierrc]
- id: black
language_version: python3.12
args: [--config=./pyproject.toml]
- repo: local
hooks:
- id: format
name: Run formatter (prettier)
language: system
entry: npx prettier --write --ignore-unknown
files: ".*\\.(js|jsx)$"
- id: lint
name: Run linter (eslint)
language: system
entry: npx eslint --fix
files: ".*\\.(js|jsx)$"

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())
12 changes: 3 additions & 9 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,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 Down Expand Up @@ -246,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 @@ -322,6 +318,4 @@
]

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
15 changes: 4 additions & 11 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 @@ -146,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 @@ -183,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
11 changes: 3 additions & 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 @@ -160,6 +159,7 @@ class ContactAdmin(admin.ModelAdmin):
list_filter = ("category",)
ordering = ("category", "name")


@admin.register(models.Agency)
class AgencyAdmin(ImportExportModelAdmin):
resource_classes = [resources.AgencyResource]
Expand All @@ -168,6 +168,7 @@ class AgencyAdmin(ImportExportModelAdmin):
list_filter = ("category",)
ordering = ("category", "name")


@admin.register(models.Client)
class ClientAdmin(admin.ModelAdmin):
list_display = ("pk", "name", "category", "address1")
Expand All @@ -177,7 +178,6 @@ class ClientAdmin(admin.ModelAdmin):

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

list_display = ("pk", "user", "batch", "time")
search_fields = ("batch__label",)
date_hierarchy = "time"
Expand All @@ -187,7 +187,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 @@ -197,7 +196,6 @@ class PetitionAdmin(admin.ModelAdmin):

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

list_display = (
"id",
"batch",
Expand All @@ -215,9 +213,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 @@ -234,7 +230,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
6 changes: 3 additions & 3 deletions dear_petition/petition/api/fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rest_framework import serializers


class ValidationField(serializers.SerializerMethodField):
def __init__(self, serializer, **kwargs):
self.serializer_class = serializer
Expand All @@ -13,12 +14,12 @@ def bind(self, field_name, parent):
super().bind(field_name, parent)

if self.serializer_class == self.parent:
raise ValueError('Validation serializer must not be parent of this serializer field')
raise ValueError("Validation serializer must not be parent of this serializer field")

def to_representation(self, value):
# handle case where serializer is creating new value
# TODO: Figure out how to get instance/native values for a new instance
if not hasattr(value, 'pk'):
if not hasattr(value, "pk"):
return None
method = getattr(self.parent, self.method_name)
try:
Expand All @@ -27,4 +28,3 @@ def to_representation(self, value):
return {}
except serializers.ValidationError as exc:
return exc.detail

Loading
Loading