-
-
Notifications
You must be signed in to change notification settings - Fork 731
/
Copy pathbootstrap.py
1131 lines (940 loc) · 37 KB
/
bootstrap.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from random import randint
from django.template import Template
from django.template.loader import render_to_string
from django.utils.safestring import SafeString
from django.utils.text import slugify
from .layout import Div, Field, LayoutObject, TemplateNameMixin
from .utils import TEMPLATE_PACK, flatatt, render_field
class PrependedAppendedText(Field):
"""
Layout object for rendering a field with prepended and appended text.
Attributes
----------
template : str
The default template which this Layout Object will be rendered
with.
attrs : dict
Attributes to be applied to the field. These are converted into html
attributes. e.g. ``data_id: 'test'`` in the attrs dict will become
``data-id='test'`` on the field's ``<input>``.
Parameters
----------
field : str
The name of the field to be rendered.
prepended_text : str, optional
The prepended text, can be HTML like, by default None
appended_text : str, optional
The appended text, can be HTML like, by default None
input_size : str, optional
For Bootstrap4+ additional classes to customise the input-group size
e.g. ``input-group-sm``. By default None
active : bool
For Bootstrap3, a boolean to render the text active. By default
``False``.
css_class : str, optional
CSS classes to be applied to the field. These are added to any classes
included in the ``attrs`` dict. By default ``None``.
wrapper_class: str, optional
CSS classes to be used when rendering the Field. This class is usually
applied to the ``<div>`` which wraps the Field's ``<label>`` and
``<input>`` tags. By default ``None``.
template : str, optional
Overrides the default template, if provided. By default ``None``.
**kwargs : dict, optional
Additional attributes are converted into key="value", pairs. These
attributes are added to the ``<div>``.
Examples
--------
Example::
PrependedAppendedText('amount', '$', '.00')
"""
template = "%s/layout/prepended_appended_text.html"
def __init__(
self,
field,
prepended_text=None,
appended_text=None,
input_size=None,
*,
active=False,
css_class=None,
wrapper_class=None,
template=None,
**kwargs,
):
self.field = field
self.appended_text = appended_text
self.prepended_text = prepended_text
self.active = active
self.input_size = input_size
if css_class: # Bootstrap 3
if "input-lg" in css_class:
self.input_size = "input-lg"
if "input-sm" in css_class:
self.input_size = "input-sm"
super().__init__(field, css_class=css_class, wrapper_class=wrapper_class, template=template, **kwargs)
def render(self, form, context, template_pack=TEMPLATE_PACK, extra_context=None, **kwargs):
extra_context = extra_context.copy() if extra_context is not None else {}
extra_context.update(
{
"crispy_appended_text": self.appended_text,
"crispy_prepended_text": self.prepended_text,
"input_size": self.input_size,
"active": getattr(self, "active", False),
"wrapper_class": self.wrapper_class,
}
)
template = self.get_template_name(template_pack)
return render_field(
self.field,
form,
context,
template=template,
attrs=self.attrs,
template_pack=template_pack,
extra_context=extra_context,
**kwargs,
)
class AppendedText(PrependedAppendedText):
"""
Layout object for rendering a field with appended text.
Attributes
----------
template : str
The default template which this Layout Object will be rendered
with.
attrs : dict
Attributes to be applied to the field. These are converted into html
attributes. e.g. ``data_id: 'test'`` in the attrs dict will become
``data-id='test'`` on the field's ``<input>``.
Parameters
----------
field : str
The name of the field to be rendered.
text : str
The appended text, can be HTML like.
input_size : str, optional
For Bootstrap4+ additional classes to customise the input-group size
e.g. ``input-group-sm``. By default None
active : bool
For Bootstrap3, a boolean to render the text active. By default
``False``.
css_class : str, optional
CSS classes to be applied to the field. These are added to any classes
included in the ``attrs`` dict. By default ``None``.
wrapper_class: str, optional
CSS classes to be used when rendering the Field. This class is usually
applied to the ``<div>`` which wraps the Field's ``<label>`` and
``<input>`` tags. By default ``None``.
template : str, optional
Overrides the default template, if provided. By default ``None``.
**kwargs : dict, optional
Additional attributes are converted into key="value", pairs. These
attributes are added to the ``<div>``.
Examples
--------
Example::
AppendedText('amount', '.00')
"""
def __init__(
self,
field,
text,
*,
input_size=None,
active=False,
css_class=None,
wrapper_class=None,
template=None,
**kwargs,
):
self.text = text
super().__init__(
field,
appended_text=text,
input_size=input_size,
active=active,
css_class=css_class,
wrapper_class=wrapper_class,
template=template,
**kwargs,
)
class PrependedText(PrependedAppendedText):
"""
Layout object for rendering a field with prepended text.
Attributes
----------
template : str
The default template which this Layout Object will be rendered
with.
attrs : dict
Attributes to be applied to the field. These are converted into html
attributes. e.g. ``data_id: 'test'`` in the attrs dict will become
``data-id='test'`` on the field's ``<input>``.
Parameters
----------
field : str
The name of the field to be rendered.
text : str
The prepended text, can be HTML like.
input_size : str, optional
For Bootstrap4+ additional classes to customise the input-group size
e.g. ``input-group-sm``. By default None
active : bool
For Bootstrap3, a boolean to render the text active. By default
``False``.
css_class : str, optional
CSS classes to be applied to the field. These are added to any classes
included in the ``attrs`` dict. By default ``None``.
wrapper_class: str, optional
CSS classes to be used when rendering the Field. This class is usually
applied to the ``<div>`` which wraps the Field's ``<label>`` and
``<input>`` tags. By default ``None``.
template : str, optional
Overrides the default template, if provided. By default ``None``.
**kwargs : dict, optional
Additional attributes are converted into key="value", pairs. These
attributes are added to the ``<div>``.
Examples
--------
Example::
PrependedText('amount', '$')
"""
def __init__(
self,
field,
text,
*,
input_size=None,
active=False,
css_class=None,
wrapper_class=None,
template=None,
**kwargs,
):
self.text = text
super().__init__(
field,
prepended_text=text,
input_size=input_size,
active=active,
css_class=css_class,
wrapper_class=wrapper_class,
template=template,
**kwargs,
)
class FormActions(LayoutObject):
"""
Bootstrap layout object. It wraps fields in a <div class="form-actions">
Attributes
----------
template: str
The default template which this Layout Object will be rendered with.
Parameters
----------
*fields : HTML or BaseInput
The layout objects to render within the ``ButtonHolder``. It should
only hold `HTML` and `BaseInput` inherited objects.
css_id : str, optional
A custom DOM id for the layout object which will be added to the
``<div>`` if provided. By default None.
css_class : str, optional
Additional CSS classes to be applied to the ``<div>``. By default
None.
template : str, optional
Overrides the default template, if provided. By default None.
**kwargs : dict, optional
Additional attributes are passed to ``flatatt`` and converted into
key="value", pairs. These attributes are added to the ``<div>``.
Examples
--------
An example using ``FormActions`` in your layout::
FormActions(
HTML(<span style="display: hidden;">Information Saved</span>),
Submit('Save', 'Save', css_class='btn-primary')
)
"""
template = "%s/layout/formactions.html"
def __init__(self, *fields, css_id=None, css_class=None, template=None, **kwargs):
self.fields = list(fields)
self.id = css_id
self.css_class = css_class
self.template = template or self.template
self.flat_attrs = flatatt(kwargs)
def render(self, form, context, template_pack=TEMPLATE_PACK, **kwargs):
html = self.get_rendered_fields(form, context, template_pack, **kwargs)
template = self.get_template_name(template_pack)
context.update({"formactions": self, "fields_output": html})
return render_to_string(template, context.flatten())
class InlineCheckboxes(Field):
"""
Layout object for rendering checkboxes inline.
Attributes
----------
template : str
The default template which this Layout Object will be rendered
with.
attrs : dict
Attributes to be applied to the field. These are converted into html
attributes. e.g. ``data_id: 'test'`` in the attrs dict will become
``data-id='test'`` on the field's ``<input>``.
Parameters
----------
*fields : str
Usually a single field, but can be any number of fields, to be rendered
with the same attributes applied.
css_class : str, optional
CSS classes to be applied to the field. These are added to any classes
included in the ``attrs`` dict. By default ``None``.
wrapper_class: str, optional
CSS classes to be used when rendering the Field. This class is usually
applied to the ``<div>`` which wraps the Field's ``<label>`` and
``<input>`` tags. By default ``None``.
template : str, optional
Overrides the default template, if provided. By default ``None``.
**kwargs : dict, optional
Additional attributes are converted into key="value", pairs. These
attributes are added to the ``<div>``.
Examples
--------
Example::
InlineCheckboxes('field_name')
"""
template = "%s/layout/checkboxselectmultiple_inline.html"
def render(self, form, context, template_pack=TEMPLATE_PACK, **kwargs):
return super().render(form, context, template_pack=template_pack, extra_context={"inline_class": "inline"})
class InlineRadios(Field):
"""
Layout object for rendering radiobuttons inline.
Attributes
----------
template : str
The default template which this Layout Object will be rendered
with.
attrs : dict
Attributes to be applied to the field. These are converted into html
attributes. e.g. ``data_id: 'test'`` in the attrs dict will become
``data-id='test'`` on the field's ``<input>``.
Parameters
----------
*fields : str
Usually a single field, but can be any number of fields, to be rendered
with the same attributes applied.
css_class : str, optional
CSS classes to be applied to the field. These are added to any classes
included in the ``attrs`` dict. By default ``None``.
wrapper_class: str, optional
CSS classes to be used when rendering the Field. This class is usually
applied to the ``<div>`` which wraps the Field's ``<label>`` and
``<input>`` tags. By default ``None``.
template : str, optional
Overrides the default template, if provided. By default ``None``.
**kwargs : dict, optional
Additional attributes are converted into key="value", pairs. These
attributes are added to the ``<div>``.
Examples
--------
Example::
InlineRadios('field_name')
"""
template = "%s/layout/radioselect_inline.html"
def render(self, form, context, template_pack=TEMPLATE_PACK, **kwargs):
return super().render(form, context, template_pack=template_pack, extra_context={"inline_class": "inline"})
class FieldWithButtons(Div):
"""
A layout object for rendering a single field with any number of buttons.
Attributes
----------
template : str
The default template which this Layout Object will be rendered
with.
css_class : str, optional
CSS classes to be applied to the wrapping ``<div>``. By default None.
Parameters
----------
*fields : str or LayoutObject
The first positional argument is the field. This can be either the
name of the field as a string or an instance of `Field`. Following
arguments will be rendered as buttons.
input_size : str
Additional CSS class to change the size of the input. e.g.
"input-group-sm".
css_id : str, optional
A DOM id for the layout object which will be added to the wrapping
``<div>`` if provided. By default None.
css_class : str, optional
Additional CSS classes to be applied in addition to those declared by
the class itself. By default None.
template : str, optional
Overrides the default template, if provided. By default None.
**kwargs : dict, optional
Additional attributes are passed to ``flatatt`` and converted into
key="value", pairs. These attributes are added to the wrapping
``<div>``.
Examples
--------
Example::
FieldWithButtons(
Field("password1", css_class="span4"),
StrictButton("Go!", css_id="go-button"),
input_size="input-group-sm",
)
"""
template = "%s/layout/field_with_buttons.html"
field_template = "%s/field.html"
def __init__(self, *fields, input_size=None, css_id=None, css_class=None, template=None, **kwargs):
self.input_size = input_size
super().__init__(*fields, css_id=css_id, css_class=css_class, template=template, **kwargs)
def render(self, form, context, template_pack=TEMPLATE_PACK, extra_context=None, **kwargs):
# We first render the buttons
field_template = self.field_template % template_pack
buttons = SafeString(
"".join(
render_field(
field,
form,
context,
field_template,
layout_object=self,
template_pack=template_pack,
**kwargs,
)
for field in self.fields[1:]
)
)
extra_context = {"div": self, "buttons": buttons}
template = self.get_template_name(template_pack)
if isinstance(self.fields[0], Field):
# FieldWithButtons(Field('field_name'), StrictButton("go"))
# We render the field passing its name and attributes
return render_field(
self.fields[0][0],
form,
context,
template,
attrs=self.fields[0].attrs,
template_pack=template_pack,
extra_context=extra_context,
**kwargs,
)
else:
return render_field(self.fields[0], form, context, template, extra_context=extra_context, **kwargs)
class StrictButton(TemplateNameMixin):
"""
Layout object for rendering an HTML button in a ``<button>`` tag.
Attributes
----------
template: str
The default template which this Layout Object will be rendered
with.
field_classes : str
The CSS classes to be applied to the button. By defult "btn".
Parameters
----------
content : str
The content of the button. This content is context aware, to bring
this to life see the examples section.
css_id : str, optional
A custom DOM id for the layout object which will be added to the
``<button>`` if provided. By default None.
css_class : str, optional
Additional CSS classes to be applied to the ``<button>``. By default
None.
template : str, optional
Overrides the default template, if provided. By default None.
**kwargs : dict, optional
Additional attributes are passed to `flatatt` and converted into
key="value", pairs. These attributes are added to the ``<button>``.
Examples
--------
In your ``Layout``::
StrictButton("button content", css_class="extra")
The content of the button is context aware, so you can do things like::
StrictButton("Button for {{ user.username }}")
"""
template = "%s/layout/button.html"
field_classes = "btn"
def __init__(self, content, css_id=None, css_class=None, template=None, **kwargs):
self.content = content
self.template = template or self.template
kwargs.setdefault("type", "button")
# We turn css_id and css_class into id and class
if css_id:
kwargs["id"] = css_id
kwargs["class"] = self.field_classes
if css_class:
kwargs["class"] += f" {css_class}"
self.flat_attrs = flatatt(kwargs)
def render(self, form, context, template_pack=TEMPLATE_PACK, **kwargs):
self.content = Template(str(self.content)).render(context)
template = self.get_template_name(template_pack)
context.update({"button": self})
return render_to_string(template, context.flatten())
class Container(Div):
"""
Base class used for `Tab` and `AccordionGroup`, represents a basic
container concept.
Attributes
----------
template : str
The default template which this Layout Object will be rendered
with.
css_class : str, optional
CSS classes to be applied to the ``<div>``. By default "".
Parameters
----------
name : str
The name of the container.
*fields : str, LayoutObject
Any number of fields as positional arguments to be rendered within
the container.
css_id : str, optional
A DOM id for the layout object which will be added to the ``<div>`` if
provided. By default None.
css_class : str, optional
Additional CSS classes to be applied in addition to those declared by
the class itself. By default None.
template : str, optional
Overrides the default template, if provided. By default None.
**kwargs : dict, optional
Additional attributes are passed to ``flatatt`` and converted into
key="value", pairs. These attributes are added to the ``<div>``.
"""
css_class = ""
def __init__(self, name, *fields, css_id=None, css_class=None, template=None, active=None, **kwargs):
super().__init__(*fields, css_id=css_id, css_class=css_class, template=template, **kwargs)
self.name = name
self._active_originally_included = active is not None
self.active = active or False
if not self.css_id:
self.css_id = slugify(self.name, allow_unicode=True)
def __contains__(self, field_name):
"""
check if field_name is contained within tab.
"""
return field_name in (pointer.name for pointer in self.get_field_names())
class ContainerHolder(Div):
"""
Base class used for `TabHolder` and `Accordion`, groups containers.
Attributes
----------
template : str
The default template which this Layout Object will be rendered
with.
css_class : str, optional
CSS classes to be applied to the ``<div>``. By default None.
Parameters
----------
*fields : str, LayoutObject
Any number of fields or layout objects as positional arguments to be
rendered within the ``<div>``.
css_id : str, optional
A DOM id for the layout object which will be added to the ``<div>`` if
provided. By default None.
css_class : str, optional
Additional CSS classes to be applied in addition to those declared by
the class itself. By default None.
template : str, optional
Overrides the default template, if provided. By default None.
**kwargs : dict, optional
Additional attributes are passed to ``flatatt`` and converted into
key="value", pairs. These attributes are added to the ``<div>``.
"""
def first_container_with_errors(self, errors):
"""
Returns the first container with errors, otherwise returns None.
"""
for tab in self.fields:
errors_here = any(error in tab for error in errors)
if errors_here:
return tab
return None
def open_target_group_for_form(self, form):
"""
Makes sure that the first group that should be open is open.
This is either the first group with errors or the first group
in the container, unless that first group was originally set to
active=False.
"""
target = self.first_container_with_errors(form.errors.keys())
if target is None:
target = self.fields[0]
if not getattr(target, "_active_originally_included", None):
target.active = True
return target
target.active = True
return target
class Tab(Container):
"""
Tab object. It wraps fields in a div whose default class is "tab-pane" and
takes a name as first argument.
Attributes
----------
template : str
The default template which this Layout Object will be rendered
with.
css_class : str, optional
CSS classes to be applied to the ``<div>``. By default "".
Parameters
----------
name : str
The name of the container.
*fields : str, LayoutObject
Any number of fields as positional arguments to be rendered within
the container.
css_id : str, optional
A DOM id for the layout object which will be added to the ``<div>`` if
provided. By default None.
css_class : str, optional
Additional CSS classes to be applied in addition to those declared by
the class itself. By default None.
template : str, optional
Overrides the default template, if provided. By default None.
**kwargs : dict, optional
Additional attributes are passed to ``flatatt`` and converted into
key="value", pairs. These attributes are added to the ``<div>``.
Examples
--------
Example::
Tab('tab_name', 'form_field_1', 'form_field_2', 'form_field_3')
"""
css_class = "tab-pane"
link_template = "%s/layout/tab-link.html"
def render_link(self, template_pack=TEMPLATE_PACK, **kwargs):
"""
Render the link for the tab-pane. It must be called after render so css_class is updated
with active if needed.
"""
link_template = self.link_template % template_pack
return render_to_string(link_template, {"link": self})
def render(self, form, context, template_pack=TEMPLATE_PACK, **kwargs):
if self.active:
if "active" not in self.css_class:
self.css_class += " active"
else:
self.css_class = self.css_class.replace("active", "")
return super().render(form, context, template_pack)
class TabHolder(ContainerHolder):
"""
TabHolder object. It wraps Tab objects in a container.
Attributes
----------
template : str
The default template which this Layout Object will be rendered
with.
css_class : str, optional
CSS classes to be applied to the ``<div>``. By default None.
Parameters
----------
*fields : str, LayoutObject
Any number of fields or layout objects as positional arguments to be
rendered within the ``<div>``.
css_id : str, optional
A DOM id for the layout object which will be added to the ``<div>`` if
provided. By default None.
css_class : str, optional
Additional CSS classes to be applied in addition to those declared by
the class itself. By default None.
template : str, optional
Overrides the default template, if provided. By default None.
**kwargs : dict, optional
Additional attributes are passed to ``flatatt`` and converted into
key="value", pairs. These attributes are added to the ``<div>``.
Examples
--------
Example::
TabHolder(
Tab('form_field_1', 'form_field_2'),
Tab('form_field_3')
)
"""
template = "%s/layout/tab.html"
def render(self, form, context, template_pack=TEMPLATE_PACK, **kwargs):
for tab in self.fields:
tab.active = False
# Open the group that should be open.
self.open_target_group_for_form(form)
content = self.get_rendered_fields(form, context, template_pack)
links = SafeString("".join(tab.render_link(template_pack) for tab in self.fields))
context.update({"tabs": self, "links": links, "content": content})
template = self.get_template_name(template_pack)
return render_to_string(template, context.flatten())
class AccordionGroup(Container):
"""
Accordion Group (pane) object. It wraps given fields inside an accordion
tab. It takes accordion tab name as first argument.
Tab object. It wraps fields in a div whose default class is "tab-pane" and
takes a name as first argument.
Attributes
----------
template : str
The default template which this Layout Object will be rendered
with.
css_class : str, optional
CSS classes to be applied to the ``<div>``. By default "".
Parameters
----------
name : str
The name of the container.
*fields : str, LayoutObject
Any number of fields as positional arguments to be rendered within
the container.
css_id : str, optional
A DOM id for the layout object which will be added to the ``<div>`` if
provided. By default None.
css_class : str, optional
Additional CSS classes to be applied in addition to those declared by
the class itself. By default None.
template : str, optional
Overrides the default template, if provided. By default None.
**kwargs : dict, optional
Additional attributes are passed to ``flatatt`` and converted into
key="value", pairs. These attributes are added to the ``<div>``.
Examples
--------
Example::
AccordionGroup("group name", "form_field_1", "form_field_2")
"""
template = "%s/accordion-group.html"
data_parent = "" # accordion parent div id.
class Accordion(ContainerHolder):
"""
Accordion menu object. It wraps `AccordionGroup` objects in a container
Attributes
----------
template : str
The default template which this Layout Object will be rendered
with.
css_class : str, optional
CSS classes to be applied to the ``<div>``. By default None.
Parameters
----------
*accordion_groups : str, LayoutObject
Any number of layout objects as positional arguments to be rendered
within the ``<div>``.
css_id : str, optional
A DOM id for the layout object which will be added to the ``<div>`` if
provided. By default None.
css_class : str, optional
Additional CSS classes to be applied in addition to those declared by
the class itself. By default None.
template : str, optional
Overrides the default template, if provided. By default None.
**kwargs : dict, optional
Additional attributes are passed to ``flatatt`` and converted into
key="value", pairs. These attributes are added to the ``<div>``.
Examples
--------
Example::
Accordion(
AccordionGroup("group name", "form_field_1", "form_field_2"),
AccordionGroup("another group name", "form_field")
)
"""
template = "%s/accordion.html"
def __init__(self, *accordion_groups, css_id=None, css_class=None, template=None, **kwargs):
super().__init__(*accordion_groups, css_id=css_id, css_class=css_class, template=template, **kwargs)
# Accordion needs to have a unique id
if not self.css_id:
self.css_id = "-".join(["accordion", str(randint(1000, 9999))])
# AccordionGroup need to have 'data-parent="#Accordion.id"'
for accordion_group in accordion_groups:
accordion_group.data_parent = self.css_id
def render(self, form, context, template_pack=TEMPLATE_PACK, **kwargs):
content = SafeString("")
# Open the group that should be open.
self.open_target_group_for_form(form)
for group in self.fields:
group.data_parent = self.css_id
content += render_field(group, form, context, template_pack=template_pack, **kwargs)
template = self.get_template_name(template_pack)
context.update({"accordion": self, "content": content})
return render_to_string(template, context.flatten())
class Alert(Div):
"""
Generates markup in the form of an alert dialog.
Attributes
----------
template: str
The default template which this Layout Object will be rendered
with.
css_class : str
The CSS classes to be applied to the alert. By defult "alert".
Parameters
----------
content : str
The content of the alert.
dismiss : bool
If true the alert contains a button to dismiss the alert. By default
True.
block : str, optional
Additional CSS classes to be applied to the ``<button>``. By default
None.
css_id : str, optional
A DOM id for the layout object which will be added to the alert if
provided. By default None.
css_class : str, optional
Additional CSS classes to be applied in addition to those declared by
the class itself. By default None.
template : str, optional
Overrides the default template, if provided. By default None.
**kwargs : dict, optional
Additional attributes are passed to ``flatatt`` and converted into
key="value", pairs. These attributes are then available in the template
context.
Examples
--------
Example::
Alert(content='<strong>Warning!</strong> Best check yo self, you're not looking too good.')
"""
template = "%s/layout/alert.html"
css_class = "alert"
def __init__(self, content, dismiss=True, block=False, css_id=None, css_class=None, template=None, **kwargs):
fields = []
if block:
self.css_class += " alert-block"
super().__init__(*fields, css_id=css_id, css_class=css_class, template=template, **kwargs)
self.content = content
self.dismiss = dismiss
def render(self, form, context, template_pack=TEMPLATE_PACK, **kwargs):
template = self.get_template_name(template_pack)
context.update({"alert": self, "content": self.content, "dismiss": self.dismiss})
return render_to_string(template, context.flatten())
class UneditableField(Field):
"""
Layout object for rendering fields as uneditable in bootstrap.
Attributes
----------
template : str
The default template which this Layout Object will be rendered
with.
attrs : dict
Attributes to be applied to the field. These are converted into html
attributes. e.g. ``data_id: 'test'`` in the attrs dict will become
``data-id='test'`` on the field's ``<input>``.
Parameters
----------
fields : str
The name of the field.
css_class : str, optional
CSS classes to be applied to the field. These are added to any classes
included in the ``attrs`` dict. By default ``None``.
wrapper_class: str, optional
CSS classes to be used when rendering the Field. This class is usually
applied to the ``<div>`` which wraps the Field's ``<label>`` and
``<input>`` tags. By default ``None``.
template : str, optional
Overrides the default template, if provided. By default ``None``.
**kwargs : dict, optional
Additional attributes are converted into key="value", pairs. These
attributes are added to the ``<div>``.
Examples
--------
Example::