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

Introduce app settings #470

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions django_celery_results/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Application settings."""
from dataclasses import dataclass
from typing import Any

from django.conf import settings as django_settings

# All attributes accessed with this prefix are possible
# to overwrite through django.conf.settings.
SETTINGS_PREFIX = "DJANGO_CELERY_RESULTS_"


@dataclass(frozen=True)
class AppSettings:
"""Proxy class to encapsulate all the app settings.

This instance should be accessed via the singleton
``django_celery_results.conf.app_settings``.

You shouldn't have to set any of these yourself, the class checks a Django
settings with the same name and use these if defined, defaulting to the
values documented here.
"""

DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH: int = 255

def __getattribute__(self, __name: str) -> Any:
"""Check if a Django project settings should override the app default.

In order to avoid returning any random properties of the Django
settings, we first inspect the prefix.
"""
if (
__name.startswith(SETTINGS_PREFIX)
and hasattr(django_settings, __name)
):
return getattr(django_settings, __name)

return super().__getattribute__(__name)


app_settings = AppSettings()
9 changes: 3 additions & 6 deletions django_celery_results/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.conf import settings
from django.db import migrations, models

from django_celery_results.conf import app_settings


class Migration(migrations.Migration):

Expand All @@ -18,11 +19,7 @@ class Migration(migrations.Migration):
serialize=False,
verbose_name='ID')),
('task_id', models.CharField(
max_length=getattr(
settings,
'DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH',
255
),
max_length=app_settings.DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH, # noqa: E501
unique=True,
verbose_name='task id'
)),
Expand Down
14 changes: 4 additions & 10 deletions django_celery_results/migrations/0004_auto_20190516_0412.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from django.conf import settings
from django.db import migrations, models

from django_celery_results.conf import app_settings


class Migration(migrations.Migration):

Expand Down Expand Up @@ -61,11 +63,7 @@ class Migration(migrations.Migration):
field=models.CharField(
db_index=True,
help_text='Celery ID for the Task that was run',
max_length=getattr(
settings,
'DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH',
255
),
max_length=app_settings.DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH,
unique=True,
verbose_name='Task ID'
),
Expand All @@ -81,11 +79,7 @@ class Migration(migrations.Migration):
field=models.CharField(
db_index=True,
help_text='Name of the Task which was run',
max_length=getattr(
settings,
'DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH',
255
),
max_length=app_settings.DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH,
null=True,
verbose_name='Task Name'),
),
Expand Down
9 changes: 3 additions & 6 deletions django_celery_results/migrations/0008_chordcounter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Generated by Django 3.0.6 on 2020-05-12 12:05

from django.conf import settings
from django.db import migrations, models

from django_celery_results.conf import app_settings


class Migration(migrations.Migration):

Expand All @@ -22,11 +23,7 @@ class Migration(migrations.Migration):
('group_id', models.CharField(
db_index=True,
help_text='Celery ID for the Chord header group',
max_length=getattr(
settings,
'DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH',
255
),
max_length=app_settings.DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH, # noqa: E501
unique=True,
verbose_name='Group ID')),
('sub_tasks', models.TextField(
Expand Down
27 changes: 6 additions & 21 deletions django_celery_results/migrations/0009_groupresult.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Generated by Django 3.2 on 2021-04-19 14:55
from django.conf import settings
from django.db import migrations, models

from django_celery_results.conf import app_settings


class FakeAddIndex(migrations.AddIndex):
"""Fake AddIndex to correct for duplicate index
Expand Down Expand Up @@ -35,11 +36,7 @@ class Migration(migrations.Migration):
verbose_name='ID')),
('group_id', models.CharField(
help_text='Celery ID for the Group that was run',
max_length=getattr(
settings,
'DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH',
255
),
max_length=app_settings.DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH, # noqa: E501
unique=True,
verbose_name='Group ID')),
('date_created', models.DateTimeField(
Expand Down Expand Up @@ -81,11 +78,7 @@ class Migration(migrations.Migration):
name='group_id',
field=models.CharField(
help_text='Celery ID for the Chord header group',
max_length=getattr(
settings,
'DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH',
255
),
max_length=app_settings.DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH, # noqa: E501
unique=True,
verbose_name='Group ID'),
),
Expand Down Expand Up @@ -128,11 +121,7 @@ class Migration(migrations.Migration):
name='task_id',
field=models.CharField(
help_text='Celery ID for the Task that was run',
max_length=getattr(
settings,
'DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH',
255
),
max_length=app_settings.DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH, # noqa: E501
unique=True,
verbose_name='Task ID'),
),
Expand All @@ -141,11 +130,7 @@ class Migration(migrations.Migration):
name='task_name',
field=models.CharField(
help_text='Name of the Task which was run',
max_length=getattr(
settings,
'DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH',
255
),
max_length=app_settings.DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH, # noqa: E501
null=True,
verbose_name='Task Name'),
),
Expand Down
26 changes: 6 additions & 20 deletions django_celery_results/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from celery import states
from celery.result import GroupResult as CeleryGroupResult
from celery.result import result_from_tuple
from django.conf import settings
from django.db import models
from django.utils.translation import gettext_lazy as _

from . import managers
from .conf import app_settings

ALL_STATES = sorted(states.ALL_STATES)
TASK_STATE_CHOICES = sorted(zip(ALL_STATES, ALL_STATES))
Expand All @@ -19,11 +19,7 @@ class TaskResult(models.Model):
"""Task result/status."""

task_id = models.CharField(
max_length=getattr(
settings,
'DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH',
255
),
max_length=app_settings.DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH,
unique=True,
verbose_name=_('Task ID'),
help_text=_('Celery ID for the Task that was run'))
Expand All @@ -32,11 +28,8 @@ class TaskResult(models.Model):
verbose_name=_('Periodic Task Name'),
help_text=_('Name of the Periodic Task which was run'))
task_name = models.CharField(
null=True, max_length=getattr(
settings,
'DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH',
255
),
null=True,
max_length=app_settings.DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH,
verbose_name=_('Task Name'),
help_text=_('Name of the Task which was run'))
task_args = models.TextField(
Expand Down Expand Up @@ -140,10 +133,7 @@ class ChordCounter(models.Model):
"""Chord synchronisation."""

group_id = models.CharField(
max_length=getattr(
settings,
"DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH",
255),
max_length=app_settings.DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH,
unique=True,
verbose_name=_("Group ID"),
help_text=_("Celery ID for the Chord header group"),
Expand Down Expand Up @@ -181,11 +171,7 @@ class GroupResult(models.Model):
"""Task Group result/status."""

group_id = models.CharField(
max_length=getattr(
settings,
"DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH",
255
),
max_length=app_settings.DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH,
unique=True,
verbose_name=_("Group ID"),
help_text=_("Celery ID for the Group that was run"),
Expand Down
15 changes: 15 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Configuration
=============

These are the available settings that can be configured in your Django
project's settings module by defining a setting with the same name.

.. _settings-task_id_max_length:

* ``DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH`` (Default: ``255``)

The max length, as an integer, of the ``task_id`` and ``task_name``
fields on the ``TaskResult`` model. Defaults to 255.

Also used for the max length of the ``group_id`` fields on the
``GroupResult`` and ``ChordCounter`` models.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Contents

getting_started
injecting_metadata
configuration
copyright

.. toctree::
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ markers =
[flake8]
# classes can be lowercase, arguments and variables can be uppercase
# whenever it makes the code more readable.
ignore = N806, N802, N801, N803
ignore = N806, N802, N801, N803, W503

[pep257]
convention=google
Expand Down