From f531e6693c7f01fd36826afffcba6f781bcc12b6 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Fri, 22 Dec 2017 09:48:36 +0000
Subject: [PATCH] Update apply to use its own site and make funds a page

---
 opentech/apply/admin.py                       | 19 +-------
 opentech/apply/migrations/0001_initial.py     | 30 +++++++++++--
 .../migrations/0002_create_apply_homepage.py  | 44 +++++++++++++++++++
 opentech/apply/models.py                      | 31 ++++++++++++-
 opentech/apply/wagtail_hooks.py               |  6 ---
 opentech/settings/dev.py                      |  2 +
 6 files changed, 103 insertions(+), 29 deletions(-)
 create mode 100644 opentech/apply/migrations/0002_create_apply_homepage.py
 delete mode 100644 opentech/apply/wagtail_hooks.py

diff --git a/opentech/apply/admin.py b/opentech/apply/admin.py
index 7e15ae656..4185d360e 100644
--- a/opentech/apply/admin.py
+++ b/opentech/apply/admin.py
@@ -1,18 +1,3 @@
-from wagtail.contrib.modeladmin.options import ModelAdmin, ModelAdminGroup
+# from django.contrib import admin
 
-from .models import Fund
-
-
-class FundModelAdmin(ModelAdmin):
-    model = Fund
-    menu_icon = 'site'
-    list_display = ('name',)
-    list_filter = ('name',)
-    search_fields = ('name',)
-
-
-class ApplyAdminGroup(ModelAdminGroup):
-    menu_label = 'Apply'
-    menu_icon = 'folder-open-inverse'
-    menu_order = 200
-    items = (FundModelAdmin,)
+# Register your models here.
diff --git a/opentech/apply/migrations/0001_initial.py b/opentech/apply/migrations/0001_initial.py
index cc00366f1..916e8f8f5 100644
--- a/opentech/apply/migrations/0001_initial.py
+++ b/opentech/apply/migrations/0001_initial.py
@@ -1,8 +1,9 @@
 # -*- coding: utf-8 -*-
-# Generated by Django 1.11.7 on 2017-12-19 09:33
+# Generated by Django 1.11.7 on 2017-12-22 09:28
 from __future__ import unicode_literals
 
 from django.db import migrations, models
+import django.db.models.deletion
 
 
 class Migration(migrations.Migration):
@@ -10,14 +11,35 @@ class Migration(migrations.Migration):
     initial = True
 
     dependencies = [
+        ('images', '0001_initial'),
+        ('wagtailcore', '0040_page_draft_title'),
     ]
 
     operations = [
         migrations.CreateModel(
-            name='Fund',
+            name='ApplyHomePage',
             fields=[
-                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
-                ('name', models.CharField(max_length=60)),
+                ('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)),
+                ('strapline', models.CharField(blank=True, max_length=255)),
+                ('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='FundPage',
+            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')),
+            ],
+            options={
+                'abstract': False,
+            },
+            bases=('wagtailcore.page',),
         ),
     ]
diff --git a/opentech/apply/migrations/0002_create_apply_homepage.py b/opentech/apply/migrations/0002_create_apply_homepage.py
new file mode 100644
index 000000000..d6bce1aed
--- /dev/null
+++ b/opentech/apply/migrations/0002_create_apply_homepage.py
@@ -0,0 +1,44 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+def create_homepage(apps, schema_editor):
+    # Get models
+    ContentType = apps.get_model('contenttypes.ContentType')
+    Page = apps.get_model('wagtailcore.Page')
+    Site = apps.get_model('wagtailcore.Site')
+    ApplyHomePage = apps.get_model('apply.ApplyHomePage')
+
+    # Create content type for homepage model
+    homepage_content_type, created = ContentType.objects.get_or_create(
+        model='applyhomepage', app_label='apply')
+
+    # Create a new homepage
+    applyhomepage = ApplyHomePage.objects.create(
+        title="ApplyHomepage",
+        draft_title="ApplyHomepage",
+        slug='apply',
+        content_type=homepage_content_type,
+        path='00010002',
+        depth=2,
+        numchild=0,
+        url_path='/apply/',
+    )
+
+    # Create a site with the new homepage set as the root
+    Site.objects.create(
+        hostname='apply.localhost', root_page=applyhomepage, is_default_site=True)
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('apply', '0001_initial'),
+        ('home', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.RunPython(create_homepage),
+    ]
diff --git a/opentech/apply/models.py b/opentech/apply/models.py
index 8b0a89a1b..d23470729 100644
--- a/opentech/apply/models.py
+++ b/opentech/apply/models.py
@@ -1,5 +1,32 @@
 from django.db import models
+from wagtail.wagtailadmin.edit_handlers import FieldPanel
+from wagtail.wagtailcore.models import Page
+from wagtail.wagtailsearch import index
+from wagtail.wagtailsnippets.edit_handlers import SnippetChooserPanel
 
+from opentech.utils.models import SocialFields, ListingFields
 
-class Fund(models.Model):
-    name = models.CharField(max_length=60)
+
+class ApplyHomePage(Page, SocialFields, ListingFields):
+    # Only allow creating HomePages at the root level
+    parent_page_types = ['wagtailcore.Page']
+
+    strapline = models.CharField(blank=True, max_length=255)
+
+    search_fields = Page.search_fields + [
+        index.SearchField('strapline'),
+    ]
+
+    content_panels = Page.content_panels + [
+        FieldPanel('strapline'),
+    ]
+
+    promote_panels = (
+        Page.promote_panels +
+        SocialFields.promote_panels +
+        ListingFields.promote_panels
+    )
+
+
+class FundPage(Page):
+    parent_page_types = [ApplyHomePage]
diff --git a/opentech/apply/wagtail_hooks.py b/opentech/apply/wagtail_hooks.py
deleted file mode 100644
index d9ad3c978..000000000
--- a/opentech/apply/wagtail_hooks.py
+++ /dev/null
@@ -1,6 +0,0 @@
-from wagtail.contrib.modeladmin.options import modeladmin_register
-
-from .admin import ApplyAdminGroup
-
-
-modeladmin_register(ApplyAdminGroup)
diff --git a/opentech/settings/dev.py b/opentech/settings/dev.py
index fb9385a42..388ccda6a 100644
--- a/opentech/settings/dev.py
+++ b/opentech/settings/dev.py
@@ -8,6 +8,8 @@ SECRET_KEY = 'CHANGEME!!!'
 
 INTERNAL_IPS = ('127.0.0.1', '10.0.2.2')
 
+ALLOWED_HOSTS= ['apply.localhost', 'localhost']
+
 BASE_URL = 'http://localhost:8000'
 
 EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
-- 
GitLab