Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Fix some pylint reported warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kmmbvnr committed May 30, 2010
1 parent 1ce74ee commit b644b51
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
6 changes: 4 additions & 2 deletions .pylintrc
Expand Up @@ -3,7 +3,9 @@ persistent=yes

[MESSAGES CONTROL]
# C0111 = Missing docstring
disable=
# I0011 = # Warning locally suppressed using disable-msg
# I0012 = # Warning locally suppressed using disable-msg
disable=I0011,I0012

[REPORTS]
output-format=parseable
Expand Down Expand Up @@ -56,6 +58,6 @@ max-branchs=12
max-statements=50
max-parents=7
max-attributes=7
min-public-methods=2
min-public-methods=0
max-public-methods=20

39 changes: 31 additions & 8 deletions django_fsm/db/fields/fsmfield.py
@@ -1,35 +1,58 @@
# -*- coding: utf-8 -*-
"""
State tracking functionality for django models
"""
from collections import defaultdict
from functools import wraps
from django.db import models

class FSMMeta(object):
"""
Models methods transitions meta information
"""
def __init__(self):
self.transitions = defaultdict()

def get_state_field(self, instance):

@staticmethod
def _get_state_field(instance):
"""
Lookup for FSMField in django model instance
"""
fields = [field for field in instance._meta.fields if isinstance(field, FSMField)]
found = len(fields)
if found == 0:
raise TypeError("No FSMField found in model")
elif found > 1:
raise TypeError("More than one FSMField found in model, please specify field name in transition decorator")
return fields[0]

def current_state(self, instance):
field_name = self.get_state_field(instance).name

@staticmethod
def current_state(instance):
"""
Return current state of Django model
"""
field_name = FSMMeta._get_state_field(instance).name
return getattr(instance, field_name)

def has_transition(self, instance):
return self.transitions.has_key(self.current_state(instance))
"""
Lookup is any transition exists from current model state
"""
return self.transitions.has_key(FSMMeta.current_state(instance))

def to_next_state(self, instance):
field_name = self.get_state_field(instance).name
"""
Switch to next state
"""
field_name = FSMMeta._get_state_field(instance).name
curr_state = getattr(instance, field_name)
setattr(instance, field_name, self.transitions[curr_state])


def transition(source='*', target=None, save=False):
"""
Method decorator for mark allowed transition
"""
def inner_transition(func):
if not hasattr(func, '_django_fsm'):
setattr(func, '_django_fsm', FSMMeta())
Expand All @@ -40,7 +63,7 @@ def inner_transition(func):
def _change_state(instance, *args, **kwargs):
meta = func._django_fsm
if not meta.has_transition(instance):
raise NotImplementedError("Can't switch from state '%s' using method '%s'" % (meta.current_state(instance), func.func_name))
raise NotImplementedError("Can't switch from state '%s' using method '%s'" % (FSMMeta.current_state(instance), func.func_name))

func(instance, *args, **kwargs)

Expand Down

0 comments on commit b644b51

Please sign in to comment.