Skip to content

Commit

Permalink
Improve home view script sorting (#391)
Browse files Browse the repository at this point in the history
* Improve home view script sorting

* Fix sorting, add test
  • Loading branch information
Chris7 authored Nov 21, 2023
1 parent 309c514 commit 1543182
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
40 changes: 40 additions & 0 deletions wooey/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.test import TestCase, RequestFactory
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.contrib.contenttypes.models import ContentType
from django.http import Http404
from django.urls import reverse

Expand Down Expand Up @@ -471,3 +472,42 @@ def test_show_settings_if_logged_in_user(self):
view = wooey_views.WooeyProfileView.as_view()
response = view(request, username=user.username)
self.assertTrue(response.context_data["is_logged_in_user"])


class TestHomeView(mixins.ScriptFactoryMixin, mixins.FileCleanupMixin, TestCase):
def test_sorts_scripts_by_name_and_favorite(self):
request_factory = RequestFactory()
user = factories.UserFactory()
url = reverse("wooey:wooey_home")
request = request_factory.get(url)
request.user = user

# by default, we sort by name
view = wooey_views.WooeyHomeView.as_view()
response = view(request)

sorted_scripts = sorted(
models.Script.objects.all(), key=lambda x: x.script_name
)
self.assertEqual(response.context_data["scripts"], sorted_scripts)

ctype = ContentType.objects.get_for_model(models.Script)
models.Favorite(
content_type=ctype, user=user, object_id=self.translate_script.script.id
).save()

# assert this script isn't naturally the first one
self.assertNotEqual(sorted_scripts[0].id, self.translate_script.script.id)

view = wooey_views.WooeyHomeView.as_view()
response = view(request)

self.assertTrue(
response.context_data["scripts"][0].id, self.translate_script.script.id
)

# the rest after the favorite are sorted alphabetically
self.assertEqual(
response.context_data["scripts"][1:],
[i for i in sorted_scripts if i.id != self.translate_script.script.id],
)
10 changes: 9 additions & 1 deletion wooey/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,17 @@ def get_context_data(self, **kwargs):
if self.request.user.is_authenticated:
# Get the id of every favorite (scrapbook) file
ctype = ContentType.objects.get_for_model(Script)
ctx["favorite_script_ids"] = Favorite.objects.filter(
favorite_scripts = Favorite.objects.filter(
content_type=ctype, user__id=self.request.user.id
).values_list("object_id", flat=True)
ctx["favorite_script_ids"] = favorite_scripts
# put favorite scripts at the top of the sort order
ctx["scripts"] = sorted(
ctx["scripts"],
# we do the `not` so we can sort in ascending order for both the
# favorite status and get alphabetical sorting
key=lambda x: (x.id not in favorite_scripts, x.script_name),
)
else:
ctx["favorite_script_ids"] = []

Expand Down

0 comments on commit 1543182

Please sign in to comment.