def test_picotable_queryset_default_ordering(rf, admin_user): shop = get_default_shop() columns = [Column("username", "Username", filter_config=Filter())] some_users = [] for x in range(5): some_users.append(create_random_user()) admin_user.is_staff = True admin_user.save() shop.staff_members.add(admin_user) request = apply_request_middleware(rf.get("/"), user=admin_user) pico = Picotable(request=request, columns=columns, mass_actions=[], queryset=get_user_model().objects.exclude( is_superuser=True).order_by("-username"), context=PicoContext(request)) users = pico.get_data({"perPage": 100, "page": 1}) # Should return users sorted by username some_users.sort(key=lambda x: x.username, reverse=True) assert len(users["items"]) == len(some_users) assert len(users["items"]) == 5 assert [item["username"] for item in users["items"] ] == [user.username for user in some_users] # Let's still double check that the regular sort works users = pico.get_data({"perPage": 100, "page": 1, "sort": "+username"}) some_users.sort(key=lambda x: x.username) assert len(users["items"]) == len(some_users) assert len(users["items"]) == 5 assert [item["username"] for item in users["items"] ] == [user.username for user in some_users]
def get_pico(rf, admin_user, model=None, columns=None): shop = get_default_shop() model = model or get_user_model() columns = columns or [ Column("id", "Id", filter_config=Filter(), display=instance_id), Column("username", "Username", sortable=False, filter_config=MultiFieldTextFilter( filter_fields=("username", "email"), operator="iregex")), Column("email", "Email", sortable=False, filter_config=TextFilter()), Column("is_superuser", "Is Superuser", display="superuser_display", filter_config=ChoicesFilter(choices=false_and_true())), Column("is_active", "Is Active", filter_config=ChoicesFilter( choices=false_and_true)), # `choices` callable Column("date_joined", "Date Joined", filter_config=DateRangeFilter()) ] admin_user.is_staff = True admin_user.save() shop.staff_members.add(admin_user) request = apply_request_middleware(rf.get("/"), user=admin_user) return Picotable(request=request, columns=columns, mass_actions=[], queryset=model.objects.all(), context=PicoContext(request))
def get_pico(rf, model=None, columns=None): model = model or get_user_model() columns = columns or [ Column("id", "Id", filter_config=Filter(), display=instance_id), Column("username", "Username", sortable=False, filter_config=MultiFieldTextFilter( filter_fields=("username", "email"), operator="iregex")), Column("email", "Email", sortable=False, filter_config=TextFilter()), Column("is_superuser", "Is Superuser", display="superuser_display", filter_config=ChoicesFilter(choices=false_and_true())), Column("is_active", "Is Active", filter_config=ChoicesFilter( choices=false_and_true)), # `choices` callable Column("date_joined", "Date Joined", filter_config=DateRangeFilter()) ] return Picotable(request=rf.get("/"), columns=columns, queryset=model.objects.all(), context=PicoContext())