Skip to content

Commit

Permalink
Merge be60957 into 41a258d
Browse files Browse the repository at this point in the history
  • Loading branch information
emil-balashov committed Jan 19, 2020
2 parents 41a258d + be60957 commit 10ebdc6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions django_logic/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Process(object):
permissions_class = Permissions
state_class = State
process_name = 'process'
queryset = None # It should be set up once for the main process
queryset_name = 'objects'

def __init__(self, field_name='', instance=None, state=None):
"""
Expand All @@ -41,7 +41,7 @@ def __init__(self, field_name='', instance=None, state=None):
self.state = state
elif state is None:
assert field_name and instance is not None
self.state = self.state_class(queryset=self.queryset,
self.state = self.state_class(queryset_name=self.queryset_name,
instance=instance,
field_name=field_name,
process_name=self.process_name)
Expand Down
12 changes: 7 additions & 5 deletions django_logic/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@


class State(object):
def __init__(self, instance: any, field_name: str, process_name=None, queryset=None):
def __init__(self, instance: any, field_name: str, process_name=None, queryset_name=None):
self.instance = instance
self.queryset = queryset or instance._meta.model.objects.all()
self.queryset_name = queryset_name or 'objects'
self.field_name = field_name
# save process name if access to process needed
self.process_name = process_name

def get_queryset(self):
return getattr(self.instance._meta.model, self.queryset_name).all()

def get_db_state(self):
"""
Fetches state directly from db instead of model instance.
"""
return self.queryset.values_list(self.field_name, flat=True).get(pk=self.instance.id)
return self.get_queryset().values_list(self.field_name, flat=True).get(pk=self.instance.id)

@cached_property
def cached_state(self):
Expand All @@ -26,7 +28,7 @@ def set_state(self, state):
Sets intermediate state to instance's field until transition is over.
"""
# TODO: how would it work if it's used within another transaction?
self.queryset.filter(pk=self.instance.id).update(**{self.field_name: state})
self.get_queryset().filter(pk=self.instance.id).update(**{self.field_name: state})
self.instance.refresh_from_db()

@property
Expand Down

0 comments on commit 10ebdc6

Please sign in to comment.