Skip to content

Commit d9c9564

Browse files
committed
Fix pre commit + cleanup old compatibility code (#10)
1 parent c19d649 commit d9c9564

File tree

13 files changed

+594
-168
lines changed

13 files changed

+594
-168
lines changed

.github/workflows/lint.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: django-fsm linting
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v1
14+
- uses: actions/setup-python@v2
15+
with:
16+
python-version: '3.11'
17+
- uses: pre-commit/[email protected]

.github/workflows/tests.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
name: django-fsm testing
22

33
on:
4-
- push
5-
- pull_request
4+
pull_request:
5+
push:
6+
branches: [main]
67

78
jobs:
89
build:

.pre-commit-config.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
default_language_version:
2+
python: python3.11
3+
4+
repos:
5+
- repo: https://github.com/pre-commit/pre-commit-hooks
6+
rev: v4.5.0
7+
hooks:
8+
- id: check-added-large-files
9+
args: ["--maxkb=700"]
10+
- id: check-case-conflict
11+
- id: check-json
12+
- id: check-merge-conflict
13+
- id: check-symlinks
14+
- id: check-toml
15+
- id: check-yaml
16+
- id: debug-statements
17+
- id: end-of-file-fixer
18+
- id: trailing-whitespace
19+
20+
- repo: https://github.com/asottile/pyupgrade
21+
rev: v3.15.0
22+
hooks:
23+
- id: pyupgrade
24+
args:
25+
- "--py38-plus"
26+
27+
- repo: https://github.com/adamchainz/django-upgrade
28+
rev: 1.15.0
29+
hooks:
30+
- id: django-upgrade
31+
args: [--target-version, "3.2"]
32+
33+
34+
- repo: https://github.com/python-poetry/poetry
35+
rev: 1.6.1
36+
hooks:
37+
- id: poetry-check
38+
- id: poetry-lock
39+
- id: poetry-export
40+
41+
- repo: https://github.com/astral-sh/ruff-pre-commit
42+
rev: v0.0.291
43+
hooks:
44+
- id: ruff-format
45+
- id: ruff

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ django-fsm unreleased
1111
- Add support for django 4.2
1212
- Add support for django 5.0
1313
- Enable Github actions for testing
14+
- Remove South support...if exists
1415

1516
django-fsm 2.8.1 2022-08-15
1617
~~~~~~~~~~~~~~~~~~~~~~~~~~~

django_fsm/__init__.py

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,13 @@
55
from functools import partialmethod, wraps
66

77
import django
8+
from django.apps import apps as django_apps
89
from django.db import models
910
from django.db.models import Field
1011
from django.db.models.query_utils import DeferredAttribute
1112
from django.db.models.signals import class_prepared
12-
from django_fsm.signals import pre_transition, post_transition
13-
14-
try:
15-
from django.apps import apps as django_apps
16-
17-
def get_model(app_label, model_name):
18-
app = django_apps.get_app_config(app_label)
19-
return app.get_model(model_name)
2013

21-
22-
except ImportError:
23-
from django.db.models.loading import get_model
14+
from django_fsm.signals import pre_transition, post_transition
2415

2516

2617
__all__ = [
@@ -38,16 +29,6 @@ def get_model(app_label, model_name):
3829
"RETURN_VALUE",
3930
]
4031

41-
# South support; see http://south.aeracode.org/docs/tutorial/part4.html#simple-inheritance
42-
try:
43-
from south.modelsinspector import add_introspection_rules
44-
except ImportError:
45-
pass
46-
else:
47-
add_introspection_rules([], [r"^django_fsm\.FSMField"])
48-
add_introspection_rules([], [r"^django_fsm\.FSMIntegerField"])
49-
add_introspection_rules([], [r"^django_fsm\.FSMKeyField"])
50-
5132

5233
class TransitionNotAllowed(Exception):
5334
"""Raised when a transition is not allowed"""
@@ -308,7 +289,8 @@ def set_proxy(self, instance, state):
308289
app_label = instance._meta.app_label
309290
model_name = state_proxy
310291

311-
model = get_model(app_label, model_name)
292+
model = django_apps.get_app_config(app_label).get_model(model_name)
293+
312294
if model is None:
313295
raise ValueError(f"No model found {state_proxy}")
314296

@@ -385,9 +367,7 @@ def contribute_to_class(self, cls, name, **kwargs):
385367
super().contribute_to_class(cls, name, **kwargs)
386368
setattr(cls, self.name, self.descriptor_class(self))
387369
setattr(cls, f"get_all_{self.name}_transitions", partialmethod(get_all_FIELD_transitions, field=self))
388-
setattr(
389-
cls, f"get_available_{self.name}_transitions", partialmethod(get_available_FIELD_transitions, field=self)
390-
)
370+
setattr(cls, f"get_available_{self.name}_transitions", partialmethod(get_available_FIELD_transitions, field=self))
391371
setattr(
392372
cls,
393373
f"get_available_user_{self.name}_transitions",

django_fsm/management/commands/graph_transitions.py

Lines changed: 28 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,15 @@
11
import graphviz
2-
from optparse import make_option
32
from itertools import chain
43

4+
from django.apps import apps
55
from django.core.management.base import BaseCommand
6-
try:
7-
from django.utils.encoding import force_str
8-
_requires_system_checks = True
9-
except ImportError: # Django >= 4.0
10-
from django.utils.encoding import force_str
11-
from django.core.management.base import ALL_CHECKS
12-
_requires_system_checks = ALL_CHECKS
6+
from django.utils.encoding import force_str
137

148
from django_fsm import FSMFieldMixin, GET_STATE, RETURN_VALUE
159

16-
try:
17-
from django.db.models import get_apps, get_app, get_models, get_model
18-
19-
NEW_META_API = False
20-
except ImportError:
21-
from django.apps import apps
22-
23-
NEW_META_API = True
24-
25-
from django import VERSION
26-
27-
HAS_ARGPARSE = VERSION >= (1, 10)
28-
2910

3011
def all_fsm_fields_data(model):
31-
if NEW_META_API:
32-
return [(field, model) for field in model._meta.get_fields() if isinstance(field, FSMFieldMixin)]
33-
else:
34-
return [(field, model) for field in model._meta.fields if isinstance(field, FSMFieldMixin)]
12+
return [(field, model) for field in model._meta.get_fields() if isinstance(field, FSMFieldMixin)]
3513

3614

3715
def node_name(field, state):
@@ -40,7 +18,7 @@ def node_name(field, state):
4018

4119

4220
def node_label(field, state):
43-
if type(state) == int or (type(state) == bool and hasattr(field, "choices")):
21+
if isinstance(state, int) or (isinstance(state, bool) and hasattr(field, "choices")):
4422
return force_str(dict(field.choices).get(state))
4523
else:
4624
return state
@@ -137,54 +115,26 @@ def get_graphviz_layouts():
137115

138116

139117
class Command(BaseCommand):
140-
requires_system_checks = _requires_system_checks
141-
142-
if not HAS_ARGPARSE:
143-
option_list = BaseCommand.option_list + (
144-
make_option(
145-
"--output",
146-
"-o",
147-
action="store",
148-
dest="outputfile",
149-
help=(
150-
"Render output file. Type of output dependent on file extensions. " "Use png or jpg to render graph to image."
151-
),
152-
),
153-
# NOQA
154-
make_option(
155-
"--layout",
156-
"-l",
157-
action="store",
158-
dest="layout",
159-
default="dot",
160-
help=("Layout to be used by GraphViz for visualization. " "Layouts: %s." % " ".join(get_graphviz_layouts())),
161-
),
162-
)
163-
args = "[appname[.model[.field]]]"
164-
else:
165-
166-
def add_arguments(self, parser):
167-
parser.add_argument(
168-
"--output",
169-
"-o",
170-
action="store",
171-
dest="outputfile",
172-
help=(
173-
"Render output file. Type of output dependent on file extensions. " "Use png or jpg to render graph to image."
174-
),
175-
)
176-
parser.add_argument(
177-
"--layout",
178-
"-l",
179-
action="store",
180-
dest="layout",
181-
default="dot",
182-
help=("Layout to be used by GraphViz for visualization. " "Layouts: %s." % " ".join(get_graphviz_layouts())),
183-
)
184-
parser.add_argument("args", nargs="*", help=("[appname[.model[.field]]]"))
185-
186118
help = "Creates a GraphViz dot file with transitions for selected fields"
187119

120+
def add_arguments(self, parser):
121+
parser.add_argument(
122+
"--output",
123+
"-o",
124+
action="store",
125+
dest="outputfile",
126+
help=("Render output file. Type of output dependent on file extensions. " "Use png or jpg to render graph to image."),
127+
)
128+
parser.add_argument(
129+
"--layout",
130+
"-l",
131+
action="store",
132+
dest="layout",
133+
default="dot",
134+
help=("Layout to be used by GraphViz for visualization. " "Layouts: %s." % " ".join(get_graphviz_layouts())),
135+
)
136+
parser.add_argument("args", nargs="*", help=("[appname[.model[.field]]]"))
137+
188138
def render_output(self, graph, **options):
189139
filename, format = options["outputfile"].rsplit(".", 1)
190140

@@ -199,35 +149,19 @@ def handle(self, *args, **options):
199149
field_spec = arg.split(".")
200150

201151
if len(field_spec) == 1:
202-
if NEW_META_API:
203-
app = apps.get_app(field_spec[0])
204-
models = apps.get_models(app)
205-
else:
206-
app = get_app(field_spec[0])
207-
models = get_models(app)
152+
app = apps.get_app(field_spec[0])
153+
models = apps.get_models(app)
208154
for model in models:
209155
fields_data += all_fsm_fields_data(model)
210156
elif len(field_spec) == 2:
211-
if NEW_META_API:
212-
model = apps.get_model(field_spec[0], field_spec[1])
213-
else:
214-
model = get_model(field_spec[0], field_spec[1])
157+
model = apps.get_model(field_spec[0], field_spec[1])
215158
fields_data += all_fsm_fields_data(model)
216159
elif len(field_spec) == 3:
217-
if NEW_META_API:
218-
model = apps.get_model(field_spec[0], field_spec[1])
219-
else:
220-
model = get_model(field_spec[0], field_spec[1])
160+
model = apps.get_model(field_spec[0], field_spec[1])
221161
fields_data += all_fsm_fields_data(model)
222162
else:
223-
if NEW_META_API:
224-
for model in apps.get_models():
225-
fields_data += all_fsm_fields_data(model)
226-
else:
227-
for app in get_apps():
228-
for model in get_models(app):
229-
fields_data += all_fsm_fields_data(model)
230-
163+
for model in apps.get_models():
164+
fields_data += all_fsm_fields_data(model)
231165
dotdata = generate_dot(fields_data)
232166

233167
if options["outputfile"]:

django_fsm/tests/test_abstract_inheritance.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class AnotherFromAbstractModel(BaseAbstractModel):
2121
inherit from a shared abstract class (example: BaseAbstractModel).
2222
Don't try to remove it.
2323
"""
24+
2425
@transition(field="state", source="published", target="sticked")
2526
def stick(self):
2627
pass
@@ -53,6 +54,4 @@ def test_field_available_transitions_works(self):
5354

5455
def test_field_all_transitions_works(self):
5556
transitions = self.model.get_all_state_transitions()
56-
self.assertEqual(
57-
{("new", "published"), ("published", "sticked")}, {(data.source, data.target) for data in transitions}
58-
)
57+
self.assertEqual({("new", "published"), ("published", "sticked")}, {(data.source, data.target) for data in transitions})

django_fsm/tests/test_basic_transitions.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,12 @@ def test_available_conditions_from_published(self):
177177
transitions = self.model.get_available_state_transitions()
178178
actual = {(transition.source, transition.target) for transition in transitions}
179179
expected = {
180-
("*", "moderated"),
181-
("published", None),
182-
("published", "hidden"),
183-
("published", "stolen"),
184-
("*", ""),
185-
("+", "blocked"),
180+
("*", "moderated"),
181+
("published", None),
182+
("published", "hidden"),
183+
("published", "stolen"),
184+
("*", ""),
185+
("+", "blocked"),
186186
}
187187
self.assertEqual(actual, expected)
188188

@@ -221,14 +221,14 @@ def test_all_conditions(self):
221221

222222
actual = {(transition.source, transition.target) for transition in transitions}
223223
expected = {
224-
("*", "moderated"),
225-
("new", "published"),
226-
("new", "removed"),
227-
("published", None),
228-
("published", "hidden"),
229-
("published", "stolen"),
230-
("hidden", "stolen"),
231-
("*", ""),
232-
("+", "blocked"),
224+
("*", "moderated"),
225+
("new", "published"),
226+
("new", "removed"),
227+
("published", None),
228+
("published", "hidden"),
229+
("published", "stolen"),
230+
("hidden", "stolen"),
231+
("*", ""),
232+
("+", "blocked"),
233233
}
234234
self.assertEqual(actual, expected)

django_fsm/tests/test_proxy_inheritance.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,4 @@ def test_field_all_transitions_base_model(self):
4646

4747
def test_field_all_transitions_works(self):
4848
transitions = self.model.get_all_state_transitions()
49-
self.assertEqual(
50-
{("new", "published"), ("published", "sticked")}, {(data.source, data.target) for data in transitions}
51-
)
49+
self.assertEqual({("new", "published"), ("published", "sticked")}, {(data.source, data.target) for data in transitions})

0 commit comments

Comments
 (0)