Skip to content
Snippets Groups Projects
models.py 4.08 KiB
Newer Older
  • Learn to ignore specific revisions
  • from django.conf import settings
    
    Todd Dembrey's avatar
    Todd Dembrey committed
    from django.core.exceptions import ValidationError
    
    from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
    
    Todd Dembrey's avatar
    Todd Dembrey committed
    from django.db import models
    
    
    from wagtail.wagtailadmin.edit_handlers import (
        FieldPanel,
    
    Todd Dembrey's avatar
    Todd Dembrey committed
        MultiFieldPanel,
    
        PageChooserPanel,
        StreamFieldPanel,
    )
    from wagtail.wagtailcore.fields import StreamField
    
    
    from opentech.public.utils.models import BasePage
    
    from .blocks import FundBlock
    
    
    
    class FundPage(BasePage):
        subpage_types = []
        parent_page_types = ['FundIndex']
    
        introduction = models.TextField(blank=True)
        fund_type = models.ForeignKey(
            'wagtailcore.Page',
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            blank=True,
            null=True,
    
            on_delete=models.SET_NULL,
            related_name='+',
        )
    
        body = StreamField(FundBlock())
    
    
        content_panels = BasePage.content_panels + [
            FieldPanel('introduction'),
    
            PageChooserPanel('fund_type', 'funds.FundType'),
    
            StreamFieldPanel('body'),
        ]
    
    
    
    Todd Dembrey's avatar
    Todd Dembrey committed
    class FundIndex(BasePage):
    
        subpage_types = ['FundPage']
        parent_page_types = ['home.HomePage']
    
        def get_context(self, request, *args, **kwargs):
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            funds = FundPage.objects.live().public().descendant_of(self)
    
    
            # Pagination
            page = request.GET.get('page', 1)
            paginator = Paginator(funds, settings.DEFAULT_PER_PAGE)
            try:
    
    Todd Dembrey's avatar
    Todd Dembrey committed
                funds = paginator.page(page)
    
            except PageNotAnInteger:
    
    Todd Dembrey's avatar
    Todd Dembrey committed
                funds = paginator.page(1)
    
            except EmptyPage:
    
    Todd Dembrey's avatar
    Todd Dembrey committed
                funds = paginator.page(paginator.num_pages)
    
    
            context = super().get_context(request, *args, **kwargs)
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            context.update(subpages=funds)
    
            return context
    
    Todd Dembrey's avatar
    Todd Dembrey committed
    
    
    class LabPage(BasePage):
        subpage_types = []
        parent_page_types = ['LabIndex']
    
        introduction = models.TextField(blank=True)
    
    Todd Dembrey's avatar
    Todd Dembrey committed
        icon = models.ForeignKey(
            'images.CustomImage',
            null=True,
            blank=True,
            related_name='+',
            on_delete=models.SET_NULL
        )
    
    Todd Dembrey's avatar
    Todd Dembrey committed
        lab_type = models.ForeignKey(
            'wagtailcore.Page',
            blank=True,
            null=True,
            on_delete=models.SET_NULL,
            related_name='+',
        )
    
    Todd Dembrey's avatar
    Todd Dembrey committed
        lab_link = models.URLField(blank=True, verbose_name='External link')
        link_text = models.CharField(max_length=255, help_text='text to display on the button')
    
    Todd Dembrey's avatar
    Todd Dembrey committed
        body = StreamField(FundBlock())
    
        content_panels = BasePage.content_panels + [
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            FieldPanel('icon'),
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            FieldPanel('introduction'),
            MultiFieldPanel([
                # Limit to lab pages once created
                PageChooserPanel('lab_type'),
                FieldPanel('lab_link'),
    
    Todd Dembrey's avatar
    Todd Dembrey committed
                FieldPanel('link_text'),
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            ], heading='Link for lab application'),
            StreamFieldPanel('body'),
        ]
    
    
    Todd Dembrey's avatar
    Todd Dembrey committed
        @property
        def link_to_lab(self):
            return self.lab_link or self.lab_type.get_url()
    
    
    Todd Dembrey's avatar
    Todd Dembrey committed
        def clean(self):
            if self.lab_type and self.lab_link:
                raise ValidationError({
                    'lab_type': 'Cannot link to both a Lab page and external link',
                    'lab_link': 'Cannot link to both a Lab page and external link',
                })
    
            if not self.lab_type and not self.lab_link:
                raise ValidationError({
                    'lab_type': 'Please provide a way for applicants to apply',
                    'lab_link': 'Please provide a way for applicants to apply',
                })
    
    
    class LabIndex(BasePage):
        subpage_types = ['LabPage']
        parent_page_types = ['home.HomePage']
    
        introduction = models.TextField(blank=True)
    
        content_panels = BasePage.content_panels + [
            FieldPanel('introduction')
        ]
    
        def get_context(self, request, *args, **kwargs):
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            labs = LabPage.objects.live().public().descendant_of(self)
    
    Todd Dembrey's avatar
    Todd Dembrey committed
    
            # Pagination
            page = request.GET.get('page', 1)
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            paginator = Paginator(labs, settings.DEFAULT_PER_PAGE)
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            try:
    
    Todd Dembrey's avatar
    Todd Dembrey committed
                labs = paginator.page(page)
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            except PageNotAnInteger:
    
    Todd Dembrey's avatar
    Todd Dembrey committed
                labs = paginator.page(1)
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            except EmptyPage:
    
    Todd Dembrey's avatar
    Todd Dembrey committed
                labs = paginator.page(paginator.num_pages)
    
    Todd Dembrey's avatar
    Todd Dembrey committed
    
            context = super().get_context(request, *args, **kwargs)
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            context.update(subpages=labs)
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            return context