Skip to content

Commit

Permalink
Merge pull request #10907 from Ostap-Zherebetskyi/fix/preprint_filters
Browse files Browse the repository at this point in the history
[ENG-6852] Optimize `latest_only` filter for `Preprint`
  • Loading branch information
cslzchen authored Jan 10, 2025
2 parents a5b4e05 + 4182a15 commit 8bc1b00
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 7 deletions.
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

0 comments on commit 8bc1b00

Please sign in to comment.