annotate shoutbox/models.py @ 791:0ca691cccf8d

Utilize select_related() for user & user profiles. This commit also removes the caching of the avatar URL in the avatar template tag. This is because we are now using select_related, so we already have the profile & avatar when we get to the tag. Thus we don't need to waste time querying the cache. Removed an apparently unused member map template as well.
author Brian Neal <bgneal@gmail.com>
date Fri, 23 May 2014 21:52:41 -0500
parents ee87ea74d46b
children 98d2388b6bb2
rev   line source
gremmie@1 1 """
gremmie@1 2 Models for the shoutbox application.
gremmie@1 3 """
bgneal@151 4 import datetime
bgneal@151 5
gremmie@1 6 from django.db import models
gremmie@1 7 from django.contrib.auth.models import User
bgneal@162 8 from django.utils.html import escape, urlize
bgneal@151 9
bgneal@151 10 from smiley import smilify_html
gremmie@1 11
bgneal@13 12
gremmie@1 13 class Shout(models.Model):
bgneal@13 14 user = models.ForeignKey(User)
bgneal@151 15 shout_date = models.DateTimeField(blank=True)
bgneal@13 16 shout = models.TextField()
bgneal@151 17 html = models.TextField()
bgneal@151 18
bgneal@151 19 class Meta:
bgneal@151 20 ordering = ('-shout_date', )
bgneal@151 21
bgneal@151 22 def __unicode__(self):
bgneal@151 23 if len(self.shout) > 60:
bgneal@151 24 return self.shout[:60] + "..."
bgneal@151 25 return self.shout
gremmie@1 26
bgneal@13 27 @models.permalink
bgneal@13 28 def get_absolute_url(self):
bgneal@13 29 return ('shoutbox-view', [str(self.id)])
gremmie@1 30
bgneal@151 31 def save(self, *args, **kwargs):
bgneal@151 32 if not self.id:
bgneal@151 33 self.shout_date = datetime.datetime.now()
bgneal@791 34 self.html = urlize(smilify_html(escape(self.shout)), trim_url_limit=15,
bgneal@151 35 nofollow=True)
bgneal@151 36 super(Shout, self).save(*args, **kwargs)
bgneal@13 37
bgneal@13 38
bgneal@13 39 class ShoutFlag(models.Model):
bgneal@13 40 """This model represents a user flagging a shout as inappropriate."""
bgneal@13 41 user = models.ForeignKey(User)
bgneal@13 42 shout = models.ForeignKey(Shout)
bgneal@13 43 flag_date = models.DateTimeField(auto_now_add=True)
bgneal@13 44
bgneal@13 45 def __unicode__(self):
bgneal@13 46 return u'Shout ID %s flagged by %s' % (self.shout_id, self.user.username)
bgneal@13 47
bgneal@13 48 class Meta:
bgneal@13 49 ordering = ('flag_date', )
bgneal@13 50
bgneal@13 51 def get_shout_url(self):
bgneal@151 52 return '<a href="/admin/shoutbox/shout/%(id)d">Shout #%(id)d</a>' % (
bgneal@151 53 {'id': self.shout.id})
bgneal@13 54 get_shout_url.allow_tags = True
bgneal@151 55 get_shout_url.short_description = 'Link to Shout'
bgneal@13 56