Skip to content

Commit

Permalink
Fix pre commit + cleanup old compatibility code (#10)
Browse files Browse the repository at this point in the history
Fix ruff settings
  • Loading branch information
pfouque committed Dec 24, 2023
1 parent 85b4f9f commit 245dc0c
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 196 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: django-fsm linting

on:
pull_request:
push:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v2
with:
python-version: '3.11'
- uses: pre-commit/[email protected]
5 changes: 3 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
name: django-fsm testing

on:
- push
- pull_request
pull_request:
push:
branches: [main]

jobs:
build:
Expand Down
15 changes: 4 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args:
- "--py38-plus"

- repo: https://github.com/adamchainz/django-upgrade
rev: 1.15.0
hooks:
Expand All @@ -32,14 +25,14 @@ repos:


- repo: https://github.com/python-poetry/poetry
rev: 1.6.1
rev: 1.6.0
hooks:
- id: poetry-check
- id: poetry-lock
- id: poetry-export
# - id: poetry-lock
# - id: poetry-export

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.291
rev: v0.1.3
hooks:
- id: ruff-format
- id: ruff
13 changes: 7 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

## django-fsm unreleased

- Add support for django 5.0
- Remove support for django < 3.2
- Add support for python 3.12
- Add support for python 3.11

- Drop support for Python < 3.7.
- Enable Github actions for testing
- Add support for django 4.2
- Add support for python 3.11
- Add support for python 3.12
- Drop support for django < 3.2
- Add support for django 4.2
- Add support for django 5.0
- Enable Github actions for testing
- Remove South support...if exists

## django-fsm 2.8.1 2022-08-15

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ django-fsm unreleased
- Add support for django 4.2
- Add support for django 5.0
- Enable Github actions for testing
- Remove South support...if exists

django-fsm 2.8.1 2022-08-15
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
30 changes: 5 additions & 25 deletions django_fsm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,13 @@
from functools import partialmethod, wraps

import django
from django.apps import apps as django_apps
from django.db import models
from django.db.models import Field
from django.db.models.query_utils import DeferredAttribute
from django.db.models.signals import class_prepared
from django_fsm.signals import pre_transition, post_transition

try:
from django.apps import apps as django_apps

def get_model(app_label, model_name):
app = django_apps.get_app_config(app_label)
return app.get_model(model_name)


except ImportError:
from django.db.models.loading import get_model
from django_fsm.signals import pre_transition, post_transition


__all__ = [
Expand All @@ -38,16 +29,6 @@ def get_model(app_label, model_name):
"RETURN_VALUE",
]

# South support; see http://south.aeracode.org/docs/tutorial/part4.html#simple-inheritance
try:
from south.modelsinspector import add_introspection_rules
except ImportError:
pass
else:
add_introspection_rules([], [r"^django_fsm\.FSMField"])
add_introspection_rules([], [r"^django_fsm\.FSMIntegerField"])
add_introspection_rules([], [r"^django_fsm\.FSMKeyField"])


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

model = get_model(app_label, model_name)
model = django_apps.get_app_config(app_label).get_model(model_name)

if model is None:
raise ValueError(f"No model found {state_proxy}")

Expand Down Expand Up @@ -385,9 +367,7 @@ def contribute_to_class(self, cls, name, **kwargs):
super().contribute_to_class(cls, name, **kwargs)
setattr(cls, self.name, self.descriptor_class(self))
setattr(cls, f"get_all_{self.name}_transitions", partialmethod(get_all_FIELD_transitions, field=self))
setattr(
cls, f"get_available_{self.name}_transitions", partialmethod(get_available_FIELD_transitions, field=self)
)
setattr(cls, f"get_available_{self.name}_transitions", partialmethod(get_available_FIELD_transitions, field=self))
setattr(
cls,
f"get_available_user_{self.name}_transitions",
Expand Down
122 changes: 28 additions & 94 deletions django_fsm/management/commands/graph_transitions.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
import graphviz
from optparse import make_option
from itertools import chain

from django.apps import apps
from django.core.management.base import BaseCommand
try:
from django.utils.encoding import force_str
_requires_system_checks = True
except ImportError: # Django >= 4.0
from django.utils.encoding import force_str as force_text
from django.core.management.base import ALL_CHECKS
_requires_system_checks = ALL_CHECKS
from django.utils.encoding import force_str

from django_fsm import FSMFieldMixin, GET_STATE, RETURN_VALUE

try:
from django.db.models import get_apps, get_app, get_models, get_model

NEW_META_API = False
except ImportError:
from django.apps import apps

NEW_META_API = True

from django import VERSION

HAS_ARGPARSE = VERSION >= (1, 10)


def all_fsm_fields_data(model):
if NEW_META_API:
return [(field, model) for field in model._meta.get_fields() if isinstance(field, FSMFieldMixin)]
else:
return [(field, model) for field in model._meta.fields if isinstance(field, FSMFieldMixin)]
return [(field, model) for field in model._meta.get_fields() if isinstance(field, FSMFieldMixin)]


def node_name(field, state):
Expand All @@ -40,7 +18,7 @@ def node_name(field, state):


def node_label(field, state):
if type(state) == int or (type(state) == bool and hasattr(field, "choices")):
if isinstance(state, int) or (isinstance(state, bool) and hasattr(field, "choices")):
return force_str(dict(field.choices).get(state))
else:
return state
Expand Down Expand Up @@ -137,54 +115,26 @@ def get_graphviz_layouts():


class Command(BaseCommand):
requires_system_checks = _requires_system_checks

if not HAS_ARGPARSE:
option_list = BaseCommand.option_list + (
make_option(
"--output",
"-o",
action="store",
dest="outputfile",
help=(
"Render output file. Type of output dependent on file extensions. " "Use png or jpg to render graph to image."
),
),
# NOQA
make_option(
"--layout",
"-l",
action="store",
dest="layout",
default="dot",
help=("Layout to be used by GraphViz for visualization. " "Layouts: %s." % " ".join(get_graphviz_layouts())),
),
)
args = "[appname[.model[.field]]]"
else:

def add_arguments(self, parser):
parser.add_argument(
"--output",
"-o",
action="store",
dest="outputfile",
help=(
"Render output file. Type of output dependent on file extensions. " "Use png or jpg to render graph to image."
),
)
parser.add_argument(
"--layout",
"-l",
action="store",
dest="layout",
default="dot",
help=("Layout to be used by GraphViz for visualization. " "Layouts: %s." % " ".join(get_graphviz_layouts())),
)
parser.add_argument("args", nargs="*", help=("[appname[.model[.field]]]"))

help = "Creates a GraphViz dot file with transitions for selected fields"

def add_arguments(self, parser):
parser.add_argument(
"--output",
"-o",
action="store",
dest="outputfile",
help=("Render output file. Type of output dependent on file extensions. " "Use png or jpg to render graph to image."),
)
parser.add_argument(
"--layout",
"-l",
action="store",
dest="layout",
default="dot",
help=("Layout to be used by GraphViz for visualization. " "Layouts: %s." % " ".join(get_graphviz_layouts())),
)
parser.add_argument("args", nargs="*", help=("[appname[.model[.field]]]"))

def render_output(self, graph, **options):
filename, format = options["outputfile"].rsplit(".", 1)

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

if len(field_spec) == 1:
if NEW_META_API:
app = apps.get_app(field_spec[0])
models = apps.get_models(app)
else:
app = get_app(field_spec[0])
models = get_models(app)
app = apps.get_app(field_spec[0])
models = apps.get_models(app)
for model in models:
fields_data += all_fsm_fields_data(model)
elif len(field_spec) == 2:
if NEW_META_API:
model = apps.get_model(field_spec[0], field_spec[1])
else:
model = get_model(field_spec[0], field_spec[1])
model = apps.get_model(field_spec[0], field_spec[1])
fields_data += all_fsm_fields_data(model)
elif len(field_spec) == 3:
if NEW_META_API:
model = apps.get_model(field_spec[0], field_spec[1])
else:
model = get_model(field_spec[0], field_spec[1])
model = apps.get_model(field_spec[0], field_spec[1])
fields_data += all_fsm_fields_data(model)
else:
if NEW_META_API:
for model in apps.get_models():
fields_data += all_fsm_fields_data(model)
else:
for app in get_apps():
for model in get_models(app):
fields_data += all_fsm_fields_data(model)

for model in apps.get_models():
fields_data += all_fsm_fields_data(model)
dotdata = generate_dot(fields_data)

if options["outputfile"]:
Expand Down
5 changes: 2 additions & 3 deletions django_fsm/tests/test_abstract_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class AnotherFromAbstractModel(BaseAbstractModel):
inherit from a shared abstract class (example: BaseAbstractModel).
Don't try to remove it.
"""

@transition(field="state", source="published", target="sticked")
def stick(self):
pass
Expand Down Expand Up @@ -53,6 +54,4 @@ def test_field_available_transitions_works(self):

def test_field_all_transitions_works(self):
transitions = self.model.get_all_state_transitions()
self.assertEqual(
{("new", "published"), ("published", "sticked")}, {(data.source, data.target) for data in transitions}
)
self.assertEqual({("new", "published"), ("published", "sticked")}, {(data.source, data.target) for data in transitions})
30 changes: 15 additions & 15 deletions django_fsm/tests/test_basic_transitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ def test_available_conditions_from_published(self):
transitions = self.model.get_available_state_transitions()
actual = {(transition.source, transition.target) for transition in transitions}
expected = {
("*", "moderated"),
("published", None),
("published", "hidden"),
("published", "stolen"),
("*", ""),
("+", "blocked"),
("*", "moderated"),
("published", None),
("published", "hidden"),
("published", "stolen"),
("*", ""),
("+", "blocked"),
}
self.assertEqual(actual, expected)

Expand Down Expand Up @@ -221,14 +221,14 @@ def test_all_conditions(self):

actual = {(transition.source, transition.target) for transition in transitions}
expected = {
("*", "moderated"),
("new", "published"),
("new", "removed"),
("published", None),
("published", "hidden"),
("published", "stolen"),
("hidden", "stolen"),
("*", ""),
("+", "blocked"),
("*", "moderated"),
("new", "published"),
("new", "removed"),
("published", None),
("published", "hidden"),
("published", "stolen"),
("hidden", "stolen"),
("*", ""),
("+", "blocked"),
}
self.assertEqual(actual, expected)
4 changes: 1 addition & 3 deletions django_fsm/tests/test_proxy_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,4 @@ def test_field_all_transitions_base_model(self):

def test_field_all_transitions_works(self):
transitions = self.model.get_all_state_transitions()
self.assertEqual(
{("new", "published"), ("published", "sticked")}, {(data.source, data.target) for data in transitions}
)
self.assertEqual({("new", "published"), ("published", "sticked")}, {(data.source, data.target) for data in transitions})
Loading

0 comments on commit 245dc0c

Please sign in to comment.