Skip to content

Commit

Permalink
🚧chore: Add support for Django 4.2
Browse files Browse the repository at this point in the history
fixed rsinger86#114

adopted some of the changes in rsinger86#115

and those in rsinger86/django-lifecycle@0.9.2...0.9.3

Signed-off-by: KimSia, Sim <245021+simkimsia@users.noreply.github.com>
  • Loading branch information
simkimsia committed Sep 4, 2023
1 parent 162226e commit 1403c23
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 25 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

This project provides a `@hook` decorator as well as a base model and mixin to add lifecycle hooks to your Django models. Django's built-in approach to offering lifecycle hooks is [Signals](https://docs.djangoproject.com/en/dev/topics/signals/). However, my team often finds that Signals introduce unnecessary indirection and are at odds with Django's "fat models" approach.

**Django Lifecycle Hooks** supports Python 3.7, 3.8 and 3.9, Django 2.0.x, 2.1.x, 2.2.x, 3.0.x, 3.1.x, and 3.2.x.
**Django Lifecycle Hooks** supports Python 3.7, 3.8, 3.9, and 3.10, Django 2.0.x, 2.1.x, 2.2.x, 3.0.x, 3.1.x, 3.2.x, 4.0.x, 4.1.x, and 4.2.x.

In short, you can write model code like this:

Expand Down Expand Up @@ -61,6 +61,10 @@ Instead of overriding `save` and `__init__` in a clunky way that hurts readabili

# Changelog

## 1.0.2 (September 2023)

- Correct package info to note that Django 4.0, 4.1, and 4.2 are supported.

## 1.0.1 (August 2023)

- Initial state gets reset using `transaction.on_commit()`, fixing the `has_changed()` and `initial_value()` methods for on_commit hooks. Thanks @alb3rto269!
Expand Down
6 changes: 3 additions & 3 deletions django_lifecycle/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .django_info import IS_GTE_1_POINT_9

__version__ = "1.0.0"
__version__ = "1.0.2"
__author__ = "Robert Singer"
__author_email__ = "robertgsinger@gmail.com"

Expand All @@ -10,9 +10,9 @@ class NotSet(object):


from .decorators import hook
from .mixins import LifecycleModelMixin
from .hooks import *

from .mixins import LifecycleModelMixin

if IS_GTE_1_POINT_9:
from .models import LifecycleModel
pass
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
asgiref==3.4.1
Click==7.0
Django==3.2.8
Django==4.2.4
django-capture-on-commit-callbacks==1.10.0
djangorestframework==3.11.2
ghp-import==2.0.2
Expand Down
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ def readme():
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Framework :: Django",
"Framework :: Django :: 2.0",
"Framework :: Django :: 2.1",
"Framework :: Django :: 2.2",
"Framework :: Django :: 3.0",
"Framework :: Django :: 3.1",
"Framework :: Django :: 3.2",
"Framework :: Django :: 4.0",
"Framework :: Django :: 4.1",
"Framework :: Django :: 4.2",
]
setup(
name="django-lifecycle",
Expand Down
7 changes: 6 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"""

import os
from packaging.version import Version

import django
from packaging.version import Version

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand Down Expand Up @@ -120,3 +120,8 @@
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = "/static/"

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
51 changes: 35 additions & 16 deletions tests/testapp/tests/test_user_account.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import contextlib
import datetime

from django.core import mail
from django.test import TestCase

from django_capture_on_commit_callbacks import capture_on_commit_callbacks

from tests.testapp.models import CannotDeleteActiveTrial, Organization, UserAccount
from tests.testapp.models import (CannotDeleteActiveTrial, Organization,
UserAccount)

# Note: Can't use contextlib.supress in Python 2
with contextlib.suppress(ImportError):
from django_capture_on_commit_callbacks import capture_on_commit_callbacks

class UserAccountTestCase(TestCase):
@property
Expand All @@ -24,8 +27,12 @@ def test_update_joined_at_before_create(self):
self.assertTrue(isinstance(account.joined_at, datetime.datetime))

def test_send_welcome_email_after_create(self):
with capture_on_commit_callbacks(execute=True) as callbacks:
UserAccount.objects.create(**self.stub_data)
if hasattr(self, "captureOnCommitCallbacks"):
with self.captureOnCommitCallbacks(execute=True) as callbacks:
UserAccount.objects.create(**self.stub_data)
else:
with capture_on_commit_callbacks(execute=True) as callbacks:
UserAccount.objects.create(**self.stub_data)

self.assertEquals(len(callbacks), 2, msg=f"{callbacks}")
self.assertEqual(len(mail.outbox), 1)
Expand Down Expand Up @@ -96,11 +103,18 @@ def test_notify_org_name_change(self):

account = UserAccount.objects.get()

with capture_on_commit_callbacks(execute=True) as callbacks:
org.name = "Coursera Wizardry"
org.save()
if hasattr(self, "captureOnCommitCallbacks"):
with self.captureOnCommitCallbacks(execute=True) as callbacks:
org.name = "Coursera Wizardry"
org.save()

account.save()
account.save()
else:
with capture_on_commit_callbacks(execute=True) as callbacks:
org.name = "Coursera Wizardry"
org.save()

account.save()

self.assertEquals(len(callbacks), 3)
self.assertEqual(len(mail.outbox), 1)
Expand All @@ -122,13 +136,18 @@ def test_additional_notify_sent_for_specific_org_name_change(self):

mail.outbox = []

with capture_on_commit_callbacks(execute=True) as callbacks:
account = UserAccount.objects.get()

org.name = "Hogwarts Online"
org.save()

account.save()
if hasattr(self, "captureOnCommitCallbacks"):
with self.captureOnCommitCallbacks(execute=True) as callbacks:
account = UserAccount.objects.get()
org.name = "Hogwarts Online"
org.save()
account.save()
else:
with capture_on_commit_callbacks(execute=True) as callbacks:
account = UserAccount.objects.get()
org.name = "Hogwarts Online"
org.save()
account.save()

self.assertEquals(len(callbacks), 3, msg="One hook and the _reset_initial_state (2) should be in the on_commit callbacks")
self.assertEqual(len(mail.outbox), 2)
Expand Down
13 changes: 10 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ envlist =
{py35,py36,py37,py38,py39}-django22
{py36,py37,py38,py39}-django30
{py36,py37,py38,py39}-django31
{py36,py37,py38,py39,py310}-django32
{py38,py39,py310}-django40
{py38,py39,py310}-django41
{py38,py39,py310}-django42
flake8
skip_missing_interpreters = True

Expand All @@ -22,14 +26,17 @@ setenv =
deps =
django20: django>=2.0,<2.1
django21: django>=2.1,<2.2
django22: django>=2.2,<3
django22: django>=2.2,<3.0
django30: django>=3.0,<3.1
django31: django>=3.1,<3.2
django31: django>=3.2,<3.3
django32: django>=3.2,<3.3
django40: django>=4.0,<4.1
django41: django>=4.1,<4.2
django42: django>=4.2,<4.3

[testenv:flake8]
basepython = python3.7
deps =
flake8==3.8.4
commands =
flake8 . --exclude=venv/,.tox/,django_lifecycle/__init__.py
flake8 . --exclude=.venv/,venv/,.tox/,django_lifecycle/__init__.py

0 comments on commit 1403c23

Please sign in to comment.