From df9234611c0d9496857fb7e6c08d7ebf849d9be5 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Thu, 11 Jan 2018 14:35:11 +0000
Subject: [PATCH] Add the base lab pages

---
 .../funds/migrations/0002_labindex_labpage.py | 62 +++++++++++++++++
 opentech/public/funds/models.py               | 69 +++++++++++++++++++
 2 files changed, 131 insertions(+)
 create mode 100644 opentech/public/funds/migrations/0002_labindex_labpage.py

diff --git a/opentech/public/funds/migrations/0002_labindex_labpage.py b/opentech/public/funds/migrations/0002_labindex_labpage.py
new file mode 100644
index 000000000..58c1f4773
--- /dev/null
+++ b/opentech/public/funds/migrations/0002_labindex_labpage.py
@@ -0,0 +1,62 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.8 on 2018-01-11 14:09
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+import opentech.public.funds.blocks
+import wagtail.wagtailcore.blocks
+import wagtail.wagtailcore.fields
+import wagtail.wagtaildocs.blocks
+import wagtail.wagtailembeds.blocks
+import wagtail.wagtailimages.blocks
+import wagtail.wagtailsnippets.blocks
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('wagtailcore', '0040_page_draft_title'),
+        ('images', '0001_initial'),
+        ('public_funds', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='LabIndex',
+            fields=[
+                ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')),
+                ('social_text', models.CharField(blank=True, max_length=255)),
+                ('listing_title', models.CharField(blank=True, help_text='Override the page title used when this page appears in listings', max_length=255)),
+                ('listing_summary', models.CharField(blank=True, help_text="The text summary used when this page appears in listings. It's also used as the description for search engines if the 'Search description' field above is not defined.", max_length=255)),
+                ('introduction', models.TextField(blank=True)),
+                ('header_image', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='images.CustomImage')),
+                ('listing_image', models.ForeignKey(blank=True, help_text='Choose the image you wish to be displayed when this page appears in listings', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='images.CustomImage')),
+                ('social_image', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='images.CustomImage')),
+            ],
+            options={
+                'abstract': False,
+            },
+            bases=('wagtailcore.page', models.Model),
+        ),
+        migrations.CreateModel(
+            name='LabPage',
+            fields=[
+                ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')),
+                ('social_text', models.CharField(blank=True, max_length=255)),
+                ('listing_title', models.CharField(blank=True, help_text='Override the page title used when this page appears in listings', max_length=255)),
+                ('listing_summary', models.CharField(blank=True, help_text="The text summary used when this page appears in listings. It's also used as the description for search engines if the 'Search description' field above is not defined.", max_length=255)),
+                ('introduction', models.TextField(blank=True)),
+                ('lab_link', models.URLField()),
+                ('body', wagtail.wagtailcore.fields.StreamField((('heading', wagtail.wagtailcore.blocks.CharBlock(classname='full title', icon='title')), ('paragraph', wagtail.wagtailcore.blocks.RichTextBlock()), ('image', wagtail.wagtailcore.blocks.StructBlock((('image', wagtail.wagtailimages.blocks.ImageChooserBlock()), ('caption', wagtail.wagtailcore.blocks.CharBlock(required=False))))), ('quote', wagtail.wagtailcore.blocks.StructBlock((('quote', wagtail.wagtailcore.blocks.CharBlock(classname='title')), ('attribution', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('job_title', wagtail.wagtailcore.blocks.CharBlock(required=False))))), ('embed', wagtail.wagtailembeds.blocks.EmbedBlock()), ('call_to_action', wagtail.wagtailsnippets.blocks.SnippetChooserBlock('utils.CallToActionSnippet', template='blocks/call_to_action_block.html')), ('document', wagtail.wagtailcore.blocks.StructBlock((('document', wagtail.wagtaildocs.blocks.DocumentChooserBlock()), ('title', wagtail.wagtailcore.blocks.CharBlock(required=False))))), ('project_list', opentech.public.funds.blocks.ProjectsBlock()), ('reviewer_list', opentech.public.funds.blocks.ReviewersBlock())))),
+                ('header_image', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='images.CustomImage')),
+                ('lab_type', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailcore.Page')),
+                ('listing_image', models.ForeignKey(blank=True, help_text='Choose the image you wish to be displayed when this page appears in listings', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='images.CustomImage')),
+                ('social_image', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='images.CustomImage')),
+            ],
+            options={
+                'abstract': False,
+            },
+            bases=('wagtailcore.page', models.Model),
+        ),
+    ]
diff --git a/opentech/public/funds/models.py b/opentech/public/funds/models.py
index c5b012db1..6c762f70e 100644
--- a/opentech/public/funds/models.py
+++ b/opentech/public/funds/models.py
@@ -1,9 +1,11 @@
 from django.conf import settings
+from django.core.exceptions import ValidationError
 from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
 from django.db import models
 
 from wagtail.wagtailadmin.edit_handlers import (
     FieldPanel,
+    MultiFieldPanel,
     PageChooserPanel,
     StreamFieldPanel,
 )
@@ -55,3 +57,70 @@ class FundIndex(BasePage):
         context = super().get_context(request, *args, **kwargs)
         context.update(news=news)
         return context
+
+
+class LabPage(BasePage):
+    subpage_types = []
+    parent_page_types = ['LabIndex']
+
+    introduction = models.TextField(blank=True)
+    lab_type = models.ForeignKey(
+        'wagtailcore.Page',
+        blank=True,
+        null=True,
+        on_delete=models.SET_NULL,
+        related_name='+',
+    )
+    lab_link = models.URLField(blank=True, verbose_name="External link")
+    body = StreamField(FundBlock())
+
+    content_panels = BasePage.content_panels + [
+        FieldPanel('introduction'),
+        MultiFieldPanel([
+            # Limit to lab pages once created
+            PageChooserPanel('lab_type'),
+            FieldPanel('lab_link'),
+        ], heading='Link for lab application'),
+        StreamFieldPanel('body'),
+    ]
+
+    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):
+        funds = LabPage.objects.live().public().descendant_of(self)
+
+        # Pagination
+        page = request.GET.get('page', 1)
+        paginator = Paginator(funds, settings.DEFAULT_PER_PAGE)
+        try:
+            news = paginator.page(page)
+        except PageNotAnInteger:
+            news = paginator.page(1)
+        except EmptyPage:
+            news = paginator.page(paginator.num_pages)
+
+        context = super().get_context(request, *args, **kwargs)
+        context.update(news=news)
+        return context
-- 
GitLab