Skip to content

Commit

Permalink
Take a first stab at the Activity Feed
Browse files Browse the repository at this point in the history
  • Loading branch information
mdiep committed Oct 13, 2011
1 parent bad75fc commit 45febe2
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 4 deletions.
102 changes: 102 additions & 0 deletions poplar/migrations/0007_auto__add_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models

class Migration(SchemaMigration):

def forwards(self, orm):

# Adding model 'Action'
db.create_table('poplar_action', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('timestamp', self.gf('django.db.models.fields.DateTimeField')()),
('is_public', self.gf('django.db.models.fields.BooleanField')(default=True)),
('actor', self.gf('django.db.models.fields.related.ForeignKey')(related_name='actions', to=orm['auth.User'])),
('verb', self.gf('django.db.models.fields.CharField')(max_length=2)),
('note', self.gf('django.db.models.fields.related.ForeignKey')(related_name='+', null=True, to=orm['poplar.Note'])),
('group', self.gf('django.db.models.fields.related.ForeignKey')(related_name='+', null=True, to=orm['poplar.Group'])),
('person', self.gf('django.db.models.fields.related.ForeignKey')(related_name='+', null=True, to=orm['poplar.Person'])),
))
db.send_create_signal('poplar', ['Action'])


def backwards(self, orm):

# Deleting model 'Action'
db.delete_table('poplar_action')


models = {
'auth.group': {
'Meta': {'object_name': 'Group'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
'auth.permission': {
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
'auth.user': {
'Meta': {'object_name': 'User'},
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
},
'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
'poplar.action': {
'Meta': {'ordering': "['-timestamp']", 'object_name': 'Action'},
'actor': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'actions'", 'to': "orm['auth.User']"}),
'group': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'+'", 'null': 'True', 'to': "orm['poplar.Group']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_public': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'note': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'+'", 'null': 'True', 'to': "orm['poplar.Note']"}),
'person': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'+'", 'null': 'True', 'to': "orm['poplar.Person']"}),
'timestamp': ('django.db.models.fields.DateTimeField', [], {}),
'verb': ('django.db.models.fields.CharField', [], {'max_length': '2'})
},
'poplar.group': {
'Meta': {'object_name': 'Group'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}),
'people': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'groups'", 'blank': 'True', 'to': "orm['poplar.Person']"}),
'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '30', 'db_index': 'True'})
},
'poplar.note': {
'Meta': {'ordering': "['-timestamp']", 'object_name': 'Note'},
'author': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'notes'", 'to': "orm['auth.User']"}),
'body': ('django.db.models.fields.TextField', [], {}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_public': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'subject': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'notes'", 'to': "orm['poplar.Person']"}),
'timestamp': ('django.db.models.fields.DateTimeField', [], {})
},
'poplar.person': {
'Meta': {'object_name': 'Person'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
}
}

complete_apps = ['poplar']
37 changes: 37 additions & 0 deletions poplar/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,40 @@ def __unicode__(self):
class Meta:
ordering = ["-timestamp"]

# Sarah wrote a note about Matt: "He's a winner!"
# actor=Sarah, action='wrote', note="He's a winner!", person=Matt
# Sarah added 1 person: Matt
# actor=Sarah, action='added', person=Matt
# Sarah added 5 people to South: Matt, Kathy, Jeremy, Dave, Alyssa
# actor=Sarah, action='added', person=Matt, group=South
# etc.
# Sarah removed 1 person from South: Matt
# actor=Sarah, action='removed', person=Matt, group=South
# Karl created a new group: Slides
# actor=Karl, action='created', group=Slides
class Action(models.Model):
VERB_CHOICES = (
('wr', 'wrote'),
('ad', 'added'),
('rm', 'removed'),
('cr', 'created'),
)
timestamp = models.DateTimeField()
is_public = models.BooleanField(default=True)
actor = models.ForeignKey(auth.models.User, related_name='actions')
verb = models.CharField(max_length=2, choices=VERB_CHOICES)

# optional objects
note = models.ForeignKey(Note, related_name='+', null=True)
group = models.ForeignKey(Group, related_name='+', null=True)
person = models.ForeignKey(Person, related_name='+', null=True)

def __unicode__(self):
if self.note and self.verb == 'wr':
return '%s wrote a note about %s' % (self.actor, self.person)
return '%s action' % (self.get_verb_display())

class Meta:
ordering = ['-timestamp']


14 changes: 14 additions & 0 deletions poplar/templates/poplar/activity_feed.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,18 @@

{% block content %}
<h1>Activity Feed</h1>
{% for action in actions %}
<div class='action'>
{% if action.verb == 'ad' and action.person %}
{{ action.actor }} added <a href='{{ action.person.get_absolute_url }}'>{{ action.person }}</a>.
{% else %}{% if action.verb == 'wr' and action.note %}
{{ action.actor }} wrote a note about <a href='{{ action.person.get_absolute_url }}'>{{ action.person }}</a>:
<blockquote>{{ action.note.body }}</blockquote>
{% else %}
{{ action }}
{% endif %}{% endif %}
</div>
{% empty %}
No recent activity.
{% endfor %}
{% endblock %}
24 changes: 20 additions & 4 deletions poplar/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

from datetime import datetime

from django.core.urlresolvers import reverse
from django.db.models import Q
from django.http import HttpResponse, HttpResponseRedirect
Expand All @@ -9,13 +11,14 @@
from django.contrib.auth.decorators import login_required
from django.contrib.sites.models import get_current_site

from poplar.models import Person, Group
from poplar.models import Person, Group, Action
from poplar.forms import PersonForm, NoteForm

@login_required
def activity_feed(request):
groups = Group.objects.all()
site = get_current_site(request)
groups = Group.objects.all()
site = get_current_site(request)
actions = Action.objects.filter(is_public=True).exclude(actor=request.user)[:20]
return render_to_response('poplar/activity_feed.html', locals(),
context_instance=RequestContext(request))

Expand Down Expand Up @@ -43,6 +46,12 @@ def person_add(request):
form = PersonForm(request.POST)
if form.is_valid():
person = form.save()
action = Action(timestamp=datetime.now(),
is_public=True,
actor=request.user,
verb='ad',
person=person)
action.save()
return HttpResponseRedirect(reverse('person', args=[person.id]))
else:
form = PersonForm()
Expand Down Expand Up @@ -75,7 +84,14 @@ def note_add(request, person_id):
if request.method == 'POST':
form = NoteForm(request.POST, author=request.user, subject=subject)
if form.is_valid():
form.save()
note = form.save()
action = Action(timestamp=datetime.now(),
is_public=note.is_public,
actor=request.user,
verb='wr',
note=note,
person=subject)
action.save()
return HttpResponseRedirect(reverse('person', args=[subject.id]))
return person(request, person_id)

Expand Down

0 comments on commit 45febe2

Please sign in to comment.