Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENG-6852] Optimize latest_only filter for Preprint #10907

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/base/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ def preprints_queryset(self, base_queryset, auth_user, allow_contribs=True, publ
public_only=public_only,
)
if latest_only:
preprints = preprints.filter(pk__in=[obj.pk for obj in preprints if obj.is_latest_version])
preprints = preprints.filter(guids__isnull=False)
return preprints


Expand Down
2 changes: 1 addition & 1 deletion api/collections/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def collection_preprints(self, collection, user, latest_only=False):
user=user,
)
if latest_only:
preprints = preprints.filter(pk__in=[obj.pk for obj in preprints if obj.is_latest_version])
preprints = preprints.filter(guids__isnull=False)
return preprints

def get_collection_submission(self, check_object_permissions=True):
Expand Down
2 changes: 1 addition & 1 deletion api_tests/nodes/views/test_node_preprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def test_unpublished_visible_to_admins(
preprint_unpublished,
preprint_published, url):
res = app.get(url, auth=user_admin_contrib.auth)
assert len(res.json['data']) == 1
assert len(res.json['data']) == 2
assert preprint_published._id in [d['id'] for d in res.json['data']]

def test_unpublished_invisible_to_write_contribs(
Expand Down
3 changes: 2 additions & 1 deletion api_tests/preprints/views/test_preprint_list_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ def test_filter_published_false_admin(
res = app.get(
f'{url}filter[is_published]=false',
auth=user_admin_contrib.auth)
assert len(res.json['data']) == 0
assert len(res.json['data']) == 1
assert preprint_unpublished._id in [d['id'] for d in res.json['data']]


@pytest.mark.django_db
Expand Down
4 changes: 1 addition & 3 deletions osf/models/preprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,7 @@ def csl(self): # formats node information into CSL format for citation parsing

@property
def is_latest_version(self):
if not self.date_published:
return False
return self.versioned_guids.first().guid.referent.version == self.version
return self.guids.exists()

def get_preprint_versions(self):
guids = self.versioned_guids.first().guid.versions.all()
Expand Down
Loading