annotate shoutbox/models.py @ 1150:0e0cd152b86d

WIP convert downloads to V3 design.
author Brian Neal <bgneal@gmail.com>
date Tue, 13 Dec 2016 20:20:24 -0600
parents eeaf387803c6
children
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@1035 8 from django.core.urlresolvers import reverse
bgneal@162 9 from django.utils.html import escape, urlize
bgneal@151 10
bgneal@925 11 from smiley.utils import smilify_html
gremmie@1 12
bgneal@13 13
gremmie@1 14 class Shout(models.Model):
bgneal@13 15 user = models.ForeignKey(User)
bgneal@151 16 shout_date = models.DateTimeField(blank=True)
bgneal@13 17 shout = models.TextField()
bgneal@151 18 html = models.TextField()
bgneal@151 19
bgneal@151 20 class Meta:
bgneal@151 21 ordering = ('-shout_date', )
bgneal@151 22
bgneal@151 23 def __unicode__(self):
bgneal@151 24 if len(self.shout) > 60:
bgneal@151 25 return self.shout[:60] + "..."
bgneal@151 26 return self.shout
gremmie@1 27
bgneal@13 28 def get_absolute_url(self):
bgneal@1035 29 return reverse('shoutbox-view', args=[str(self.pk)])
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