|
1 | 1 | from django.db import models
|
| 2 | +from django.db.models import Q |
| 3 | +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
2 | 4 |
|
3 | 5 | from wagtail.models import Page
|
4 | 6 | from wagtail.fields import RichTextField, StreamField
|
5 | 7 | from wagtail.blocks import StreamBlock, CharBlock, URLBlock, RichTextBlock, StructBlock
|
6 | 8 | from wagtail.images.blocks import ImageChooserBlock
|
7 | 9 | from wagtail.admin.panels import FieldPanel, MultiFieldPanel
|
8 | 10 |
|
| 11 | +from app.projects.models import IndividualProjectPage |
9 | 12 |
|
10 |
| -class IndividualBlock(StructBlock): |
| 13 | + |
| 14 | +class UseCaseStructBlock(StructBlock): |
| 15 | + description = RichTextBlock() |
| 16 | + link_text = CharBlock() |
| 17 | + link_url = URLBlock(blank=True, null=True) |
| 18 | + |
| 19 | + |
| 20 | +class UseCaseBlock(StreamBlock): |
| 21 | + blocks = UseCaseStructBlock() |
| 22 | + |
| 23 | + |
| 24 | +class IndividualImpactAreaPage(Page): |
| 25 | + def get_context(self, request, *args, **kwargs): |
| 26 | + context = super().get_context(request, *args, **kwargs) |
| 27 | + |
| 28 | + projects_list = IndividualProjectPage.objects.live().filter( |
| 29 | + Q(impact_area_list__contains=[{'type': 'impact_area', 'value': context['page'].id}]) |
| 30 | + ) |
| 31 | + # print(IndividualProjectPage.objects.first().impact_area_list[0].value.id) |
| 32 | + # print(context['page'].id) |
| 33 | + # print(IndividualProjectPage.objects.first().impact_area_list.__contains__([{'value__id': context['page'].id}])) |
| 34 | + # print(dir(IndividualProjectPage.objects.first().impact_area_list)) |
| 35 | + page = request.GET.get('page', 1) |
| 36 | + paginator = Paginator(projects_list, 4) |
| 37 | + try: |
| 38 | + projects = paginator.page(page) |
| 39 | + except PageNotAnInteger: |
| 40 | + projects = paginator.page(1) |
| 41 | + except EmptyPage: |
| 42 | + projects = paginator.page(paginator.num_pages) |
| 43 | + |
| 44 | + context['projects'] = projects |
| 45 | + other_impact_areas = IndividualImpactAreaPage.objects.live() |
| 46 | + context['other_impact_areas'] = other_impact_areas |
| 47 | + return context |
| 48 | + |
| 49 | + header_image = models.ForeignKey( |
| 50 | + "wagtailimages.Image", |
| 51 | + null=True, |
| 52 | + blank=True, |
| 53 | + on_delete=models.SET_NULL, |
| 54 | + related_name="+", |
| 55 | + help_text="Header image" |
| 56 | + ) |
| 57 | + header_text = RichTextField(blank=True) |
| 58 | + |
| 59 | + external_icon = models.ForeignKey( |
| 60 | + "wagtailimages.Image", |
| 61 | + null=True, |
| 62 | + blank=True, |
| 63 | + on_delete=models.SET_NULL, |
| 64 | + related_name="+", |
| 65 | + help_text="The icon representing this page which is shown for previews of this page on other pages." |
| 66 | + ) |
| 67 | + intro_image = models.ForeignKey( |
| 68 | + "wagtailimages.Image", |
| 69 | + null=True, |
| 70 | + blank=True, |
| 71 | + on_delete=models.SET_NULL, |
| 72 | + related_name="+", |
| 73 | + help_text="Intro image" |
| 74 | + ) |
| 75 | + intro = RichTextField(blank=True) |
| 76 | + description = RichTextField(blank=True) |
| 77 | + |
| 78 | + use_cases_title = models.CharField(default="Use Cases") |
| 79 | + use_cases = StreamField(UseCaseBlock(), use_json_field=True, blank=True, null=True) |
| 80 | + |
| 81 | + projects_title = models.CharField(default="Projects") |
| 82 | + view_all_projects_text = models.CharField(default="View all projects") |
| 83 | + view_all_projects_link = models.URLField(blank=True) |
| 84 | + load_more_projects_text = models.CharField(default="Load More Projects") |
| 85 | + |
| 86 | + explore_impact_areas_text = models.CharField(default="Explore Other Impact Areas") |
| 87 | + |
| 88 | + red_dogear_box_title = models.CharField(blank=True) |
| 89 | + red_dogear_box_link_text = models.CharField(blank=True) |
| 90 | + red_dogear_box_link_url = models.URLField(blank=True) |
| 91 | + |
| 92 | + black_dogear_box_title = models.CharField(blank=True) |
| 93 | + black_dogear_box_link_text = models.CharField(blank=True) |
| 94 | + black_dogear_box_link_url = models.URLField(blank=True) |
| 95 | + |
| 96 | + content_panels = Page.content_panels + [ |
| 97 | + MultiFieldPanel([ |
| 98 | + FieldPanel('header_image'), |
| 99 | + FieldPanel('header_text'), |
| 100 | + ], heading="Header"), |
| 101 | + MultiFieldPanel([ |
| 102 | + FieldPanel('external_icon'), |
| 103 | + FieldPanel('intro_image'), |
| 104 | + FieldPanel('intro'), |
| 105 | + FieldPanel('description'), |
| 106 | + ], heading="Body"), |
| 107 | + MultiFieldPanel([ |
| 108 | + FieldPanel('use_cases_title'), |
| 109 | + FieldPanel('use_cases'), |
| 110 | + ], heading="Use Cases"), |
| 111 | + MultiFieldPanel([ |
| 112 | + FieldPanel('projects_title'), |
| 113 | + FieldPanel('view_all_projects_text'), |
| 114 | + FieldPanel('view_all_projects_link'), |
| 115 | + FieldPanel('load_more_projects_text'), |
| 116 | + ], heading="Projects"), |
| 117 | + FieldPanel('explore_impact_areas_text'), |
| 118 | + MultiFieldPanel([ |
| 119 | + FieldPanel('red_dogear_box_title'), |
| 120 | + FieldPanel('red_dogear_box_link_text'), |
| 121 | + FieldPanel('red_dogear_box_link_url'), |
| 122 | + FieldPanel('black_dogear_box_title'), |
| 123 | + FieldPanel('black_dogear_box_link_text'), |
| 124 | + FieldPanel('black_dogear_box_link_url'), |
| 125 | + ], heading="Dogear Boxes"), |
| 126 | + ] |
| 127 | + |
| 128 | + |
| 129 | +class ImpactAreaStructBlock(StructBlock): |
11 | 130 | image = ImageChooserBlock()
|
12 | 131 | title = CharBlock()
|
13 | 132 | description = RichTextBlock()
|
14 | 133 | link = URLBlock(required=False)
|
15 | 134 |
|
16 | 135 |
|
17 | 136 | class ImpactAreaBlock(StreamBlock):
|
18 |
| - impact_area_block = IndividualBlock() |
| 137 | + impact_area_block = ImpactAreaStructBlock() |
19 | 138 |
|
20 | 139 |
|
21 | 140 | class ImpactAreasPage(Page):
|
|
0 commit comments