Skip to content

Commit

Permalink
fix more of preprints machine
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed Jul 16, 2024
1 parent 47bf2c5 commit d7197d7
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 189 deletions.
5 changes: 3 additions & 2 deletions api/actions/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from api.base.utils import get_user_auth
from osf.models import CollectionSubmissionAction
from osf.models.action import BaseAction
from osf.models.mixins import PreprintStateMachineMixin, ReviewProviderMixin
from osf.models.mixins import ReviewProviderMixin
from osf.models.preprint import Preprint
from osf.utils.workflows import PreprintStateTriggers
from osf.utils import permissions as osf_permissions

Expand All @@ -30,7 +31,7 @@ def has_object_permission(self, request, view, obj):
if isinstance(obj, tuple(BaseAction.__subclasses__())):
target = obj.target
provider = target.provider
elif isinstance(obj, PreprintStateMachineMixin):
elif isinstance(obj, Preprint):
target = obj
provider = target.provider
elif isinstance(obj, ReviewProviderMixin):
Expand Down
44 changes: 30 additions & 14 deletions api/actions/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,36 @@ def create(self, validated_data):
comment = validated_data.get('comment', '')
permissions = validated_data.get('permissions', '')
visible = validated_data.get('visible', '')
try:
if trigger == DefaultTriggers.ACCEPT.db_name:
return target.run_accept(user=user, comment=comment, permissions=permissions, visible=visible)
if trigger == DefaultTriggers.REJECT.db_name:
return target.run_reject(user, comment)
if trigger == DefaultTriggers.EDIT_COMMENT.db_name:
return target.run_edit_comment(user, comment)
if trigger == DefaultTriggers.SUBMIT.db_name:
return target.run_submit(user)
except InvalidTriggerError as e:
# Invalid transition from the current state
raise Conflict(str(e))
if isinstance(target, Preprint):
try:
if trigger == PreprintStateTriggers.ACCEPT.db_name:
return target.accept(user=user, comment=comment, permissions=permissions, visible=visible)
if trigger == PreprintStateTriggers.REJECT.db_name:
return target.reject(user, comment)
if trigger == PreprintStateTriggers.EDIT_COMMENT.db_name:
return target.edit_comment(user, comment)
if trigger == PreprintStateTriggers.SUBMIT.db_name:
return target.submit(user)
except InvalidTriggerError as e:
# Invalid transition from the current state
raise Conflict(str(e))
else:
raise JSONAPIAttributeException(attribute='trigger', detail='Invalid trigger.')
else:
raise JSONAPIAttributeException(attribute='trigger', detail='Invalid trigger.')
try:
if trigger == DefaultTriggers.ACCEPT.db_name:
return target.run_accept(user=user, comment=comment, permissions=permissions, visible=visible)
if trigger == DefaultTriggers.REJECT.db_name:
return target.run_reject(user, comment)
if trigger == DefaultTriggers.EDIT_COMMENT.db_name:
return target.run_edit_comment(user, comment)
if trigger == DefaultTriggers.SUBMIT.db_name:
return target.run_submit(user)
except InvalidTriggerError as e:
# Invalid transition from the current state
raise Conflict(str(e))
else:
raise JSONAPIAttributeException(attribute='trigger', detail='Invalid trigger.')

class Meta:
type_ = 'actions'
Expand Down Expand Up @@ -217,7 +233,7 @@ def create(self, validated_data):
target = validated_data.pop('target')
comment = validated_data.pop('comment', '')
try:
return target.run_withdraw(user=user, comment=comment)
return target.withdraw(user=user, comment=comment)
except InvalidTriggerError as e:
# Invalid transition from the current state
raise Conflict(str(e))
Expand Down
1 change: 0 additions & 1 deletion api/preprints/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def has_object_permission(self, request, view, obj):
return False



class PreprintPublishedOrAdmin(permissions.BasePermission):

acceptable_models = (Preprint,)
Expand Down
10 changes: 2 additions & 8 deletions api/preprints/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from distutils.version import StrictVersion

from rest_framework import generics
from rest_framework.exceptions import MethodNotAllowed, NotFound, PermissionDenied, NotAuthenticated
from rest_framework.exceptions import MethodNotAllowed, NotFound
from rest_framework import permissions as drf_permissions

from framework.auth.oauth_scopes import CoreScopes
Expand Down Expand Up @@ -123,11 +123,7 @@ def get_default_queryset(self):

