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

[IN-4593] Prepare for Django 3.2 #99

Open
wants to merge 7 commits into
base: master
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
2 changes: 1 addition & 1 deletion django_states/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
__all__ = ('StateField',)

from django.db import models
from django.utils.functional import curry
from django_states.machine import StateMachine
from functools import partial as curry

from django_states.model_methods import (get_STATE_transitions,
get_public_STATE_transitions,
Expand Down
8 changes: 4 additions & 4 deletions django_states/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
from __future__ import absolute_import

import json
import six
import sys

from django.db import models
from django.db.models.base import ModelBase
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from django.conf import settings

from django_states import conf
from django_states.fields import StateField
from django_states.machine import StateMachine, StateDefinition, StateTransition
import six
from six import python_2_unicode_compatible


def _create_state_log_model(state_model, field_name, machine):
Expand Down Expand Up @@ -85,7 +85,7 @@ def __new__(c, name, bases, attrs):

def new_unicode(self):
"""New Unicode"""
return u'%s (%s)' % (old_unicode(self), self.get_state_info().description)
return u'%s (%s)' % (old_unicode(self), self.get_state_info(self).description)

attrs['__unicode__'] = new_unicode

Expand Down Expand Up @@ -200,7 +200,7 @@ def make_transition(self, transition, user=None):
:param transition: Name of the transition
:param user: User object
"""
return self.get_state_info().make_transition(transition, user=user)
return self.get_state_info(self).make_transition(transition, user=user)

@property
def is_public(self):
Expand Down
8 changes: 4 additions & 4 deletions django_states/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.contrib import messages
from django_states.exceptions import (TransitionNotFound, TransitionValidationError,
UnknownState, TransitionException, MachineDefinitionException)
from django.utils.encoding import python_2_unicode_compatible
from six import python_2_unicode_compatible


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -245,17 +245,17 @@ def action(modeladmin, request, queryset):
for o in queryset:
get_STATE_info = getattr(o, 'get_%s_info' % field_name)
try:
get_STATE_info().test_transition(transition_name,
get_STATE_info(o).test_transition(transition_name,
request.user)
except TransitionException as e:
modeladmin.message_user(request, 'ERROR: %s on: %s' % (e.message, six.text_type(o)),
modeladmin.message_user(request, 'ERROR: %s on: %s' % (str(e), six.text_type(o)),
level=messages.ERROR)
return

# Make actual transitions
for o in queryset:
get_STATE_info = getattr(o, 'get_%s_info' % field_name)
get_STATE_info().make_transition(transition_name,
get_STATE_info(o).make_transition(transition_name,
request.user)

# Feeback
Expand Down
2 changes: 1 addition & 1 deletion django_states/model_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_public_STATE_transitions(self, field='state'):
"""
if getattr(self, '_%s_log_model' % field, None):
transitions = getattr(self, 'get_%s_transitions' % field)
return [t for t in transitions() if t.is_public and t.completed]
return [t for t in transitions(self) if t.is_public and t.completed]
else:
return []

Expand Down
16 changes: 8 additions & 8 deletions django_states/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

from django.db import models
from django.db.models.base import ModelBase
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from six import python_2_unicode_compatible

from django_states.machine import StateMachine, StateDefinition, StateTransition
from django_states.exceptions import States2Exception
Expand Down Expand Up @@ -96,21 +96,21 @@ def state_transitions(self):
"""
Wraps :meth:`django_states.model_methods.get_STATE_transitions`
"""
return self.get_state_transitions()
return self.get_state_transitions(self)

@property
def public_transitions(self):
"""
Wraps :meth:`django_states.model_methods.get_public_STATE_transitions`
"""
return self.get_public_state_transitions()
return self.get_public_state_transitions(self)

@property
def state_description(self):
"""
Gets the full description of the (current) state
"""
return six.text_type(self.get_state_info().description)
return six.text_type(self.get_state_info(self).description)

@property
def is_initial_state(self):
Expand All @@ -119,7 +119,7 @@ def is_initial_state(self):

:returns: ``True`` when the current state is the initial state
"""
return bool(self.get_state_info().initial)
return bool(self.get_state_info(self).initial)

@property
def possible_transitions(self):
Expand All @@ -128,7 +128,7 @@ def possible_transitions(self):

:returns: list of transitions which can be made from the current state
"""
return self.get_state_info().possible_transitions
return self.get_state_info(self).possible_transitions

@classmethod
def get_state_model_name(self):
Expand Down Expand Up @@ -166,7 +166,7 @@ def test_transition(self, transition, user=None):
succesfully. It will raise an ``Exception`` when this
transition is impossible or not allowed.
"""
return self.get_state_info().test_transition(transition, user=user)
return self.get_state_info(self).test_transition(transition, user=user)

def make_transition(self, transition, user=None, **kwargs):
"""
Expand All @@ -179,7 +179,7 @@ def make_transition(self, transition, user=None, **kwargs):
:param dict kwargs: the kwargs that will be passed to
:meth:`~django_states.machine.StateTransition.handler`
"""
return self.get_state_info().make_transition(transition, user=user, **kwargs)
return self.get_state_info(self).make_transition(transition, user=user, **kwargs)

@classmethod
def get_state_choices(cls):
Expand Down
20 changes: 10 additions & 10 deletions django_states/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,10 @@ def test_initial_state(self):
testclass = DjangoState2Class(field1=100, field2="LALALALALA")
testclass.save()

self.assertEqual(testclass.get_state_machine(), TestMachine)
self.assertEqual(testclass.get_state_display(), 'Starting State.')
self.assertEqual(testclass.get_state_machine(testclass), TestMachine)
self.assertEqual(testclass.get_state_display(testclass), 'Starting State.')

state_info = testclass.get_state_info()
state_info = testclass.get_state_info(testclass)

self.assertEqual(testclass.state, 'start')
self.assertTrue(state_info.initial)
Expand All @@ -389,7 +389,7 @@ def test_end_to_end(self):
testclass = DjangoState2Class(field1=100, field2="LALALALALA")
testclass.save()

state_info = testclass.get_state_info()
state_info = testclass.get_state_info(testclass)

# Verify the starting state.
self.assertEqual(testclass.state, 'start')
Expand Down Expand Up @@ -432,7 +432,7 @@ def test_invalid_user(self):

kwargs = {'transition': 'start_step_1', 'user': user}

state_info = testclass.get_state_info()
state_info = testclass.get_state_info(testclass)

self.assertRaises(PermissionDenied, state_info.make_transition, **kwargs)

Expand All @@ -441,7 +441,7 @@ def test_in_group(self):
testclass = DjangoState2Class(field1=100, field2="LALALALALA")
testclass.save()

state_info = testclass.get_state_info()
state_info = testclass.get_state_info(testclass)

self.assertTrue(state_info.in_group['states_valid_start'])
state_info.make_transition('start_step_1', user=self.superuser)
Expand All @@ -458,7 +458,7 @@ def test_unknown_transition(self):
test = DjangoState2Class(field1=100, field2="LALALALALA")
test.save()

state_info = test.get_state_info()
state_info = test.get_state_info(test)
with self.assertRaises(UnknownTransition):
state_info.make_transition('unknown_transition', user=self.superuser)

Expand Down Expand Up @@ -528,7 +528,7 @@ def test_statelog(self):
test.save(no_state_validation=False)

# Verify the starting state.
state_info = test.get_state_info()
state_info = test.get_state_info(test)
self.assertEqual(test.state, 'start')
self.assertEqual(state_info.name, test.state)
# Make transition
Expand All @@ -540,5 +540,5 @@ def test_statelog(self):
entry = StateLogModel.objects.all()[0]
self.assertTrue(entry.completed)
# We should also be able to find this via
self.assertEqual(test.get_state_transitions().count(), 1)
self.assertEqual(len(test.get_public_state_transitions()), 1)
self.assertEqual(test.get_state_transitions(test).count(), 1)
self.assertEqual(len(test.get_public_state_transitions(test)), 1)