From 771ca8cd5b9e975fd798a14befd1d488b209a1a7 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Wed, 14 Feb 2018 17:34:41 +0000
Subject: [PATCH] Add the lead to the round and ensure that all OTF staff are
 staff

---
 .../apply/funds/migrations/0021_round_lead.py | 24 +++++++++++++++++++
 opentech/apply/funds/models.py                |  2 ++
 opentech/apply/users/models.py                |  3 +++
 opentech/apply/users/piplines.py              |  8 +++++++
 opentech/settings/base.py                     |  4 +++-
 5 files changed, 40 insertions(+), 1 deletion(-)
 create mode 100644 opentech/apply/funds/migrations/0021_round_lead.py
 create mode 100644 opentech/apply/users/piplines.py

diff --git a/opentech/apply/funds/migrations/0021_round_lead.py b/opentech/apply/funds/migrations/0021_round_lead.py
new file mode 100644
index 000000000..6d9d8c237
--- /dev/null
+++ b/opentech/apply/funds/migrations/0021_round_lead.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.8 on 2018-02-14 17:21
+from __future__ import unicode_literals
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+        ('funds', '0020_applicationsubmission_form_fields'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='round',
+            name='lead',
+            field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
+            preserve_default=False,
+        ),
+    ]
diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py
index 8d6a4d65a..1bf83fec5 100644
--- a/opentech/apply/funds/models.py
+++ b/opentech/apply/funds/models.py
@@ -274,6 +274,7 @@ class Round(WorkflowStreamForm, SubmittableStreamForm):  # type: ignore
     parent_page_types = ['funds.FundType']
     subpage_types = []  # type: ignore
 
+    lead = models.ForeignKey(settings.AUTH_USER_MODEL, limit_choices_to={'is_staff': True})
     start_date = models.DateField(default=date.today)
     end_date = models.DateField(
         blank=True,
@@ -283,6 +284,7 @@ class Round(WorkflowStreamForm, SubmittableStreamForm):  # type: ignore
     )
 
     content_panels = SubmittableStreamForm.content_panels + [
+        FieldPanel('lead'),
         MultiFieldPanel([
             FieldRowPanel([
                 FieldPanel('start_date'),
diff --git a/opentech/apply/users/models.py b/opentech/apply/users/models.py
index 5e39dfb2e..162d1d1f5 100644
--- a/opentech/apply/users/models.py
+++ b/opentech/apply/users/models.py
@@ -69,3 +69,6 @@ class User(AbstractUser):
     username = None
 
     objects = UserManager()
+
+    def __str__(self):
+        return self.get_full_name()
diff --git a/opentech/apply/users/piplines.py b/opentech/apply/users/piplines.py
new file mode 100644
index 000000000..0f608e6b1
--- /dev/null
+++ b/opentech/apply/users/piplines.py
@@ -0,0 +1,8 @@
+from django.conf import settings
+
+
+def make_otf_staff(backend, user, response, *args, **kwargs):
+    _, email_domain = user.email.split('@')
+    if email_domain in settings.STAFF_EMAIL_DOMAINS:
+        user.is_staff = True
+        user.save()
diff --git a/opentech/settings/base.py b/opentech/settings/base.py
index 98a914eb5..b90b8a368 100644
--- a/opentech/settings/base.py
+++ b/opentech/settings/base.py
@@ -298,7 +298,8 @@ SOCIAL_AUTH_URL_NAMESPACE = 'social'
 # Set the Google OAuth2 credentials in ENV variables or local.py
 # To create a new set of credentials, go to https://console.developers.google.com/apis/credentials
 # Make sure the Google+ API is enabled for your API project
-SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = ['opentechfund.org']
+STAFF_EMAIL_DOMAINS = ['opentechfund.org']
+SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = STAFF_EMAIL_DOMAINS
 SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ''
 SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ''
 
@@ -318,6 +319,7 @@ SOCIAL_AUTH_PIPELINE = (
     'social_core.pipeline.social_auth.associate_user',
     'social_core.pipeline.social_auth.load_extra_data',
     'social_core.pipeline.user.user_details',
+    'opentech.apply.users.pipeline.make_otf_staff',
 )
 
 # Bleach Settings
-- 
GitLab