# Path: advanced_filters/models.py
# class AdvancedFilter(models.Model):
#     class Meta:
#         verbose_name = _('Advanced Filter')
#         verbose_name_plural = _('Advanced Filters')
# 
#     title = models.CharField(max_length=255, null=False, blank=False, verbose_name=_('Title'))
#     created_by = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         related_name='created_advanced_filters',
#         verbose_name=_('Created by'),
#         on_delete=models.CASCADE,
#     )
#     created_at = models.DateTimeField(auto_now_add=True, null=True, verbose_name=_('Created at'))
#     url = models.CharField(max_length=255, null=False, blank=False, verbose_name=_('URL'))
#     users = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, verbose_name=_('Users'))
#     groups = models.ManyToManyField('auth.Group', blank=True, verbose_name=_('Groups'))
# 
#     objects = UserLookupManager()
# 
#     b64_query = models.CharField(max_length=2048)
#     model = models.CharField(max_length=64, blank=True, null=True)
# 
#     @property
#     def query(self):
#         """
#         De-serialize, decode and return an ORM query stored in b64_query.
#         """
#         if not self.b64_query:
#             return None
#         s = QSerializer(base64=True)
#         return s.loads(self.b64_query)
# 
#     @query.setter
#     def query(self, value):
#         """
#         Serialize an ORM query, Base-64 encode it and set it to
#         the b64_query field
#         """
#         if not isinstance(value, Q):
#             raise Exception('Must only be passed a Django (Q)uery object')
#         s = QSerializer(base64=True)
#         self.b64_query = s.dumps(value)
# 
#     def list_fields(self):
#         s = QSerializer(base64=True)
#         d = s.loads(self.b64_query, raw=True)
#         return s.get_field_values_list(d)


import pytest
from django.contrib.auth.models import Permission
from django.urls import reverse_lazy
from ..models import AdvancedFilter
    assert form.errors["__all__"] == ["Error validating filter forms"]


@pytest.fixture()
def good_data(form_data):
    form_data.update(
        {
            "title": "Test title",
            "form-0-field": "language",
            "form-0-operator": "iexact",
            "form-0-value": "ru",
        }
    )
    return form_data


@pytest.fixture()
def query():
    return ["language__iexact", "ru"]


def test_create_form_valid(user, client, good_data, query):
    assert AdvancedFilter.objects.count() == 0
    user.user_permissions.add(Permission.objects.get(codename="change_client"))
    res = client.post(URL_CLIENT_CHANGELIST, data=good_data)
    assert res.status_code == 200
    form = res.context_data["advanced_filters"]
    assert form.is_valid()
    assert AdvancedFilter.objects.count() == 1


# Path: advanced_filters/models.py
# class AdvancedFilter(models.Model):
#     class Meta:
#         verbose_name = _('Advanced Filter')
#         verbose_name_plural = _('Advanced Filters')
# 
#     title = models.CharField(max_length=255, null=False, blank=False, verbose_name=_('Title'))
#     created_by = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         related_name='created_advanced_filters',
#         verbose_name=_('Created by'),
#         on_delete=models.CASCADE,
#     )
#     created_at = models.DateTimeField(auto_now_add=True, null=True, verbose_name=_('Created at'))
#     url = models.CharField(max_length=255, null=False, blank=False, verbose_name=_('URL'))
#     users = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, verbose_name=_('Users'))
#     groups = models.ManyToManyField('auth.Group', blank=True, verbose_name=_('Groups'))
# 
#     objects = UserLookupManager()
# 
#     b64_query = models.CharField(max_length=2048)
#     model = models.CharField(max_length=64, blank=True, null=True)
# 
#     @property
#     def query(self):
#         """
#         De-serialize, decode and return an ORM query stored in b64_query.
#         """
#         if not self.b64_query:
#             return None
#         s = QSerializer(base64=True)
#         return s.loads(self.b64_query)
# 
#     @query.setter
#     def query(self, value):
#         """
#         Serialize an ORM query, Base-64 encode it and set it to
#         the b64_query field
#         """
#         if not isinstance(value, Q):
#             raise Exception('Must only be passed a Django (Q)uery object')
#         s = QSerializer(base64=True)
#         self.b64_query = s.dumps(value)
# 
#     def list_fields(self):
#         s = QSerializer(base64=True)
#         d = s.loads(self.b64_query, raw=True)
#         return s.get_field_values_list(d)


# Path: advanced_filters/tests/test_creation.py
import pytest
from django.contrib.auth.models import Permission
from django.urls import reverse_lazy
from ..models import AdvancedFilter

    assert form.errors["__all__"] == ["Error validating filter forms"]


@pytest.fixture()
def good_data(form_data):
    form_data.update(
        {
            "title": "Test title",
            "form-0-field": "language",
            "form-0-operator": "iexact",
            "form-0-value": "ru",
        }
    )
    return form_data


@pytest.fixture()
def query():
    return ["language__iexact", "ru"]


def test_create_form_valid(user, client, good_data, query):
    assert AdvancedFilter.objects.count() == 0
    user.user_permissions.add(Permission.objects.get(codename="change_client"))
    res = client.post(URL_CLIENT_CHANGELIST, data=good_data)
    assert res.status_code == 200
    form = res.context_data["advanced_filters"]
    assert form.is_valid()
    assert AdvancedFilter.objects.count() == 1


    created_filter = AdvancedFilter.objects.order_by("pk").last()