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 0000000000000000000000000000000000000000..d0688ecb048114d572c479d534d4632264d7f36d --- /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 c01132e720799eeb537f136b087ec7ed7ed764bb..05152b9d63150c0edabd510cc98cd5dcec3fd5e3 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 59bf9561644bbf633f6bdd5f2857625678642872..f7a15a82e759b1202d1a4fad9d862a235e02b350 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 aa7754b7d652ebd8459ae833f89b69f2f825bb98..1158689b50a0411ddb40fdfdf5eb58557b5fc024 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}'