Skip to content

Commit ac485e1

Browse files
committed
refactor: ♻️ Sort Code in Models
1 parent da2cdf4 commit ac485e1

File tree

1 file changed

+52
-35
lines changed

1 file changed

+52
-35
lines changed

home/models.py

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from django.db import models
2-
from wagtail.fields import RichTextField, StreamField
3-
from wagtail.models import Page
4-
from wagtail.admin.panels import FieldPanel
52
from wagtail import blocks
3+
from wagtail.admin.panels import FieldPanel
4+
from wagtail.fields import RichTextField, StreamField
65
from wagtail.images.blocks import ImageChooserBlock
7-
from wagtail_color_panel.fields import ColorField
6+
from wagtail.models import Page
87
from wagtail_color_panel.edit_handlers import NativeColorPanel
8+
from wagtail_color_panel.fields import ColorField
9+
910

1011
class HomePage(Page):
1112
event = RichTextField(blank=True)
@@ -48,7 +49,9 @@ class HomePage(Page):
4849
on_delete=models.SET_NULL,
4950
related_name="+",
5051
)
51-
hero_image_overlay_opacity = models.DecimalField(default=0.70, decimal_places=2, max_digits=3)
52+
hero_image_overlay_opacity = models.DecimalField(
53+
default=0.70, decimal_places=2, max_digits=3
54+
)
5255
hero_padding_left = models.CharField(max_length=250, default="20px")
5356
hero_padding_right = models.CharField(max_length=250, default="20px")
5457
footer_title = models.CharField(max_length=250, blank=True)
@@ -80,45 +83,52 @@ class HomePage(Page):
8083
)
8184

8285
primaryFontURL = models.URLField(max_length=250, blank=True)
83-
primaryFontFamily= models.CharField(max_length=250, default="sans-serif")
86+
primaryFontFamily = models.CharField(max_length=250, default="sans-serif")
8487
secondaryFontURL = models.URLField(max_length=250, blank=True)
8588
secondaryFontFamily = models.CharField(max_length=250, default="sans-serif")
8689
color_gradient_1 = ColorField(max_length=250, blank=True, default="#FF4D79")
8790
color_gradient_2 = ColorField(max_length=250, blank=True, default="#FF809F")
8891
color_primary = ColorField(max_length=250, blank=True, default="#ff4a67")
89-
color_text_hero = ColorField(max_length=250, blank=True, default="#FFFFFF")
92+
color_text_hero = ColorField(max_length=250, blank=True, default="#FFFFFF")
9093

