-
Notifications
You must be signed in to change notification settings - Fork 57
/
models.py
207 lines (180 loc) · 5.84 KB
/
models.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
from functools import partial
from django.db import models
from django.utils.translation import gettext as _
from django.utils.translation import ngettext
from cms.models import CMSPlugin
from djangocms_bootstrap4.constants import DEVICE_SIZES
from djangocms_bootstrap4.fields import AttributesField, IntegerRangeField, TagTypeField
from djangocms_bootstrap4.helpers import (
get_choices_match,
get_first_choice,
mark_safe_lazy,
)
from .constants import (
GRID_COLUMN_ALIGNMENT_CHOICES,
GRID_COLUMN_CHOICES,
GRID_CONTAINER_CHOICES,
GRID_ROW_HORIZONTAL_ALIGNMENT_CHOICES,
GRID_ROW_VERTICAL_ALIGNMENT_CHOICES,
GRID_SIZE,
)
class Bootstrap4GridContainer(CMSPlugin):
"""
Layout > Grid: "Container" Plugin
https://getbootstrap.com/docs/4.0/layout/grid/
"""
container_type = models.CharField(
verbose_name=_("Container type"),
choices=GRID_CONTAINER_CHOICES,
default=get_first_choice(GRID_CONTAINER_CHOICES),
max_length=255,
help_text=mark_safe_lazy(
_(
"Defines if the grid should use fixed width (<code>.container</code>) "
"or fluid width (<code>.container-fluid</code>)."
)
),
)
tag_type = TagTypeField()
attributes = AttributesField()
def __str__(self):
return str(self.pk)
def get_short_description(self):
choice = get_choices_match(GRID_CONTAINER_CHOICES, self.container_type)
return f"({choice})"
class Bootstrap4GridRow(CMSPlugin):
"""
Layout > Grid: "Row" Plugin
https://getbootstrap.com/docs/4.0/layout/grid/
"""
vertical_alignment = models.CharField(
verbose_name=_("Vertical alignment"),
choices=GRID_ROW_VERTICAL_ALIGNMENT_CHOICES,
blank=True,
max_length=255,
help_text=mark_safe_lazy(
_(
'Read more in the <a href="{link}" target="_blank">documentation</a>.'
).format(
link="https://getbootstrap.com/docs/4.0/layout/grid/#vertical-alignment"
)
),
)
horizontal_alignment = models.CharField(
verbose_name=_("Horizontal alignment"),
choices=GRID_ROW_HORIZONTAL_ALIGNMENT_CHOICES,
blank=True,
max_length=255,
help_text=mark_safe_lazy(
_(
'Read more in the <a href="{link}" target="_blank">documentation</a>.'
).format(
link="https://getbootstrap.com/docs/4.0/layout/grid/#horizontal-alignment"
)
),
)
gutters = models.BooleanField(
verbose_name=_("Remove gutters"),
default=False,
help_text=_("Removes the marginal gutters from the grid."),
)
tag_type = TagTypeField()
attributes = AttributesField()
def __str__(self):
return str(self.pk)
def get_short_description(self):
column_count = len(self.child_plugin_instances or [])
column_count_str = ngettext(
"(1 column)", "(%(count)i columns)", column_count
) % {"count": column_count}
return column_count_str
class Bootstrap4GridColumn(CMSPlugin):
"""
Layout > Grid: "Column" Plugin
https://getbootstrap.com/docs/4.0/layout/grid/
"""
column_type = models.CharField(
verbose_name=_("Column type"),
choices=GRID_COLUMN_CHOICES,
default=GRID_COLUMN_CHOICES[0][0],
blank=True,
max_length=255,
)
column_alignment = models.CharField(
verbose_name=_("Alignment"),
choices=GRID_COLUMN_ALIGNMENT_CHOICES,
blank=True,
max_length=255,
)
tag_type = TagTypeField()
attributes = AttributesField()
def __str__(self):
return str(self.pk)
def get_short_description(self):
text = ""
classes = self.get_grid_values()
if self.xs_col:
text += f"(col-{self.xs_col}) "
else:
text += "(auto) "
if self.column_type != "col":
text += f".{self.column_type} "
if classes:
text += ".{}".format(" .".join(self.get_grid_values()))
return text
def get_grid_values(self):
classes = []
for device in DEVICE_SIZES:
for element in ("col", "order", "offset", "ml", "mr"):
size = getattr(self, f"{device}_{element}")
if isinstance(size, int) and (
element == "col" or element == "order" or element == "offset"
):
if device == "xs":
classes.append(f"{element}-{int(size)}")
else:
classes.append(f"{element}-{device}-{int(size)}")
elif size:
if device == "xs":
classes.append("{}-{}".format(element, "auto"))
else:
classes.append("{}-{}-{}".format(element, device, "auto"))
return classes
IntegerRangeFieldPartial = partial(
IntegerRangeField,
blank=True,
null=True,
max_value=GRID_SIZE,
)
BooleanFieldPartial = partial(
models.BooleanField,
default=False,
)
# Loop through Bootstrap 4 device choices and generate
# model fields to cover col-*, order-*, offset-*, etc.
for size in DEVICE_SIZES:
# Grid size
Bootstrap4GridColumn.add_to_class(
f"{size}_col",
IntegerRangeFieldPartial(),
)
# Grid ordering
Bootstrap4GridColumn.add_to_class(
f"{size}_order",
IntegerRangeFieldPartial(),
)
# Grid offset
Bootstrap4GridColumn.add_to_class(
f"{size}_offset",
IntegerRangeFieldPartial(),
)
# Grid margin left (ml)
Bootstrap4GridColumn.add_to_class(
f"{size}_ml",
BooleanFieldPartial(),
)
# Grid margin right (ml)
Bootstrap4GridColumn.add_to_class(
f"{size}_mr",
BooleanFieldPartial(),
)