comparison shoutbox/models.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/shoutbox/models.py@6a5bdcf93ad3
children 0ca691cccf8d
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 Models for the shoutbox application.
3 """
4 import datetime
5
6 from django.db import models
7 from django.contrib.auth.models import User
8 from django.utils.html import escape, urlize
9
10 from smiley import smilify_html
11
12
13 class Shout(models.Model):
14 user = models.ForeignKey(User)
15 shout_date = models.DateTimeField(blank=True)
16 shout = models.TextField()
17 html = models.TextField()
18
19 class Meta:
20 ordering = ('-shout_date', )
21
22 def __unicode__(self):
23 if len(self.shout) > 60:
24 return self.shout[:60] + "..."
25 return self.shout
26
27 @models.permalink
28 def get_absolute_url(self):
29 return ('shoutbox-view', [str(self.id)])
30
31 def save(self, *args, **kwargs):
32 if not self.id:
33 self.shout_date = datetime.datetime.now()
34 self.html = urlize(smilify_html(escape(self.shout)), trim_url_limit=15,
35 nofollow=True)
36 super(Shout, self).save(*args, **kwargs)
37
38
39 class ShoutFlag(models.Model):
40 """This model represents a user flagging a shout as inappropriate."""
41 user = models.ForeignKey(User)
42 shout = models.ForeignKey(Shout)
43 flag_date = models.DateTimeField(auto_now_add=True)
44
45 def __unicode__(self):
46 return u'Shout ID %s flagged by %s' % (self.shout_id, self.user.username)
47
48 class Meta:
49 ordering = ('flag_date', )
50
51 def get_shout_url(self):
52 return '<a href="/admin/shoutbox/shout/%(id)d">Shout #%(id)d</a>' % (
53 {'id': self.shout.id})
54 get_shout_url.allow_tags = True
55 get_shout_url.short_description = 'Link to Shout'
56