From 0515de484ba64efcf5c686de4aa6d15fd628728d Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Thu, 1 Mar 2018 16:19:48 +0000
Subject: [PATCH] Add the concept of type to activity

---
 .../activity/migrations/0002_activity_type.py | 21 +++++++++++++
 opentech/apply/activity/models.py             | 31 +++++++++++++++++++
 opentech/apply/activity/views.py              |  5 +--
 opentech/apply/funds/views.py                 |  2 +-
 4 files changed, 56 insertions(+), 3 deletions(-)
 create mode 100644 opentech/apply/activity/migrations/0002_activity_type.py

diff --git a/opentech/apply/activity/migrations/0002_activity_type.py b/opentech/apply/activity/migrations/0002_activity_type.py
new file mode 100644
index 000000000..d0688ecb0
--- /dev/null
+++ b/opentech/apply/activity/migrations/0002_activity_type.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.8 on 2018-03-01 15:49
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('activity', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='activity',
+            name='type',
+            field=models.CharField(choices=[('comment', 'Comment'), ('activity', 'Activity')], default='comment', max_length=30),
+            preserve_default=False,
+        ),
+    ]
diff --git a/opentech/apply/activity/models.py b/opentech/apply/activity/models.py
index c01132e72..05152b9d6 100644
--- a/opentech/apply/activity/models.py
+++ b/opentech/apply/activity/models.py
@@ -1,12 +1,43 @@
 from django.conf import settings
 from django.db import models
 
+COMMENT = 'comment'
+ACTIVITY = 'activity'
+
+ACTIVITY_TYPES = {
+    COMMENT: 'Comment',
+    ACTIVITY: 'Activity',
+}
+
+
+class CommentManger(models.Manager):
+    def create(self, **kwargs):
+        kwargs.update(type=COMMENT)
+        return super().create(**kwargs)
+
+    def get_queryset(self):
+        return super().get_queryset().filter(type=COMMENT)
+
+
+class ActivityManager(models.Manager):
+    def create(self, **kwargs):
+        kwargs.update(type=ACTIVITY)
+        return super().create(**kwargs)
+
+    def get_queryset(self):
+        return super().get_queryset().filter(type=ACTIVITY)
+
 
 class Activity(models.Model):
     timestamp = models.DateTimeField(auto_now_add=True)
+    type = models.CharField(choices=ACTIVITY_TYPES.items(), max_length=30)
     user = models.ForeignKey(settings.AUTH_USER_MODEL)
     submission = models.ForeignKey('funds.ApplicationSubmission', related_name='activities')
     message = models.TextField()
 
+    objects = models.Manager()
+    comments = CommentManger()
+    activities = ActivityManager()
+
     class Meta:
         ordering = ['-timestamp']
diff --git a/opentech/apply/activity/views.py b/opentech/apply/activity/views.py
index 59bf95616..f7a15a82e 100644
--- a/opentech/apply/activity/views.py
+++ b/opentech/apply/activity/views.py
@@ -1,13 +1,13 @@
 from django.views.generic import CreateView, View
 
 from .forms import CommentForm
-from .models import Activity
+from .models import Activity, COMMENT
 
 
 class CommentContextMixin:
     def get_context_data(self, **kwargs):
         extra = {
-            'comments': Activity.objects.filter(submission=self.object),
+            'comments': Activity.comments.filter(submission=self.object),
             CommentFormView.context_name: CommentFormView.form_class(),
         }
 
@@ -34,6 +34,7 @@ class CommentFormView(DelegatedViewMixin, CreateView):
     def form_valid(self, form):
         form.instance.user = self.request.user
         form.instance.submission = self.kwargs['submission']
+        form.instance.type = COMMENT
         return super().form_valid(form)
 
     def get_success_url(self):
diff --git a/opentech/apply/funds/views.py b/opentech/apply/funds/views.py
index aa7754b7d..1158689b5 100644
--- a/opentech/apply/funds/views.py
+++ b/opentech/apply/funds/views.py
@@ -62,7 +62,7 @@ class ProgressSubmissionView(DelegatedViewMixin, UpdateView):
         old_phase = form.instance.phase.name
         response = super().form_valid(form)
         new_phase = form.instance.phase.name
-        Activity.objects.create(
+        Activity.activities.create(
             user=self.request.user,
             submission=self.kwargs['submission'],
             message=f'Progressed from {old_phase} to {new_phase}'
-- 
GitLab