Repository: AlexsLemonade/refinebio

Issue:
Filter out our emails from engagement bots
### Context

We don't want notifications when internal users download datasets from refine.bio. Also we don't want any notifications when we download datasets from staging ever.

### Problem or idea

Check the environment flag to make sure we only email when in prod and filter out any email addresses that are similar to our internal users.

### Solution or next step

This query filters out the email addresses we want to filter:
```
 select count(*) from data_refinery_common_dataset where email_address is not NULL and email_address is not NULL and email_address not like 'cansav09%' and email_address not like 'arielsvn%' and email_address not like 'jaclyn.n.taroni%' and email_address not like 'kurt.wheeler91%'  and email_address not like 'greenescientist%' and email_address not like '%@alexslemonade.org%' and email_address not like 'miserlou%' and email_address not like 'michael.zietz@gmail.com%' and email_address not like 'd.prasad%' and email_address not like 'daniel.himmelstein@gmail.com%' and email_address not like 'dv.prasad991@gmail.com%';
 count 
-------
   102
```



Discussion:

Resolving patch:
diff --git a/api/data_refinery_api/settings.py b/api/data_refinery_api/settings.py
--- a/api/data_refinery_api/settings.py
+++ b/api/data_refinery_api/settings.py
@@ -196,6 +196,8 @@
 
 RUNNING_IN_CLOUD = get_env_variable('RUNNING_IN_CLOUD') == "True"
 
+ENVIRONMENT = get_env_variable_gracefully('ENVIRONMENT')
+
 # Elastic Search
 ELASTICSEARCH_DSL = {
     'default': {
diff --git a/api/data_refinery_api/views.py b/api/data_refinery_api/views.py
--- a/api/data_refinery_api/views.py
+++ b/api/data_refinery_api/views.py
@@ -584,6 +584,21 @@ class DatasetView(generics.RetrieveUpdateAPIView):
     serializer_class = DatasetSerializer
     lookup_field = 'id'
 
+    @staticmethod
+    def _should_display_on_engagement_bot(email: str) -> bool:
+        return email is not None \
+            and email.find("cansav09") != 0 \
+            and email.find("arielsvn") != 0 \
+            and email.find("jaclyn.n.taroni") != 0 \
+            and email.find("kurt.wheeler") != 0 \
+            and email.find("greenescientist") != 0 \
+            and email.find("@alexslemonade.org") == -1 \
+            and email.find("miserlou") != 0 \
+            and email.find("michael.zietz@gmail.com") != 0 \
+            and email.find("d.prasad") != 0 \
+            and email.find("daniel.himmelstein@gmail.com") != 0 \
+            and email.find("dv.prasad991@gmail.com") != 0
+
     def get_serializer_context(self):
         """
         Extra context provided to the serializer class.
@@ -696,7 +711,8 @@ def perform_update(self, serializer):
                 serializer.validated_data['is_processing'] = True
                 obj = serializer.save()
 
-                if settings.RUNNING_IN_CLOUD:
+                if settings.RUNNING_IN_CLOUD and settings.ENVIRONMENT == "prod" \
+                   and DatasetView._should_display_on_engagement_bot(supplied_email_address):
                     try:
                         try:
                             remote_ip = get_client_ip(self.request)


Test patch:
diff --git a/api/data_refinery_api/tests.py b/api/data_refinery_api/tests.py
--- a/api/data_refinery_api/tests.py
+++ b/api/data_refinery_api/tests.py
@@ -25,7 +25,7 @@
     ProcessorSerializer,
     SurveyJobSerializer,
 )
-from data_refinery_api.views import ExperimentList, Stats
+from data_refinery_api.views import ExperimentList, Stats, DatasetView
 from data_refinery_common.utils import get_env_variable
 from data_refinery_common.models import (
     ComputationalResult,
@@ -932,6 +932,22 @@ def test_dataset_stats(self):
         response = self.client.get(reverse('samples'), {'dataset_id': good_id})
         self.assertEqual(response.json()['count'], 2)
 
+    def test_engagement_bot_email_filtering(self):
+        self.assertTrue(DatasetView._should_display_on_engagement_bot("test@gmail.com"))
+
+        self.assertFalse(DatasetView._should_display_on_engagement_bot(None))
+        self.assertFalse(DatasetView._should_display_on_engagement_bot("cansav09@gmail.com"))
+        self.assertFalse(DatasetView._should_display_on_engagement_bot("arielsvn@gmail.com"))
+        self.assertFalse(DatasetView._should_display_on_engagement_bot("jaclyn.n.taroni@gmail.com"))
+        self.assertFalse(DatasetView._should_display_on_engagement_bot("kurt.wheeler@gmail.com"))
+        self.assertFalse(DatasetView._should_display_on_engagement_bot("greenescientist@gmail.com"))
+        self.assertFalse(DatasetView._should_display_on_engagement_bot("test@alexslemonade.org"))
+        self.assertFalse(DatasetView._should_display_on_engagement_bot("miserlou@gmail.com"))
+        self.assertFalse(DatasetView._should_display_on_engagement_bot("michael.zietz@gmail.com"))
+        self.assertFalse(DatasetView._should_display_on_engagement_bot("d.prasad@gmail.com"))
+        self.assertFalse(DatasetView._should_display_on_engagement_bot("daniel.himmelstein@gmail.com"))
+        self.assertFalse(DatasetView._should_display_on_engagement_bot("dv.prasad991@gmail.com"))
+
     @patch('raven.contrib.django.models.client')
     def test_sentry_middleware_ok(self, mock_client):
         # We don't even import raven if it's a good response.
@@ -1360,4 +1376,4 @@ def test_processor_and_organism_in_sample(self):
 
         organism_index = response.json()['results'][0]['organism_index']
         self.assertEqual(organism_index["result"], transcriptome_result.id)
-        self.assertEqual(organism_index["index_type"], "TRANSCRIPTOME_LONG")
\ No newline at end of file
+        self.assertEqual(organism_index["index_type"], "TRANSCRIPTOME_LONG")