# 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/factories.py
# class AdvancedFilterFactory(factory.django.DjangoModelFactory):
#     model = 'customers.Client'
# 
#     class Meta:
#         model = 'advanced_filters.AdvancedFilter'


import pytest
from django.contrib.auth.models import Permission
from django.db.models import Q
from django.urls import reverse
from ..models import AdvancedFilter
from .factories import AdvancedFilterFactory
def test_change_page_requires_perms(client, advanced_filter):
    url = reverse(URL_NAME_CHANGE, args=(advanced_filter.pk,))
    res = client.get(url)
    assert res.status_code == 403


def test_change_page_renders(client, user, settings, advanced_filter):
    user.user_permissions.add(Permission.objects.get(codename="change_advancedfilter"))
    url = reverse(URL_NAME_CHANGE, args=(advanced_filter.pk,))

    settings.ADVANCED_FILTER_EDIT_BY_USER = False
    res = client.get(url)
    assert res.status_code == 200


def test_change_and_goto(client, user, settings, advanced_filter):
    user.user_permissions.add(Permission.objects.get(codename="change_advancedfilter"))
    url = reverse(URL_NAME_CHANGE, args=(advanced_filter.pk,))
    form_data = {"form-TOTAL_FORMS": 1, "form-INITIAL_FORMS": 0, "_save_goto": 1}
    settings.ADVANCED_FILTER_EDIT_BY_USER = False
    res = client.post(url, data=form_data)
    assert res.status_code == 302
    url = res["location"]
    assert url.endswith("%s?_afilter=1" % reverse(URL_NAME_CLIENT_CHANGELIST))


def test_create_page_disabled(client, user):
    user.user_permissions.add(Permission.objects.get(codename="add_advancedfilter"))
    url = reverse(URL_NAME_ADD)
    res = client.get(url)

# 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/factories.py
# class AdvancedFilterFactory(factory.django.DjangoModelFactory):
#     model = 'customers.Client'
# 
#     class Meta:
#         model = 'advanced_filters.AdvancedFilter'


# Path: advanced_filters/tests/test_admin_change_form.py
import pytest
from django.contrib.auth.models import Permission
from django.db.models import Q
from django.urls import reverse
from ..models import AdvancedFilter
from .factories import AdvancedFilterFactory

def test_change_page_requires_perms(client, advanced_filter):
    url = reverse(URL_NAME_CHANGE, args=(advanced_filter.pk,))
    res = client.get(url)
    assert res.status_code == 403


def test_change_page_renders(client, user, settings, advanced_filter):
    user.user_permissions.add(Permission.objects.get(codename="change_advancedfilter"))
    url = reverse(URL_NAME_CHANGE, args=(advanced_filter.pk,))

    settings.ADVANCED_FILTER_EDIT_BY_USER = False
    res = client.get(url)
    assert res.status_code == 200


def test_change_and_goto(client, user, settings, advanced_filter):
    user.user_permissions.add(Permission.objects.get(codename="change_advancedfilter"))
    url = reverse(URL_NAME_CHANGE, args=(advanced_filter.pk,))
    form_data = {"form-TOTAL_FORMS": 1, "form-INITIAL_FORMS": 0, "_save_goto": 1}
    settings.ADVANCED_FILTER_EDIT_BY_USER = False
    res = client.post(url, data=form_data)
    assert res.status_code == 302
    url = res["location"]
    assert url.endswith("%s?_afilter=1" % reverse(URL_NAME_CLIENT_CHANGELIST))


def test_create_page_disabled(client, user):
    user.user_permissions.add(Permission.objects.get(codename="add_advancedfilter"))
    url = reverse(URL_NAME_ADD)
    res = client.get(url)

    assert res.status_code == 403