# Permissions on the list objects are handled by the query
public_only = self.metrics_requested
from django.db.models import Q
queryset_id = self.preprints_queryset(Preprint.objects.all(), auth_user, public_only=public_only).values_list('id', flat=True)
queryset = Preprint.objects.filter(
id__in=queryset_id
).exclude(machine_state='initial')
queryset = self.preprints_queryset(Preprint.objects.all(), auth_user, public_only=public_only)
# Use get_metrics_queryset to return an queryset with annotated metrics
# iff ?metrics query param is present
if self.metrics_requested:
Expand Down Expand Up @@ -252,7 +248,6 @@ def get_object(self):
return preprint.csl



class PreprintCitationStyleDetail(JSONAPIBaseView, generics.RetrieveAPIView, PreprintMixin):
"""The documentation for this endpoint can be found [here](https://developer.osf.io/#operation/preprints_citation_read).
"""
Expand Down Expand Up @@ -282,7 +277,6 @@ def get_object(self):
return {'citation': citation, 'id': style}



class PreprintIdentifierList(IdentifierList, PreprintMixin):
"""List of identifiers for a specified preprint. *Read-only*.
Expand Down
12 changes: 7 additions & 5 deletions api_tests/preprints/views/test_preprint_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,12 @@ def expected_reviewables(self, user):
is_public=True)), PreprintFactory(
is_published=False, project=ProjectFactory(
is_public=True)), ]
preprints[0].run_submit(user)
preprints[0].run_accept(user, 'comment')
preprints[1].run_submit(user)
preprints[1].run_reject(user, 'comment')
preprints[2].run_submit(user)

preprints[0].submit(user)
preprints[0].accept(user, 'comment')
preprints[1].submit(user)
preprints[1].reject(user, 'comment')
preprints[2].submit(user)
return preprints

@pytest.fixture
Expand Down Expand Up @@ -983,6 +984,7 @@ def test_unpublished_not_visible_to_admins(
preprint_unpublished,
preprint_published,
url):

res = app.get(url, auth=user_admin_contrib.auth)
assert len(res.json['data']) == 1
assert preprint_unpublished._id not in [d['id'] for d in res.json['data']]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ def expected_reviewables(self, provider, user):
is_published=False,
provider=provider,
project=ProjectFactory(is_public=True)), ]
preprints[0].run_submit(user)
preprints[0].run_accept(user, 'comment')
preprints[1].run_submit(user)
preprints[2].run_submit(user)
preprints[0].submit(user)
preprints[0].accept(user, 'comment')
preprints[1].submit(user)
preprints[2].submit(user)
return preprints

@pytest.fixture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def withdraw_all_preprints(provider_id, page_size, user_guid, comment=None):
preprints_withdrawn = 0

for preprint in preprints:
preprint.run_withdraw(user, comment)
preprint.withdraw(user, comment)
preprint.reload()
assert preprint.is_retracted
preprints_withdrawn += 1
Expand Down
30 changes: 1 addition & 29 deletions osf/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.utils.functional import cached_property
from guardian.shortcuts import assign_perm, get_perms, remove_perm, get_group_perms

from api.providers.workflows import Workflows, PUBLIC_STATES
from api.providers.workflows import Workflows
from framework import status
from framework.auth import Auth
from framework.auth.core import get_user
Expand All @@ -35,7 +35,6 @@
from osf.utils.fields import NonNaiveDateTimeField
from osf.utils.datetime_aware_jsonfield import DateTimeAwareJSONField
from osf.utils.machines import (
PreprintStateMachine,
NodeRequestMachine,
PreprintRequestMachine,
)
Expand All @@ -46,7 +45,6 @@
DefaultStates,
DefaultTriggers,
PreprintStates,
PreprintStateTriggers,
)

from osf.utils.requests import get_request_and_user_id
Expand Down Expand Up @@ -854,32 +852,6 @@ class Meta:
MachineClass = PreprintRequestMachine


class PreprintStateMachineMixin(MachineableMixin):
"""Something that may be included in a reviewed collection and is subject to a reviews workflow.
"""
TriggersClass = PreprintStateTriggers
MachineClass = PreprintStateMachine
machine_state = models.CharField(
max_length=15,
db_index=True,
choices=PreprintStates.char_field_choices(),
default=PreprintStates.INITIAL.db_name
)

class Meta:
abstract = True

@property
def in_public_reviews_state(self):
public_states = PUBLIC_STATES.get(self.provider.reviews_workflow)
if not public_states:
return False
return self.machine_state in public_states

def run_withdraw(self, user, comment):
return self.validate_transition('withdraw', user, comment=comment)


class GuardianMixin(models.Model):
""" Helper for managing object-level permissions with django-guardian
Expects:
Expand Down
Loading

0 comments on commit d7197d7

Please sign in to comment.