9194
show_time = models.BooleanField(default=True)
9295
background_section = ColorField(max_length=250, blank=True, default="#FFFFFF")
93-
color_text_section = ColorField(max_length=250, blank=True, default="#FFFFFF")
94-
navbar_background = ColorField(max_length=250, blank=True, default="#000000")
95-
navbar_links_color = ColorField(max_length=250, blank=True, default="#FFFFFF")
96-
message_show_time = models.CharField(max_length=250, blank=True, default="Próximamente...")
96+
color_text_section = ColorField(max_length=250, blank=True, default="#FFFFFF")
97+
navbar_background = ColorField(max_length=250, blank=True, default="#000000")
98+
navbar_links_color = ColorField(max_length=250, blank=True, default="#FFFFFF")
99+
message_show_time = models.CharField(
100+
max_length=250, blank=True, default="Próximamente..."
101+
)
97102
menu_links = StreamField(
98-
[
99-
(
100-
"menu_links",
101-
blocks.StructBlock(
102-
[
103-
("text", blocks.CharBlock(max_length=250, required=True)),
104-
("url",blocks.CharBlock(max_length=250, required=True, default="#")),
105-
]
106-
),
103+
[
104+
(
105+
"menu_links",
106+
blocks.StructBlock(
107+
[
108+
("text", blocks.CharBlock(max_length=250, required=True)),
109+
(
110+
"url",
111+
blocks.CharBlock(
112+
max_length=250, required=True, default="#"
113+
),
114+
),
115+
]
107116
),
108-
],
109-
blank=True,
110-
use_json_field=True,
111-
)
117+
),
118+
],
119+
blank=True,
120+
use_json_field=True,
121+
)
112122
favicon = models.ForeignKey(
113123
"wagtailimages.Image",
114124
null=True,
115125
blank=True,
116126
on_delete=models.SET_NULL,
117127
related_name="+",
118-
help_text="max size 256x256 png format"
128+
help_text="max size 256x256 png format",
119129
)
120130
footer_background = models.CharField(max_length=250, blank=True, default="#161E26")
121-
footer_color_text = models.CharField(max_length=250, blank=True, default="#9e9e9e")
131+
footer_color_text = models.CharField(max_length=250, blank=True, default="#9e9e9e")
122132
externalRaws = StreamField(
123133
[
124134
(
@@ -152,16 +162,13 @@ class HomePage(Page):
152162
NativeColorPanel("color_text_hero"),
153163
FieldPanel("hero_padding_left"),
154164
FieldPanel("hero_padding_right"),
155-
156165
FieldPanel("primaryFontURL"),
157166
FieldPanel("primaryFontFamily"),
158167
FieldPanel("secondaryFontURL"),
159168
FieldPanel("secondaryFontFamily"),
160-
161169
NativeColorPanel("color_gradient_1"),
162170
NativeColorPanel("color_gradient_2"),
163171
NativeColorPanel("color_primary"),
164-
165172
FieldPanel("show_time"),
166173
NativeColorPanel("background_section"),
167174
NativeColorPanel("color_text_section"),
@@ -175,13 +182,17 @@ class HomePage(Page):
175182
NativeColorPanel("footer_color_text"),
176183
FieldPanel("externalRaws"),
177184
]
178-
185+
179186
def get_context(self, request):
180187
# Update context to include only published posts, ordered by reverse-chron
181188
context = super().get_context(request)
182189
segments = SegmentPage.objects.child_of(self).live().order_by("order")
183190
context["segments"] = segments # sorted (segments, key=lambda x: x.order)
184-
context["exist_descendants"] = [descendant for descendant in self.get_descendants() if descendant.live and descendant.content_type == self.content_type]
191+
context["exist_descendants"] = [
192+
descendant
193+
for descendant in self.get_descendants()
194+
if descendant.live and descendant.content_type == self.content_type
195+
]
185196
return context
186197

187198

@@ -204,6 +215,7 @@ class SponsorBlock(blocks.StructBlock):
204215
)
205216
url = blocks.URLBlock(required=False)
206217

218+
207219
class TiersBlock(blocks.StructBlock):
208220
classname = blocks.CharBlock(required=False)
209221
name = blocks.CharBlock()
@@ -216,6 +228,7 @@ class TiersBlock(blocks.StructBlock):
216228
url_text = blocks.CharBlock(required=False, default="Buy now")
217229
bg_color = blocks.CharBlock(required=False, default="#fff")
218230

231+
219232
class ScheduleDetailBlock(blocks.StructBlock):
220233
menu_title = blocks.CharBlock()
221234
menu_subtitle = blocks.CharBlock(default="Workshop")
@@ -226,15 +239,17 @@ class ScheduleDetailBlock(blocks.StructBlock):
226239
location = blocks.CharBlock(required=False)
227240
image = ImageChooserBlock(required=False)
228241

242+
229243
class ScheduleBlock(blocks.StructBlock):
230244
name = blocks.CharBlock()
231245
detail = blocks.CharBlock()
232246
schedule_details = blocks.ListBlock(ScheduleDetailBlock(required=False))
233247

248+
234249
class SegmentPage(Page):
235250
order = models.IntegerField(default=0)
236251
background_section = ColorField(max_length=250, blank=True, default="#FFFFFF")
237-
color_text_section = ColorField(max_length=250, blank=True, default="#000000")
252+
color_text_section = ColorField(max_length=250, blank=True, default="#000000")
238253
segments = StreamField(
239254
[
240255
# detail segment
@@ -269,7 +284,8 @@ class SegmentPage(Page):
269284
),
270285
),
271286
(
272-
"list_style",blocks.BooleanBlock(required=False, default=False),
287+
"list_style",
288+
blocks.BooleanBlock(required=False, default=False),
273289
),
274290
]
275291
),
@@ -381,9 +397,10 @@ class SegmentPage(Page):
381397
FieldPanel("order"),
382398
NativeColorPanel("color_text_section"),
383399
FieldPanel("segments"),
384-
NativeColorPanel("background_section")
400+
NativeColorPanel("background_section"),
385401
]
402+
386403
def get_context(self, request):
387404
context = super().get_context(request)
388-
context['parent'] = self.get_parent().specific
405+
context["parent"] = self.get_parent().specific
389406
return context

0 commit comments

Comments
 (0)