Skip to content

Commit

Permalink
add PreprintDraftSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed Aug 21, 2024
1 parent d3b7da2 commit ca3033e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
37 changes: 37 additions & 0 deletions api/preprints/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,43 @@ def to_internal_value(self, license_id):
raise exceptions.NotFound('Unable to find specified license.')


class PreprintDraftSerializer(JSONAPISerializer):
filterable_fields = frozenset([
'id',
'date_created',
'date_modified',
'date_published',
'original_publication_date',
'provider',
'reviews_state',
])
available_metrics = frozenset([
'downloads',
'views',
])

id = IDField(source='_id', read_only=True)
type = TypeField()
title = ser.CharField(required=True, max_length=512)
description = ser.CharField(required=False, allow_blank=True, allow_null=True)
date_created = VersionedDateTimeField(source='created', read_only=True)
date_modified = VersionedDateTimeField(source='modified', read_only=True)
date_published = VersionedDateTimeField(read_only=True)
original_publication_date = VersionedDateTimeField(required=False, allow_null=True)
provider = PreprintProviderRelationshipField(
related_view='providers:preprint-providers:preprint-provider-detail',
related_view_kwargs={'provider_id': '<provider._id>'},
read_only=False,
)
reviews_state = ser.CharField(
source='machine_state',
read_only=True,
max_length=15,
)




class PreprintSerializer(TaxonomizableSerializerMixin, MetricsSerializerMixin, JSONAPISerializer):
filterable_fields = frozenset([
'id',
Expand Down
2 changes: 1 addition & 1 deletion api/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
re_path(r'^(?P<user_id>\w+)/nodes/$', views.UserNodes.as_view(), name=views.UserNodes.view_name),
re_path(r'^(?P<user_id>\w+)/groups/$', views.UserGroups.as_view(), name=views.UserGroups.view_name),
re_path(r'^(?P<user_id>\w+)/preprints/$', views.UserPreprints.as_view(), name=views.UserPreprints.view_name),
re_path(r'^(?P<user_id>\w+)/draft_preprints/$', views.UserPreprints.as_view(), name=views.UserPreprints.view_name),
re_path(r'^(?P<user_id>\w+)/draft_preprints/$', views.UserDraftPreprints.as_view(), name=views.UserDraftPreprints.view_name),
re_path(r'^(?P<user_id>\w+)/registrations/$', views.UserRegistrations.as_view(), name=views.UserRegistrations.view_name),
re_path(r'^(?P<user_id>\w+)/settings/$', views.UserSettings.as_view(), name=views.UserSettings.view_name),
re_path(r'^(?P<user_id>\w+)/quickfiles/$', views.UserQuickFiles.as_view(), name=views.UserQuickFiles.view_name),
Expand Down
9 changes: 4 additions & 5 deletions api/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from api.nodes.serializers import DraftRegistrationLegacySerializer
from api.nodes.utils import NodeOptimizationMixin
from api.osf_groups.serializers import GroupSerializer
from api.preprints.serializers import PreprintSerializer
from api.preprints.serializers import PreprintSerializer, PreprintDraftSerializer
from api.registrations import annotations as registration_annotations
from api.registrations.serializers import RegistrationSerializer
from api.resources import annotations as resource_annotations
Expand Down Expand Up @@ -423,14 +423,13 @@ class UserDraftPreprints(JSONAPIBaseView, generics.ListAPIView, UserMixin, Prepr
)

ordering = ('-created')
model_class = AbstractNode

required_read_scopes = [CoreScopes.USERS_READ, CoreScopes.NODE_PREPRINTS_READ]
required_write_scopes = [CoreScopes.USERS_WRITE, CoreScopes.NODE_PREPRINTS_WRITE]

serializer_class = PreprintSerializer
serializer_class = PreprintDraftSerializer
view_category = 'users'
view_name = 'user-preprints'
view_name = 'user-draft-preprints'

def get_default_queryset(self):
# the user who is requesting
Expand All @@ -443,7 +442,7 @@ def get_default_queryset(self):
return self.preprints_queryset(
Preprint.objects.filter(
_contributors__guids___id=target_user._id,
machine_state='initial'
machine_state='initial',
),
auth_user,
allow_contribs=False
Expand Down
20 changes: 10 additions & 10 deletions api_tests/users/views/test_user_draft_preprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def abandoned_public_preprint(self, admin, provider, subject, public_project):
machine_state='initial'
)

def test_authorized_in_gets_200(self, app, admin, preprint):
def test_authorized_in_gets_200(self, app, admin, abandoned_public_preprint, abandoned_private_preprint):
url = f'/{API_BASE}users/{admin._id}/draft_preprints/'
res = app.get(url, auth=admin.auth)
assert res.status_code == 200
Expand All @@ -118,31 +118,31 @@ def test_anonymous_gets_401(self, app, admin):
res = app.get(url)
assert res.status_code == 401

def test_get_preprints_403(self, app, admin, non_contrib, preprint):
def test_get_preprints_403(self, app, admin, non_contrib, abandoned_public_preprint, abandoned_private_preprint):
url = f'/{API_BASE}users/{admin._id}/draft_preprints/'
res = app.get(url, auth=non_contrib.auth)
assert res.status_code == 401

def test_get_projects_not_logged_in(self, app, preprint, admin, project_public, project_private):
def test_get_projects_not_logged_in(self, app, abandoned_public_preprint, abandoned_private_preprint, admin):
res = app.get(f'/{API_BASE}users/{admin._id}/draft_preprints/')
node_json = res.json['data']

ids = [each['id'] for each in node_json]
assert preprint._id in ids
assert project_public._id not in ids
assert project_private._id not in ids
assert abandoned_public_preprint._id in ids
assert abandoned_public_preprint._id not in ids
assert abandoned_public_preprint._id not in ids

def test_get_projects_logged_in_as_different_user(self, app, admin, write_contrib, preprint, project_public, project_private):
def test_get_projects_logged_in_as_different_user(self, app, admin, write_contrib, abandoned_public_preprint):
res = app.get(
f'/{API_BASE}users/{admin._id}/draft_preprints/',
auth=write_contrib.auth
)
node_json = res.json['data']

ids = [each['id'] for each in node_json]
assert preprint._id in ids
assert project_public._id not in ids
assert project_private._id not in ids
assert abandoned_public_preprint._id in ids
assert abandoned_public_preprint._id not in ids
assert abandoned_public_preprint._id not in ids

def test_abandoned_preprint_in_results(self, app, admin, abandoned_public_preprint, abandoned_private_preprint, published_preprint):
res = app.get(
Expand Down

0 comments on commit ca3033e

Please sign in to comment.