-
-
Notifications
You must be signed in to change notification settings - Fork 732
/
test_layout.py
630 lines (524 loc) · 20.5 KB
/
test_layout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
import pytest
from django import forms
from django.forms.models import formset_factory, modelformset_factory
from django.middleware.csrf import _get_new_csrf_string
from django.shortcuts import render
from django.template import Context, Template
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from crispy_forms.bootstrap import Field, InlineCheckboxes
from crispy_forms.helper import FormHelper
from crispy_forms.layout import HTML, ButtonHolder, Column, Div, Fieldset, Layout, MultiField, Row, Submit
from crispy_forms.utils import render_crispy_form
from .conftest import only_bootstrap, only_bootstrap3, only_bootstrap4, only_uni_form
from .forms import (
CheckboxesSampleForm,
CrispyEmptyChoiceTestModel,
CrispyTestModel,
FileForm,
SampleForm,
SampleForm2,
SampleForm3,
SampleForm4,
SampleForm6,
)
from .utils import contains_partial
def test_invalid_unicode_characters(settings):
# Adds a BooleanField that uses non valid unicode characters "ñ"
form_helper = FormHelper()
form_helper.add_layout(Layout("españa"))
template = Template(
"""
{% load crispy_forms_tags %}
{% crispy form form_helper %}
"""
)
c = Context({"form": SampleForm(), "form_helper": form_helper})
settings.CRISPY_FAIL_SILENTLY = False
with pytest.raises(Exception):
template.render(c)
def test_unicode_form_field():
class UnicodeForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["contraseña"] = forms.CharField()
helper = FormHelper()
helper.layout = Layout("contraseña")
html = render_crispy_form(UnicodeForm())
assert 'id="id_contraseña"' in html
def test_meta_extra_fields_with_missing_fields():
class FormWithMeta(SampleForm):
class Meta:
fields = ("email", "first_name", "last_name")
form = FormWithMeta()
# We remove email field on the go
del form.fields["email"]
form_helper = FormHelper()
form_helper.layout = Layout("first_name",)
template = Template(
"""
{% load crispy_forms_tags %}
{% crispy form form_helper %}
"""
)
c = Context({"form": form, "form_helper": form_helper})
html = template.render(c)
assert "email" not in html
def test_layout_unresolved_field(settings):
form_helper = FormHelper()
form_helper.add_layout(Layout("typo"))
template = Template(
"""
{% load crispy_forms_tags %}
{% crispy form form_helper %}
"""
)
c = Context({"form": SampleForm(), "form_helper": form_helper})
settings.CRISPY_FAIL_SILENTLY = False
with pytest.raises(Exception):
template.render(c)
def test_double_rendered_field(settings):
form_helper = FormHelper()
form_helper.add_layout(Layout("is_company", "is_company",))
template = Template(
"""
{% load crispy_forms_tags %}
{% crispy form form_helper %}
"""
)
c = Context({"form": SampleForm(), "form_helper": form_helper})
settings.CRISPY_FAIL_SILENTLY = False
with pytest.raises(Exception):
template.render(c)
def test_context_pollution():
class ExampleForm(forms.Form):
comment = forms.CharField()
form = ExampleForm()
form2 = SampleForm()
template = Template(
"""
{% load crispy_forms_tags %}
{{ form.as_ul }}
{% crispy form2 %}
{{ form.as_ul }}
"""
)
c = Context({"form": form, "form2": form2})
html = template.render(c)
assert html.count('name="comment"') == 2
assert html.count('name="is_company"') == 1
def test_layout_fieldset_row_html_with_unicode_fieldnames(settings):
form_helper = FormHelper()
form_helper.add_layout(
Layout(
Fieldset(
"Company Data",
"is_company",
css_id="fieldset_company_data",
css_class="fieldsets",
title="fieldset_title",
test_fieldset="123",
),
Fieldset(
"User Data",
"email",
Row("password1", "password2", css_id="row_passwords", css_class="rows",),
HTML('<a href="#" id="testLink">test link</a>'),
HTML(
"""
{% if flag %}{{ message }}{% endif %}
"""
),
"first_name",
"last_name",
),
)
)
template = Template(
"""
{% load crispy_forms_tags %}
{% crispy form form_helper %}
"""
)
c = Context({"form": SampleForm(), "form_helper": form_helper, "flag": True, "message": "Hello!"})
html = template.render(c)
assert 'id="fieldset_company_data"' in html
assert 'class="fieldsets' in html
assert 'title="fieldset_title"' in html
assert 'test-fieldset="123"' in html
assert 'id="row_passwords"' in html
assert html.count("<label") == 6
if settings.CRISPY_TEMPLATE_PACK == "uni_form":
assert 'class="formRow rows"' in html
elif settings.CRISPY_TEMPLATE_PACK == "bootstrap4":
assert 'class="form-row rows"' in html
else:
assert 'class="row rows"' in html
assert "Hello!" in html
assert "testLink" in html
def test_change_layout_dynamically_delete_field():
template = Template(
"""
{% load crispy_forms_tags %}
{% crispy form form_helper %}
"""
)
form = SampleForm()
form_helper = FormHelper()
form_helper.add_layout(
Layout(
Fieldset("Company Data", "is_company", "email", "password1", "password2", css_id="multifield_info",),
Column("first_name", "last_name", css_id="column_name",),
)
)
# We remove email field on the go
# Layout needs to be adapted for the new form fields
del form.fields["email"]
del form_helper.layout.fields[0].fields[1]
c = Context({"form": form, "form_helper": form_helper})
html = template.render(c)
assert "email" not in html
def test_column_has_css_classes(settings):
template = Template(
"""
{% load crispy_forms_tags %}
{% crispy form form_helper %}
"""
)
form = SampleForm()
form_helper = FormHelper()
form_helper.add_layout(
Layout(
Fieldset("Company Data", "is_company", "email", "password1", "password2", css_id="multifield_info",),
Column("first_name", "last_name",),
)
)
c = Context({"form": form, "form_helper": form_helper})
html = template.render(c)
if settings.CRISPY_TEMPLATE_PACK == "uni_form":
assert html.count("formColumn") == 1
assert html.count("col") == 0
elif settings.CRISPY_TEMPLATE_PACK == "bootstrap4":
assert html.count("formColumn") == 0
assert html.count("col-md") == 1
@only_bootstrap4
def test_bs4_column_css_classes(settings):
template = Template(
"""
{% load crispy_forms_tags %}
{% crispy form form_helper %}
"""
)
form = SampleForm()
form_helper = FormHelper()
form_helper.add_layout(
Layout(
Column("first_name", "last_name",),
Column("first_name", "last_name", css_class="col-sm"),
Column("first_name", "last_name", css_class="mb-4"),
)
)
c = Context({"form": form, "form_helper": form_helper})
html = template.render(c)
assert html.count("col-md") == 2
assert html.count("col-sm") == 1
def test_formset_layout(settings):
SampleFormSet = formset_factory(SampleForm, extra=3)
formset = SampleFormSet()
helper = FormHelper()
helper.form_id = "thisFormsetRocks"
helper.form_class = "formsets-that-rock"
helper.form_method = "POST"
helper.form_action = "simpleAction"
helper.layout = Layout(
Fieldset("Item {{ forloop.counter }}", "is_company", "email",),
HTML("{% if forloop.first %}Note for first form only{% endif %}"),
Row("password1", "password2"),
Fieldset("", "first_name", "last_name"),
)
html = render_crispy_form(form=formset, helper=helper, context={"csrf_token": _get_new_csrf_string()})
# Check formset fields
assert contains_partial(html, '<input id="id_form-TOTAL_FORMS" name="form-TOTAL_FORMS" type="hidden" value="3"/>')
assert contains_partial(
html, '<input id="id_form-INITIAL_FORMS" name="form-INITIAL_FORMS" type="hidden" value="0"/>'
)
assert contains_partial(
html, '<input id="id_form-MAX_NUM_FORMS" name="form-MAX_NUM_FORMS" type="hidden" value="1000"/>'
)
assert contains_partial(
html, '<input id="id_form-MIN_NUM_FORMS" name="form-MIN_NUM_FORMS" type="hidden" value="0"/>'
)
assert html.count("hidden") == 5
# Check form structure
assert html.count("<form") == 1
assert html.count("csrfmiddlewaretoken") == 1
assert "formsets-that-rock" in html
assert 'method="post"' in html
assert 'id="thisFormsetRocks"' in html
assert 'action="%s"' % reverse("simpleAction") in html
# Check form layout
assert "Item 1" in html
assert "Item 2" in html
assert "Item 3" in html
assert html.count("Note for first form only") == 1
if settings.CRISPY_TEMPLATE_PACK == "uni_form":
assert html.count("formRow") == 3
elif settings.CRISPY_TEMPLATE_PACK in ("bootstrap3", "bootstrap4"):
assert html.count("row") == 3
if settings.CRISPY_TEMPLATE_PACK == "bootstrap4":
assert html.count("form-group") == 18
def test_modelformset_layout():
CrispyModelFormSet = modelformset_factory(CrispyTestModel, form=SampleForm4, extra=3)
formset = CrispyModelFormSet(queryset=CrispyTestModel.objects.none())
helper = FormHelper()
helper.layout = Layout("email")
html = render_crispy_form(form=formset, helper=helper)
assert html.count("id_form-0-id") == 1
assert html.count("id_form-1-id") == 1
assert html.count("id_form-2-id") == 1
assert contains_partial(html, '<input id="id_form-TOTAL_FORMS" name="form-TOTAL_FORMS" type="hidden" value="3"/>')
assert contains_partial(
html, '<input id="id_form-INITIAL_FORMS" name="form-INITIAL_FORMS" type="hidden" value="0"/>'
)
assert contains_partial(
html, '<input id="id_form-MAX_NUM_FORMS" name="form-MAX_NUM_FORMS" type="hidden" value="1000"/>'
)
assert html.count('name="form-0-email"') == 1
assert html.count('name="form-1-email"') == 1
assert html.count('name="form-2-email"') == 1
assert html.count('name="form-3-email"') == 0
assert html.count("password") == 0
def test_i18n():
template = Template(
"""
{% load crispy_forms_tags %}
{% crispy form form.helper %}
"""
)
form = SampleForm()
form_helper = FormHelper()
form_helper.layout = Layout(HTML(_("i18n text")), Fieldset(_("i18n legend"), "first_name", "last_name",))
form.helper = form_helper
html = template.render(Context({"form": form}))
assert html.count("i18n legend") == 1
def test_default_layout():
test_form = SampleForm2()
assert test_form.helper.layout.fields == [
"is_company",
"email",
"password1",
"password2",
"first_name",
"last_name",
"datetime_field",
]
def test_default_layout_two():
test_form = SampleForm3()
assert test_form.helper.layout.fields == ["email"]
def test_modelform_layout_without_meta():
test_form = SampleForm4()
test_form.helper = FormHelper()
test_form.helper.layout = Layout("email")
html = render_crispy_form(test_form)
assert "email" in html
assert "password" not in html
def test_specialspaceless_not_screwing_intended_spaces():
# see issue #250
test_form = SampleForm()
test_form.fields["email"].widget = forms.Textarea()
test_form.helper = FormHelper()
test_form.helper.layout = Layout("email", HTML("<span>first span</span> <span>second span</span>"))
html = render_crispy_form(test_form)
assert "<span>first span</span> <span>second span</span>" in html
def test_choice_with_none_is_selected():
# see issue #701
model_instance = CrispyEmptyChoiceTestModel()
model_instance.fruit = None
test_form = SampleForm6(instance=model_instance)
html = render_crispy_form(test_form)
assert "checked" in html
@only_uni_form
def test_layout_composition():
form_helper = FormHelper()
form_helper.add_layout(
Layout(
Layout(MultiField("Some company data", "is_company", "email", css_id="multifield_info",),),
Column(
"first_name",
# 'last_name', Missing a field on purpose
css_id="column_name",
css_class="columns",
),
ButtonHolder(Submit("Save", "Save", css_class="button white"),),
Div("password1", "password2", css_id="custom-div", css_class="customdivs",),
)
)
template = Template(
"""
{% load crispy_forms_tags %}
{% crispy form form_helper %}
"""
)
c = Context({"form": SampleForm(), "form_helper": form_helper})
html = template.render(c)
assert "multiField" in html
assert "formColumn" in html
assert 'id="multifield_info"' in html
assert 'id="column_name"' in html
assert 'class="formColumn columns"' in html
assert 'class="buttonHolder">' in html
assert 'input type="submit"' in html
assert 'name="Save"' in html
assert 'id="custom-div"' in html
assert 'class="customdivs"' in html
assert "last_name" not in html
@only_uni_form
def test_second_layout_multifield_column_buttonholder_submit_div():
form_helper = FormHelper()
form_helper.add_layout(
Layout(
MultiField(
"Some company data",
"is_company",
"email",
css_id="multifield_info",
title="multifield_title",
multifield_test="123",
),
Column("first_name", "last_name", css_id="column_name", css_class="columns",),
ButtonHolder(
Submit(
"Save the world", "{{ value_var }}", css_class="button white", data_id="test", data_name="test"
),
Submit("store", "Store results"),
),
Div("password1", "password2", css_id="custom-div", css_class="customdivs", test_markup="123"),
)
)
template = Template(
"""
{% load crispy_forms_tags %}
{% crispy form form_helper %}
"""
)
c = Context({"form": SampleForm(), "form_helper": form_helper, "value_var": "Save"})
html = template.render(c)
assert "multiField" in html
assert "formColumn" in html
assert 'id="multifield_info"' in html
assert 'title="multifield_title"' in html
assert 'multifield-test="123"' in html
assert 'id="column_name"' in html
assert 'class="formColumn columns"' in html
assert 'class="buttonHolder">' in html
assert 'input type="submit"' in html
assert "button white" in html
assert 'data-id="test"' in html
assert 'data-name="test"' in html
assert 'name="save-the-world"' in html
assert 'value="Save"' in html
assert 'name="store"' in html
assert 'value="Store results"' in html
assert 'id="custom-div"' in html
assert 'class="customdivs"' in html
assert 'test-markup="123"' in html
@only_bootstrap
def test_keepcontext_context_manager(settings):
# Test case for issue #180
# Apparently it only manifest when using render_to_response this exact way
form = CheckboxesSampleForm()
form.helper = FormHelper()
# We use here InlineCheckboxes as it updates context in an unsafe way
form.helper.layout = Layout("checkboxes", InlineCheckboxes("alphacheckboxes"), "numeric_multiple_checkboxes")
context = {"form": form}
response = render(request=None, template_name="crispy_render_template.html", context=context)
if settings.CRISPY_TEMPLATE_PACK == "bootstrap":
assert response.content.count(b"checkbox inline") == 3
elif settings.CRISPY_TEMPLATE_PACK == "bootstrap3":
assert response.content.count(b"checkbox-inline") == 3
elif settings.CRISPY_TEMPLATE_PACK == "bootstrap4":
assert response.content.count(b"custom-control-inline") == 3
assert response.content.count(b"custom-checkbox") > 0
@only_bootstrap4
def test_use_custom_control_is_used():
form = CheckboxesSampleForm()
form.helper = FormHelper()
form.helper.layout = Layout("checkboxes", InlineCheckboxes("alphacheckboxes"), "numeric_multiple_checkboxes")
# form.helper.use_custom_control take default value which is True
response = render(request=None, template_name="crispy_render_template.html", context={"form": form})
assert response.content.count(b"custom-control-inline") == 3
assert response.content.count(b"custom-checkbox") == 9
form.helper.use_custom_control = True
response = render(request=None, template_name="crispy_render_template.html", context={"form": form})
assert response.content.count(b"custom-control-inline") == 3
assert response.content.count(b"custom-checkbox") == 9
form.helper.use_custom_control = False
response = render(request=None, template_name="crispy_render_template.html", context={"form": form})
assert response.content.count(b"custom-control-inline") == 0
assert response.content.count(b"form-check-inline") == 3
assert response.content.count(b"form-check") > 0
assert response.content.count(b"custom-checkbox") == 0
@only_bootstrap3
def test_form_inline():
form = SampleForm()
form.helper = FormHelper()
form.helper.form_class = "form-inline"
form.helper.field_template = "bootstrap3/layout/inline_field.html"
form.helper.layout = Layout("email", "password1", "last_name",)
html = render_crispy_form(form)
assert html.count('class="form-inline"') == 1
assert html.count('class="form-group"') == 3
assert html.count('<label for="id_email" class="sr-only') == 1
assert html.count('id="div_id_email" class="form-group"') == 1
assert html.count('placeholder="email"') == 1
assert html.count("</label> <input") == 3
@only_bootstrap4
def test_bootstrap4_form_inline():
form = SampleForm()
form.helper = FormHelper()
form.helper.form_class = "form-inline"
form.helper.field_template = "bootstrap4/layout/inline_field.html"
form.helper.layout = Layout("email", "password1", "last_name",)
html = render_crispy_form(form)
assert html.count('class="form-inline"') == 1
assert html.count('class="input-group"') == 3
assert html.count('<label for="id_email" class="sr-only') == 1
assert html.count('id="div_id_email" class="input-group"') == 1
assert html.count('placeholder="email"') == 1
assert html.count("</label> <input") == 3
def test_update_attributes_class():
form = SampleForm()
form.helper = FormHelper()
form.helper.layout = Layout("email", Field("password1"), "password2",)
form.helper["password1"].update_attributes(css_class="hello")
html = render_crispy_form(form)
assert html.count(' class="hello textinput') == 1
form.helper = FormHelper()
form.helper.layout = Layout("email", Field("password1", css_class="hello"), "password2",)
form.helper["password1"].update_attributes(css_class="hello2")
html = render_crispy_form(form)
assert html.count(' class="hello hello2 textinput') == 1
@only_bootstrap4
def test_file_field():
form = FileForm()
form.helper = FormHelper()
form.helper.layout = Layout("clearable_file")
html = render_crispy_form(form)
assert '<span class="custom-control custom-checkbox">' in html
assert '<input type="file" name="clearable_file" class="custom-file-input" >' in html
form.helper.use_custom_control = False
html = render_crispy_form(form)
assert '<input type="checkbox" name="clearable_file-clear" id="clearable_file-clear_id">' in html
assert '<input type="file" name="clearable_file" class="custom-file-input" >' not in html
form.helper.use_custom_control = True
form.helper.layout = Layout("file_field")
html = render_crispy_form(form)
assert '<div class="form-control custom-file"' in html
assert '<input type="file" name="file_field" class="custom-file-input"' in html
assert '<label class="custom-file-label' in html
assert 'for="id_file_field">---</label>' in html
form.helper.use_custom_control = False
html = render_crispy_form(form)
assert "custom-file" not in html
assert "custom-file-input" not in html
assert "custom-file-label" not in html