Skip to content

Commit d9fc275

Browse files
committed
Sort published_codelists in the same order as all_codelists
1 parent 00d3cf1 commit d9fc275

3 files changed

Lines changed: 57 additions & 13 deletions

File tree

opencodelists/tests/views/test_user.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,49 @@ def test_user_codelists_case_insensitive_sort_with_user_first_for_same_name(
184184
if codelist["codelist"].name.casefold() == "alpha list"
185185
]
186186
assert [codelist.owner for codelist in matching_codelists] == [user, organisation]
187+
188+
189+
def test_published_codelists_sorted_in_same_order_as_all_codelists(
190+
client,
191+
user,
192+
user_codelist,
193+
user_codelist_from_scratch,
194+
):
195+
update_codelist(
196+
codelist=user_codelist,
197+
owner=user,
198+
name="zeta list",
199+
slug="zeta-list",
200+
description=user_codelist.description,
201+
methodology=user_codelist.methodology,
202+
references={},
203+
signoffs={},
204+
)
205+
update_codelist(
206+
codelist=user_codelist_from_scratch,
207+
owner=user,
208+
name="ALPHA LIST",
209+
slug="alpha-list",
210+
description=user_codelist_from_scratch.description,
211+
methodology=user_codelist_from_scratch.methodology,
212+
references={},
213+
signoffs={},
214+
)
215+
save_for_review(draft=user_codelist_from_scratch.versions.first())
216+
publish_version(version=user_codelist_from_scratch.versions.last())
217+
218+
client.force_login(user)
219+
response = client.get(reverse("user", args=(user.username,)))
220+
221+
codelist_ids = {user_codelist.id, user_codelist_from_scratch.id}
222+
all_codelist_ids = [
223+
codelist["codelist"].id
224+
for codelist in response.context["all_codelists"]
225+
if codelist["codelist"].id in codelist_ids
226+
]
227+
published_codelist_ids = [
228+
version.codelist.id
229+
for version in response.context["published_codelists"]
230+
if version.codelist.id in codelist_ids
231+
]
232+
assert published_codelist_ids == all_codelist_ids

opencodelists/views/user.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
def user(request, username):
99
user = get_object_or_404(User, username=username)
1010

11+
def codelist_sort_key(codelist):
12+
return (
13+
codelist.name.casefold(),
14+
codelist.owner != user,
15+
str(codelist.owner).casefold(),
16+
codelist.updated_at,
17+
)
18+
1119
# Find all of the codelists owned (in their current version) by this user with at least one published version.
1220
owned_codelists = user.codelists.filter(
1321
versions__status=Status.PUBLISHED
@@ -38,15 +46,7 @@ def user(request, username):
3846
else []
3947
),
4048
}
41-
for codelist in sorted(
42-
codelists_to_display,
43-
key=lambda x: (
44-
x.name.casefold(),
45-
x.owner != user,
46-
str(x.owner).casefold(),
47-
x.updated_at,
48-
),
49-
)
49+
for codelist in sorted(codelists_to_display, key=codelist_sort_key)
5050
# We sort by name, then owner, then date (all case-insensitive where applicable),
5151
# while making sure the current user's codelists appear before organisation ones
5252
# when names are the same.
@@ -59,10 +59,8 @@ def user(request, username):
5959
"user": user,
6060
"published_codelists": [
6161
codelist.latest_published_version()
62-
for codelist in owned_codelists.order_by("handles__name")
62+
for codelist in sorted(owned_codelists, key=codelist_sort_key)
6363
],
64-
# note that name is a property on a codelist, not an attribute, and it comes from the current handle.
65-
# If we want to order codelists or versions by codelist name, we actually need to order them by handle name.
6664
"all_codelists": all_codelists,
6765
}
6866

templates/opencodelists/that_user.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ <h2 class="text-2xl/tight font-bold text-slate-900">Published codelists</h2>
2424
aria-label="Search for codelists published by {{ user.name }}"
2525
class="block w-full rounded-md border border-slate-300 bg-white px-4 py-3 text-base text-slate-900 placeholder:text-slate-400 focus:border-blue-600 focus:ring-2 focus:ring-blue-600/20 focus:outline-none"
2626
id="codelist-search"
27-
placeholder="Filter by name or coding system"
27+
placeholder="Search codelists by name or coding system"
2828
type="search"
2929
/>
3030
</div>

0 commit comments

Comments
 (0)