# Path: tests/test_forms.py
# def remove_csrf(html_code):
#     csrf_regex = r'<input[^>]+csrfmiddlewaretoken[^>]+>'
#     return re.sub(csrf_regex, '', html_code)


import json
import pytest
from bs4 import BeautifulSoup
from tri_struct import Struct, merged
from tests.test_forms import remove_csrf
from tri_form import INITIALS_FROM_GET, DISPATCH_PATH_SEPARATOR
from tri_form.views import create_object, edit_object, create_or_edit_object_redirect
    from tests.models import CreateOrEditObjectTest, get_saved_something, Foo, reset_saved_something
    from tests.models import UniqueConstraintTest
    from tests.models import get_saved_something, reset_saved_something, NamespaceFormsTest
    from tests.models import Bar, Foo
    from tests.models import Foo
    from tests.models import Foo
    from tests.models import Foo
    from tests.models import Baz
    from tests.models import Foo
    assert form.fields_by_name['f_float'].value == 2
    assert form.fields_by_name['f_bool'].value is None
    assert set(form.fields_by_name.keys()) == {'f_int', 'f_float', 'f_bool', 'f_foreign_key', 'f_many_to_many'}

    # 2. Create
    foo = Foo.objects.create(foo=7)

    request.method = 'POST'
    request.POST = {
        'f_int': '3',
        'f_float': '5.1',
        'f_bool': 'True',
        'f_foreign_key': str(foo.pk),
        'f_many_to_many': [str(foo.pk)],
        f'{DISPATCH_PATH_SEPARATOR}': '',
    }

    def on_save(form, instance, **_):
        # validate  that the arguments are what we expect
        assert form.instance is instance
        assert isinstance(instance, CreateOrEditObjectTest)
        assert instance.pk is not None

    response = create_object(
        request=request,
        model=CreateOrEditObjectTest,
        on_save=on_save,  # just to check that we get called with the instance as argument
        render=lambda **kwargs: kwargs,
    )
    instance = get_saved_something()

# Path: tests/test_forms.py
# def remove_csrf(html_code):
#     csrf_regex = r'<input[^>]+csrfmiddlewaretoken[^>]+>'
#     return re.sub(csrf_regex, '', html_code)


# Path: tests/test_views.py
import json
import pytest
from bs4 import BeautifulSoup
from tri_struct import Struct, merged
from tests.test_forms import remove_csrf
from tri_form import INITIALS_FROM_GET, DISPATCH_PATH_SEPARATOR
from tri_form.views import create_object, edit_object, create_or_edit_object_redirect
    from tests.models import CreateOrEditObjectTest, get_saved_something, Foo, reset_saved_something
    from tests.models import UniqueConstraintTest
    from tests.models import get_saved_something, reset_saved_something, NamespaceFormsTest
    from tests.models import Bar, Foo
    from tests.models import Foo
    from tests.models import Foo
    from tests.models import Foo
    from tests.models import Baz
    from tests.models import Foo

    assert form.fields_by_name['f_float'].value == 2
    assert form.fields_by_name['f_bool'].value is None
    assert set(form.fields_by_name.keys()) == {'f_int', 'f_float', 'f_bool', 'f_foreign_key', 'f_many_to_many'}

    # 2. Create
    foo = Foo.objects.create(foo=7)

    request.method = 'POST'
    request.POST = {
        'f_int': '3',
        'f_float': '5.1',
        'f_bool': 'True',
        'f_foreign_key': str(foo.pk),
        'f_many_to_many': [str(foo.pk)],
        f'{DISPATCH_PATH_SEPARATOR}': '',
    }

    def on_save(form, instance, **_):
        # validate  that the arguments are what we expect
        assert form.instance is instance
        assert isinstance(instance, CreateOrEditObjectTest)
        assert instance.pk is not None

    response = create_object(
        request=request,
        model=CreateOrEditObjectTest,
        on_save=on_save,  # just to check that we get called with the instance as argument
        render=lambda **kwargs: kwargs,
    )
    instance = get_saved_something()

    reset_saved_something()