annotate gpp/weblinks/models.py @ 160:2eb3984ccb15

Implement #45, add a who's online feature for the forums. Created middleware that caches usernames and guest session ids in the cache. Added a tag that displays this info.
author Brian Neal <bgneal@gmail.com>
date Tue, 22 Dec 2009 02:08:05 +0000
parents c0d0779b266f
children 952e05cb3d80
rev   line source
gremmie@1 1 '''
gremmie@1 2 This module contains the models for the weblinks application.
gremmie@1 3 '''
gremmie@1 4
gremmie@1 5 from django.db import models
gremmie@1 6 from django.contrib import auth
gremmie@1 7
gremmie@1 8
gremmie@1 9 class Category(models.Model):
gremmie@1 10 '''Links belong to categories'''
gremmie@1 11 title = models.CharField(max_length = 64)
gremmie@1 12 description = models.TextField(blank = True)
gremmie@1 13
gremmie@1 14 def __unicode__(self):
gremmie@1 15 return self.title
gremmie@1 16
gremmie@1 17 def num_links(self):
gremmie@1 18 return Link.public_objects.filter(category = self.pk).count()
gremmie@1 19
gremmie@1 20 class Meta:
gremmie@1 21 verbose_name_plural = 'Categories'
gremmie@1 22 ordering = ('title', )
gremmie@1 23
gremmie@1 24
gremmie@1 25 class PublicLinkManager(models.Manager):
gremmie@1 26 """The manager for all public links."""
gremmie@1 27 def get_query_set(self):
gremmie@1 28 return super(PublicLinkManager, self).get_query_set().filter(is_public=True)
gremmie@1 29
gremmie@1 30
gremmie@1 31 class Link(models.Model):
gremmie@1 32 '''Model to represent a web link'''
gremmie@1 33 category = models.ForeignKey(Category)
gremmie@1 34 title = models.CharField(max_length=128)
gremmie@1 35 url = models.URLField(verify_exists=False, db_index=True)
gremmie@1 36 description = models.TextField(blank=True)
gremmie@1 37 user = models.ForeignKey(auth.models.User)
gremmie@1 38 date_added = models.DateField(auto_now_add=True)
gremmie@1 39 hits = models.IntegerField(default=0)
gremmie@1 40 is_public = models.BooleanField(default=False, db_index=True)
gremmie@1 41
gremmie@1 42 # Managers:
gremmie@1 43 objects = models.Manager()
gremmie@1 44 public_objects = PublicLinkManager()
gremmie@1 45
bgneal@20 46 class Meta:
bgneal@20 47 ordering = ('title', )
bgneal@20 48
gremmie@1 49 def __unicode__(self):
gremmie@1 50 return self.title
gremmie@1 51
bgneal@20 52 @models.permalink
bgneal@20 53 def get_absolute_url(self):
bgneal@20 54 return ('weblinks-link_detail', [str(self.id)])
gremmie@1 55
gremmie@1 56
gremmie@1 57 class FlaggedLinkManager(models.Manager):
gremmie@1 58
gremmie@1 59 def create(self, link, user):
gremmie@1 60 flagged_link = FlaggedLink(link = link, user = user, approved = False)
gremmie@1 61 flagged_link.save()
gremmie@1 62
gremmie@1 63
gremmie@1 64 class FlaggedLink(models.Model):
gremmie@1 65 '''Model to represent links that have been flagged as broken by users'''
gremmie@1 66 link = models.ForeignKey(Link)
gremmie@1 67 user = models.ForeignKey(auth.models.User)
gremmie@1 68 date_flagged = models.DateField(auto_now_add = True)
gremmie@1 69 approved = models.BooleanField(default = False,
gremmie@1 70 help_text = 'Check this and save to remove the referenced link from the database')
gremmie@1 71
gremmie@1 72 objects = FlaggedLinkManager()
gremmie@1 73
gremmie@1 74 def save(self, force_insert = False, force_update = False):
gremmie@1 75 if self.approved:
gremmie@1 76 self.link.delete()
gremmie@1 77 self.delete()
gremmie@1 78 else:
gremmie@1 79 super(FlaggedLink, self).save(force_insert, force_update)
gremmie@1 80
gremmie@1 81 def url(self):
gremmie@1 82 return self.link.url
gremmie@1 83
gremmie@1 84 def __unicode__(self):
gremmie@1 85 return self.link.title
gremmie@1 86
gremmie@1 87 class Meta:
gremmie@1 88 ordering = ('-date_flagged', )