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

Cleanup #65

Merged
merged 14 commits into from
Feb 27, 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
29 changes: 9 additions & 20 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,20 @@ repos:
- id: detect-private-key

- repo: https://github.com/adamchainz/django-upgrade
rev: "1.15.0"
rev: "1.16.0"
hooks:
- id: django-upgrade
args: ["--target-version", "4.2"]

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
# Run the Ruff linter.
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.2
hooks:
- id: pyupgrade
args: [--py312-plus]

- repo: https://github.com/psf/black
rev: 23.12.1
hooks:
- id: black

- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort

- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
# Linter
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
# Formatter
- id: ruff-format

- repo: https://github.com/Riverside-Healthcare/djLint
rev: v1.34.1
Expand Down
16 changes: 16 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ more from the `Celery Workers Guide`_.
You can also use Django admin to queue up tasks, thanks to the
`django-celerybeat`_ package.

To run [periodic tasks](https://docs.celeryq.dev/en/stable/userguide/periodic-tasks.html),
you'll need to start the celery beat scheduler service. You can start it as a
standalone process:

```bash
cd democrasite
celery -A config.celery_app beat
```

or you can embed the beat service inside a worker with the `-B` option:

```bash
cd democrasite
celery -A config.celery_app worker -B -l info
```

.. _Getting started with Redis guide: https://redis.io/docs/getting-started/
.. _Celery Workers Guide: https://docs.celeryq.dev/en/stable/userguide/workers.html
.. _django-celerybeat: https://django-celery-beat.readthedocs.io/en/latest/
Expand Down
14 changes: 6 additions & 8 deletions config/api_router.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
from django.conf import settings
from rest_framework.routers import DefaultRouter, SimpleRouter
from rest_framework.routers import DefaultRouter
from rest_framework.routers import SimpleRouter

from democrasite.users.api.views import UserViewSet
from democrasite.webiscite.api.views import BillViewSet

router: SimpleRouter | DefaultRouter
if settings.DEBUG:
router = DefaultRouter()
else:
router = SimpleRouter()
router = DefaultRouter() if settings.DEBUG else SimpleRouter()

router.register("users", UserViewSet)
router.register("bills", BillViewSet)

# Unfortunately if we want to automatically create links between models we can't use a namespace
# app_name = "api"
# Unfortunately if we want automatical links for models we can't use a namespace
# but I may reconsider anyway
# app_name = "api" # noqa: ERA001
urlpatterns = router.urls
61 changes: 39 additions & 22 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

import django_stubs_ext
import environ
from machina import MACHINA_MAIN_STATIC_DIR, MACHINA_MAIN_TEMPLATE_DIR
from machina import MACHINA_MAIN_STATIC_DIR
from machina import MACHINA_MAIN_TEMPLATE_DIR

# Monkeypatching Django, so stubs will work for all generics,
# see: https://github.com/typeddjango/django-stubs
django_stubs_ext.monkeypatch()

# TODO: Change to base_dir
ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
# democrasite/
APPS_DIR = ROOT_DIR / "democrasite"
APPS_DIR = BASE_DIR / "democrasite"
env = environ.Env()

READ_DOT_ENV_FILE = env.bool("DJANGO_READ_DOT_ENV_FILE", default=False)
if READ_DOT_ENV_FILE:
# OS environment variables take precedence over variables from .env
env.read_env(str(ROOT_DIR / ".env"))
env.read_env(str(BASE_DIR / ".env"))

# GENERAL
# ------------------------------------------------------------------------------
Expand All @@ -39,7 +39,7 @@
# https://docs.djangoproject.com/en/dev/ref/settings/#use-tz
USE_TZ = True
# https://docs.djangoproject.com/en/dev/ref/settings/#locale-paths
LOCALE_PATHS = [str(ROOT_DIR / "locale")]
LOCALE_PATHS = [str(BASE_DIR / "locale")]

# DATABASES
# ------------------------------------------------------------------------------
Expand All @@ -51,6 +51,18 @@
# https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-DEFAULT_AUTO_FIELD
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# CACHES
# ------------------------------------------------------------------------------
# Machina needs this cache to be defined
# TODO: I don't know if this will work in production
CACHES = {
"machina_attachments": {
"BACKEND": "django.core.cache.backends.filebased.FileBasedCache",
"LOCATION": "/tmp", # noqa: S108
},
}


# URLS
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#root-urlconf
Expand Down Expand Up @@ -140,7 +152,9 @@
]
# 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" # noqa: E501
},
{"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 @@ -168,7 +182,7 @@
# STATIC
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = str(ROOT_DIR / "staticfiles")
STATIC_ROOT = str(BASE_DIR / "staticfiles")
# https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = "/static/"
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
Expand Down Expand Up @@ -210,7 +224,7 @@
"django.contrib.messages.context_processors.messages",
# Custom context processors
"democrasite.webiscite.context_processors.settings_context",
# Machina (forum)
# Machina
"machina.core.context_processors.metadata",
],
},
Expand Down Expand Up @@ -272,7 +286,10 @@
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"verbose": {"format": "%(levelname)s %(asctime)s %(module)s " "%(process)d %(thread)d %(message)s"}
"verbose": {
"format": "%(levelname)s %(asctime)s %(module)s "
"%(process)d %(thread)d %(message)s"
}
},
"handlers": {
"console": {
Expand Down Expand Up @@ -311,13 +328,6 @@
CELERY_TIMEZONE = TIME_ZONE
# https://docs.celeryq.dev/en/stable/userguide/configuration.html#std:setting-broker_url
CELERY_BROKER_URL = env("CELERY_BROKER_URL", default="")
# https://docs.celeryproject.org/en/latest/userguide/configuration.html#std-setting-broker_transport_options
CELERY_BROKER_TRANSPORT_OPTIONS = {
# Allow tasks to remain in queue long enough for bills to finish voting period
# See also:
# https://docs.celeryproject.org/en/latest/getting-started/backends-and-brokers/redis.html#visibility-timeout
"visibility_timeout": (60 * 60 * 24 * (7 + 1)) # voting period + 1 days in seconds
}
# https://docs.celeryq.dev/en/stable/userguide/configuration.html#std:setting-result_backend
CELERY_RESULT_BACKEND = CELERY_BROKER_URL
# https://docs.celeryq.dev/en/stable/userguide/configuration.html#result-extended
Expand Down Expand Up @@ -348,12 +358,17 @@
# ------------------------------------------------------------------------------
ACCOUNT_ALLOW_REGISTRATION = env.bool("DJANGO_ACCOUNT_ALLOW_REGISTRATION", True)
# Local registration is currently disabled, accounts must be created through a provider
ACCOUNT_ALLOW_LOCAL_REGISTRATION = env.bool("DJANGO_ACCOUNT_ALLOW_LOCAL_REGISTRATION", ACCOUNT_ALLOW_REGISTRATION)
ACCOUNT_ALLOW_SOCIAL_REGISTRATION = env.bool("DJANGO_ACCOUNT_ALLOW_SOCIAL_REGISTRATION", ACCOUNT_ALLOW_REGISTRATION)
ACCOUNT_ALLOW_LOCAL_REGISTRATION = env.bool(
"DJANGO_ACCOUNT_ALLOW_LOCAL_REGISTRATION", ACCOUNT_ALLOW_REGISTRATION
)
ACCOUNT_ALLOW_SOCIAL_REGISTRATION = env.bool(
"DJANGO_ACCOUNT_ALLOW_SOCIAL_REGISTRATION", ACCOUNT_ALLOW_REGISTRATION
)
# https://django-allauth.readthedocs.io/en/latest/configuration.html
ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_EMAIL_REQUIRED = True
# If email verification is used I would like to apply it to each linked account individually
# If email verification is used I would like to apply it to each linked account
# individually so we don't have to rely on providers to do so
ACCOUNT_EMAIL_VERIFICATION = "none"
ACCOUNT_MAX_EMAIL_ADDRESSES = 1
ACCOUNT_FORMS = {
Expand Down Expand Up @@ -404,7 +419,9 @@
"rest_framework.authentication.SessionAuthentication",
"rest_framework.authentication.TokenAuthentication",
),
"DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticatedOrReadOnly",),
"DEFAULT_PERMISSION_CLASSES": (
"rest_framework.permissions.IsAuthenticatedOrReadOnly",
),
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
}

