88def 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
0 commit comments