comparison apps/snippet/models.py @ 139:b8e0bdc37e32

Hide snippets created by anonymous users Created a custom manager for Snippet, with a public() method for easily retrieving all the snippets that were created by registered users. This change makes the code a bit neater, as it's no longer to necessary to have to do the {% if snippet.author %} check every time.
author dellsystem <ilostwaldo@gmail.com>
date Sat, 29 Sep 2012 21:31:17 -0400
parents 180404efc8cf
children c7be7def8b57
comparison
equal deleted inserted replaced
138:4d358e1e3014 139:b8e0bdc37e32
12 12
13 t = 'abcdefghijkmnopqrstuvwwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ1234567890' 13 t = 'abcdefghijkmnopqrstuvwwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ1234567890'
14 def generate_secret_id(length=4): 14 def generate_secret_id(length=4):
15 return ''.join([random.choice(t) for i in range(length)]) 15 return ''.join([random.choice(t) for i in range(length)])
16 16
17 class SnippetManager(models.Manager):
18 def public(self):
19 """
20 Returns all the snippets that were created by registered users
21 and thus can be publicly listed.
22 """
23 return self.filter(author__isnull=False)
24
25
17 class Snippet(models.Model): 26 class Snippet(models.Model):
27 objects = SnippetManager()
18 secret_id = models.CharField(_(u'Secret ID'), max_length=4, blank=True) 28 secret_id = models.CharField(_(u'Secret ID'), max_length=4, blank=True)
19 title = models.CharField(_(u'Title'), max_length=120, blank=True) 29 title = models.CharField(_(u'Title'), max_length=120, blank=True)
20 author = models.ForeignKey(User, max_length=30, blank=True, null=True) 30 author = models.ForeignKey(User, max_length=30, blank=True, null=True)
21 content = models.TextField(_(u'Content'), ) 31 content = models.TextField(_(u'Content'), )
22 content_highlighted = models.TextField(_(u'Highlighted Content'), 32 content_highlighted = models.TextField(_(u'Highlighted Content'),