Expand All @@ -420,7 +437,7 @@
"SERVE_PERMISSIONS": ["rest_framework.permissions.IsAdminUser"],
}

# Machina (forum)
# Machina
# ------------------------------------------------------------------------------
# https://django-machina.readthedocs.io/en/stable/settings.html#machina-forum-name
MACHINA_FORUM_NAME = "Democrasite Forum"
Expand Down
36 changes: 18 additions & 18 deletions config/settings/local.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""Settings for development environment"""

from .base import * # noqa pylint: disable=wildcard-import,unused-wildcard-import
from .base import * # noqa: F403
from .base import BASE_DIR
from .base import CACHES
from .base import INSTALLED_APPS
from .base import MIDDLEWARE
from .base import env

# GENERAL
Expand All @@ -13,43 +17,39 @@
default="3Mfi1l3xK2ctztoU1wCQFSTz1HNTl22NCc1yi2wUezqfdwlvC0v0121Gr2NWSECi",
)
# https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ["localhost", "0.0.0.0", "127.0.0.1"]
ALLOWED_HOSTS = ["localhost", "0.0.0.0", "127.0.0.1"] # noqa: S104
# Admin site is only enabled during development
INSTALLED_APPS += ["django.contrib.admin"] # type: ignore[used-before-def] # noqa F405
INSTALLED_APPS += ["django.contrib.admin"] # type: ignore[used-before-def]

# CACHES
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#caches
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
"LOCATION": "",
},
"machina_attachments": {
"BACKEND": "django.core.cache.backends.filebased.FileBasedCache",
"LOCATION": "/tmp",
},
CACHES["default"] = {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
"LOCATION": "",
}

# EMAIL
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
EMAIL_BACKEND = env("DJANGO_EMAIL_BACKEND", default="django.core.mail.backends.filebased.EmailBackend")
EMAIL_BACKEND = env(
"DJANGO_EMAIL_BACKEND", default="django.core.mail.backends.filebased.EmailBackend"
)
# https://docs.djangoproject.com/en/dev/ref/settings/#email-file-path
EMAIL_FILE_PATH = ROOT_DIR / "app-messages" # noqa F405
EMAIL_FILE_PATH = BASE_DIR / "app-messages"

# WhiteNoise
# ------------------------------------------------------------------------------
# http://whitenoise.evans.io/en/latest/django.html#using-whitenoise-in-development
INSTALLED_APPS = ["whitenoise.runserver_nostatic"] + INSTALLED_APPS # noqa F405
INSTALLED_APPS = ["whitenoise.runserver_nostatic", *INSTALLED_APPS]


# django-debug-toolbar
# ------------------------------------------------------------------------------
# https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#prerequisites
INSTALLED_APPS += ["debug_toolbar"] # noqa F405
INSTALLED_APPS += ["debug_toolbar"]
# https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#middleware
MIDDLEWARE += ["debug_toolbar.middleware.DebugToolbarMiddleware"] # noqa F405
MIDDLEWARE += ["debug_toolbar.middleware.DebugToolbarMiddleware"]
# https://django-debug-toolbar.readthedocs.io/en/latest/configuration.html#debug-toolbar-config
DEBUG_TOOLBAR_CONFIG = {
"DISABLE_PANELS": ["debug_toolbar.panels.redirects.RedirectsPanel"],
Expand All @@ -62,7 +62,7 @@
# django-extensions
# ------------------------------------------------------------------------------
# https://django-extensions.readthedocs.io/en/latest/installation_instructions.html#configuration
INSTALLED_APPS += ["django_extensions"] # noqa F405
INSTALLED_APPS += ["django_extensions"]
# Celery
# ------------------------------------------------------------------------------
# https://docs.celeryq.dev/en/stable/userguide/configuration.html#task-always-eager
Expand Down
Loading
Loading