Skip to content

Commit

Permalink
fix machineMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed Jul 17, 2024
1 parent 9c6a641 commit 3690750
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 48 deletions.
8 changes: 4 additions & 4 deletions api/actions/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ def create(self, validated_data):
visible = validated_data.get('visible', '')
try:
if trigger == DefaultTriggers.ACCEPT.db_name:
return target.run_accept(user=user, comment=comment, permissions=permissions, visible=visible)
target.accept(user=user, comment=comment, permissions=permissions, visible=visible)
if trigger == DefaultTriggers.REJECT.db_name:
return target.run_reject(user, comment)
target.reject(user=user, comment=comment)
if trigger == DefaultTriggers.EDIT_COMMENT.db_name:
return target.run_edit_comment(user, comment)
target.edit_comment(user=user, comment=comment)
if trigger == DefaultTriggers.SUBMIT.db_name:
return target.run_submit(user)
target.submit(user=user, comment=comment)
except InvalidTriggerError as e:
# Invalid transition from the current state
raise Conflict(str(e))
Expand Down
64 changes: 35 additions & 29 deletions osf/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,11 +786,18 @@ def get_extra_log_params(self, comment):


class MachineableMixin(models.Model):
TriggersClass = DefaultTriggers

class Meta:
abstract = True

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.state_machine = self.MachineClass(
model=self,
active_state=self.state,
state_property_name='state'
)

machine_state = models.CharField(
max_length=15,
db_index=True,
Expand All @@ -799,35 +806,21 @@ class Meta:
)
date_last_transitioned = models.DateTimeField(null=True, blank=True, db_index=True)

def validate_transition(self, transition_name, user, **kwargs):
"""
Run the specified state transition and create a corresponding Action.
Params:
transition_name: The name of the transition to run.
user: The user triggering this transition.
kwargs: Additional parameters required by the transition.
"""
machine = self.MachineClass(self, 'machine_state')
transition = getattr(machine, transition_name)(user=user, **kwargs)

if not transition:
valid_triggers = machine.get_triggers(self.machine_state)
raise InvalidTriggerError(transition_name, self.machine_state, valid_triggers)

return machine.action

def run_submit(self, user):
return self.validate_transition('submit', user=user)
@property
def MachineClass(self):
raise NotImplementedError()

def run_accept(self, user, comment, **kwargs):
return self.validate_transition('accept', user=user, comment=comment, **kwargs)
@property
def States(self):
raise NotImplementedError()

def run_reject(self, user, comment):
return self.validate_transition('reject', user=user, comment=comment)
@property
def state(self):
return self.States(self.machine_state)

def run_edit_comment(self, user, comment):
return self.validate_transition('edit_comment', user=user, comment=comment)
@state.setter
def state(self, new_state):
self.machine_state = new_state.db_name


class NodeRequestableMixin(MachineableMixin):
Expand All @@ -838,7 +831,14 @@ class NodeRequestableMixin(MachineableMixin):
class Meta:
abstract = True

MachineClass = NodeRequestMachine
@property
def MachineClass(self):
return NodeRequestMachine

@property
def States(self):
return DefaultStates



class PreprintRequestableMixin(MachineableMixin):
Expand All @@ -849,7 +849,13 @@ class PreprintRequestableMixin(MachineableMixin):
class Meta:
abstract = True

MachineClass = PreprintRequestMachine
@property
def MachineClass(self):
return PreprintRequestMachine

@property
def States(self):
return DefaultStates


class GuardianMixin(models.Model):
Expand Down
20 changes: 10 additions & 10 deletions osf/models/preprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

from .base import BaseModel, GuidMixin, GuidMixinQuerySet
from .identifiers import IdentifierMixin, Identifier
from .mixins import TaxonomizableMixin, ContributorMixin, SpamOverrideMixin, TitleMixin, DescriptionMixin
from .mixins import TaxonomizableMixin, ContributorMixin, SpamOverrideMixin, TitleMixin, DescriptionMixin, MachineableMixin
from addons.osfstorage.models import OsfStorageFolder, Region, BaseFileNode, OsfStorageFile

from framework.sentry import log_exception
Expand Down Expand Up @@ -110,25 +110,25 @@ def can_view(self, base_queryset=None, user=None, allow_contribs=True, public_on
return ret.distinct('id', 'created') if include_non_public else ret


class Preprint(DirtyFieldsMixin, GuidMixin, IdentifierMixin, BaseModel, TitleMixin, DescriptionMixin,
class Preprint(DirtyFieldsMixin, GuidMixin, MachineableMixin, IdentifierMixin, BaseModel, TitleMixin, DescriptionMixin,
Loggable, Taggable, ContributorMixin, GuardianMixin, SpamOverrideMixin, TaxonomizableMixin):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@property
def MachineClass(self):
from osf.utils.machines import PreprintStateMachine
self.state_machine = PreprintStateMachine(
model=self,
active_state=self.state,
state_property_name='state'
)
raise PreprintStateMachine

@property
def States(self):
from osf.utils.workflows import PreprintStates
return PreprintStates

machine_state = models.CharField(
max_length=15,
db_index=True,
choices=PreprintStates.char_field_choices(),
default=PreprintStates.INITIAL.db_name
)
date_last_transitioned = models.DateTimeField(null=True, blank=True, db_index=True)

def _validate_state(self, ev):
from django.core.exceptions import ValidationError
Expand Down
10 changes: 5 additions & 5 deletions osf/utils/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,32 +268,32 @@ class DefaultTriggers(ModerationEnum):

DEFAULT_TRANSITIONS = [
{
'trigger': 'submit',
'trigger': 'run_submit',
'source': [DefaultStates.INITIAL.db_name],
'dest': DefaultStates.PENDING.db_name,
'after': ['save_action', 'update_last_transitioned', 'save_changes', 'notify_submit'],
},
{
'trigger': 'submit',
'trigger': 'run_submit',
'source': [DefaultStates.PENDING.db_name, DefaultStates.REJECTED.db_name],
'conditions': 'resubmission_allowed',
'dest': DefaultStates.PENDING.db_name,
'after': ['save_action', 'update_last_transitioned', 'save_changes', 'notify_resubmit'],
},
{
'trigger': 'accept',
'trigger': 'run_accept',
'source': [DefaultStates.PENDING.db_name, DefaultStates.REJECTED.db_name],
'dest': DefaultStates.ACCEPTED.db_name,
'after': ['save_action', 'update_last_transitioned', 'save_changes', 'notify_accept_reject'],
},
{
'trigger': 'reject',
'trigger': 'run_reject',
'source': [DefaultStates.PENDING.db_name, DefaultStates.ACCEPTED.db_name],
'dest': DefaultStates.REJECTED.db_name,
'after': ['save_action', 'update_last_transitioned', 'save_changes', 'notify_accept_reject'],
},
{
'trigger': 'edit_comment',
'trigger': 'run_edit_comment',
'source': [DefaultStates.PENDING.db_name, DefaultStates.REJECTED.db_name, DefaultStates.ACCEPTED.db_name],
'dest': '=',
'after': ['save_action', 'save_changes', 'notify_edit_comment'],
Expand Down

0 comments on commit 3690750

Please sign in to comment.