Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
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]
Ejemplo n.º 3
0
def test_picotable_correctly_sorts_translated_fields(rf, admin_user,
                                                     regular_user):
    """
    Make sure that translated fields, such as product names, are correctly sorted
    """
    populate_if_required()

    columns = [
        Column("id", "Id", filter_config=Filter(), display=instance_id),
        Column("name",
               "Name",
               sort_field="translations__name",
               filter_config=TextFilter(filter_field="translations__name")),
    ]

    pico = get_pico(rf, admin_user, model=Product, columns=columns)

    # Verify ascending sort
    sorted_products = pico.get_data({
        "perPage": 100,
        "page": 1,
        "sort": "+name"
    })
    sorted_names = [p["name"] for p in sorted_products["items"]]
    assert sorted_names == sorted(sorted_names)

    # Verify descending sort
    sorted_products = pico.get_data({
        "perPage": 100,
        "page": 1,
        "sort": "-name"
    })
    sorted_names = [p["name"] for p in sorted_products["items"]]
    assert sorted_names == sorted(sorted_names, reverse=True)
Ejemplo n.º 4
0
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())
Ejemplo n.º 5
0
def test_picotable_display_related(rf, admin_user, regular_user):
    group_1 = Group.objects.create(name='group 1')
    group_2 = Group.objects.create(name='group 2')
    groups = (group_1, group_2)
    admin_user.groups.add(*groups)

    columns = (
        Column("id", "Id", filter_config=Filter(), display=instance_id),
        Column("groups", "Groups", filter_config=Filter()),
    )
    pico = get_pico(rf, admin_user, columns=columns)
    data = pico.get_data({"perPage": 100, "page": 1})

    for item in data["items"]:
        if item["id"] == admin_user.pk:
            assert item["groups"] == ", ".join(str(g) for g in groups)
        if item["id"] == regular_user.pk:
            assert item["groups"] == ""
Ejemplo n.º 6
0
def test_choice_filter_with_default(rf, admin_user, regular_user):
    columns = [
        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("date_joined", "Date Joined", filter_config=DateRangeFilter())
    ]

    is_active = [
        Column("is_active",
               "Is Active",
               filter_config=ChoicesFilter(choices=false_and_true))
    ]
    is_active_with_default = [
        Column("is_active",
               "Is Active",
               filter_config=ChoicesFilter(choices=false_and_true,
                                           default=True))
    ]

    query = {"perPage": 100, "page": 1, "sort": "+id"}

    pico_no_defaults = get_pico(rf, columns=(columns + is_active))
    data = pico_no_defaults.get_data(query)
    user_data = data["items"][0]
    user = get_user_model().objects.get(id=user_data["id"])
    assert user.is_active

    pico_with_defaults = get_pico(rf,
                                  columns=(columns + is_active_with_default))
    data = pico_with_defaults.get_data(query)
    user_data = data["items"][0]
    user_with_defaults = get_user_model().objects.get(id=user_data["id"])
    assert user_with_defaults == user

    user.is_active = False
    user.save()

    data = pico_no_defaults.get_data(query)
    user_data = data["items"][0]
    new_user = get_user_model().objects.get(id=user_data["id"])
    assert new_user == user

    data = pico_with_defaults.get_data(query)
    user_data = data["items"][0]
    new_user_with_defaults = get_user_model().objects.get(id=user_data["id"])
    assert new_user_with_defaults != user_with_defaults