Skip to content

Commit

Permalink
Replaced flake8 with ruff and added it to CI (#306)
Browse files Browse the repository at this point in the history
* Replaced flake8 with ruff and added it to CI

* Added missing newline

* Added pre-commit including docs

* Add venv in project for poetry.
  • Loading branch information
GitRon committed Feb 4, 2024
1 parent ab2def2 commit 685cfe1
Show file tree
Hide file tree
Showing 19 changed files with 337 additions and 144 deletions.
20 changes: 18 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Unit tests
name: Quality assurance

on:
push:
Expand All @@ -7,6 +7,22 @@ on:
branches: [ main ]

jobs:
linting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python 3.12
uses: actions/setup-python@v4
with:
python-version: "3.12"

- name: Install required packages
run: pip install ruff

- name: Run linter "Ruff"
run: ruff .

build:
name: Python ${{ matrix.python-version }}, django ${{ matrix.django-version }}
runs-on: ubuntu-latest
Expand All @@ -19,7 +35,7 @@ jobs:
django-version: 22

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: setup python
uses: actions/setup-python@v4
with:
Expand Down
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# You find the full pre-commit-tools docs here:
# https://pre-commit.com/

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.6
hooks:
# Run the Ruff linter.
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
26 changes: 26 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,32 @@ If you'd like to fix a bug, add a feature, etc
Write your own test showing the issue has been resolved, or the feature
works as intended.

Git hooks (via pre-commit)
==========================

We use pre-push hooks to ensure that only linted code reaches our remote repository and pipelines aren't triggered in
vain.

To enable the configured pre-push hooks, you need to [install](https://pre-commit.com/) pre-commit and run once::

pre-commit install -t pre-push -t pre-commit --install-hooks

This will permanently install the git hooks for both, frontend and backend, in your local
[`.git/hooks`](./.git/hooks) folder.
The hooks are configured in the [`.pre-commit-config.yaml`](.pre-commit-config.yaml).

You can check whether hooks work as intended using the [run](https://pre-commit.com/#pre-commit-run) command::

pre-commit run [hook-id] [options]

Example: run single hook::

pre-commit run ruff --all-files --hook-stage push

Example: run all hooks of pre-push stage::

pre-commit run --all-files --hook-stage push

Running Tests
=============
To run the tests::
Expand Down
9 changes: 4 additions & 5 deletions django_ses/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import logging
from datetime import datetime, timedelta
from time import sleep

import boto3
from botocore.vendored.requests.packages.urllib3.exceptions import ResponseError
from django.core.mail.backends.base import BaseEmailBackend
from django_ses import settings

from datetime import datetime, timedelta
from time import sleep

from django_ses import settings

try:
import importlib.metadata as importlib_metadata
Expand Down Expand Up @@ -268,7 +267,7 @@ def _get_v2_parameters(self, message, source, email_feedback):
params['FromEmailAddressIdentityArn'] = self.ses_from_arn or self.ses_source_arn
if email_feedback is not None:
params['FeedbackForwardingEmailAddress'] = email_feedback

return params

def _get_v1_parameters(self, message, source):
Expand Down
1 change: 0 additions & 1 deletion django_ses/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from django_ses.views import DashboardView


urlpatterns = [
path('', DashboardView.as_view(), name='django_ses_stats'),
]
2 changes: 1 addition & 1 deletion django_ses/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def _get_bytes_to_sign(self):
return "".join(bytes_to_sign).encode()


def BounceMessageVerifier(*args, **kwargs):
def BounceMessageVerifier(*args, **kwargs): # noqa: N802
warnings.warn(
"utils.BounceMessageVerifier is deprecated. It is renamed to EventMessageVerifier.",
RemovedInDjangoSES20Warning,
Expand Down
33 changes: 14 additions & 19 deletions django_ses/views.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import copy
import json
import logging
import warnings
from urllib.error import URLError
from urllib.request import urlopen

import boto3
import pytz
from django.core.cache import cache
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse, HttpResponseBadRequest
from django.shortcuts import render
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from django.views.generic.base import TemplateView, View

from django_ses import settings, signals, utils
from django_ses.deprecation import RemovedInDjangoSES20Warning

from urllib.request import urlopen
from urllib.error import URLError
import copy
import logging


from django.http import HttpResponse, HttpResponseBadRequest
from django.views.decorators.http import require_POST
from django.core.cache import cache
from django.core.exceptions import PermissionDenied
from django.shortcuts import render

from django_ses import settings
from django_ses import signals
from django_ses import utils

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -99,7 +93,8 @@ def dashboard(request):
"""
Graph SES send statistics over time.
"""
warnings.warn('This view will be removed in future versions. Consider using DashboardView instead', DeprecationWarning)
warnings.warn('This view will be removed in future versions. Consider using DashboardView instead',
DeprecationWarning)
cache_key = 'vhash:django_ses_stats'
cached_view = cache.get(cache_key)
if cached_view:
Expand Down Expand Up @@ -170,7 +165,7 @@ def get_context_data(self, **kwargs):
verified_emails = emails_parse(verified_emails_dict)
ordered_data = stats_to_list(stats)
summary = sum_stats(ordered_data)

context.update({
'title': 'SES Statistics',
'datapoints': ordered_data,
Expand All @@ -193,7 +188,7 @@ def get(self, request, *args, **kwargs):
cached_view = cache.get(cache_key)
if cached_view:
return cached_view

response = super().get(request, *args, **kwargs).render()
cache.set(cache_key, response, 60 * 15) # Cache for 15 minutes
return response
Expand Down
1 change: 1 addition & 0 deletions example/local_settings.template.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# noqa: N999
AWS_ACCESS_KEY_ID = 'YOUR-ACCESS-KEY-ID'
AWS_SECRET_ACCESS_KEY = 'YOUR-SECRET-ACCESS-KEY'
2 changes: 1 addition & 1 deletion example/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
re_path(r'^send-email/$', views.send_email, name='send-email'),
re_path(r'^reporting/', include('django_ses.urls')),

re_path(r'^bounce/', 'django_ses.views.handle_bounce', name='handle_bounce'), # Deprecated, see SESEventWebhookView.
re_path(r'^bounce/', 'django_ses.views.handle_bounce', name='handle_bounce'), # Deprecated, see SESEventWebhookView
re_path(r'^event-webhook/', SESEventWebhookView.as_view(), name='event_webhook'),
]

Expand Down

0 comments on commit 685cfe1

Please sign in to comment.