annotate gpp/weblinks/models.py @ 263:307a74e28112

Fixing #113; re-arrange the RSS forum feed icons around a bit.
author Brian Neal <bgneal@gmail.com>
date Thu, 23 Sep 2010 00:26:07 +0000
parents 7e8d2dda99e3
children d424b8bae71d
rev   line source
bgneal@193 1 """
gremmie@1 2 This module contains the models for the weblinks application.
bgneal@193 3 """
bgneal@204 4 import datetime
bgneal@204 5
gremmie@1 6 from django.db import models
bgneal@237 7 from django.contrib.auth.models import User
gremmie@1 8
gremmie@1 9
gremmie@1 10 class Category(models.Model):
bgneal@193 11 """Links belong to categories"""
bgneal@193 12 title = models.CharField(max_length=64)
bgneal@242 13 slug = models.SlugField(max_length=64)
bgneal@193 14 description = models.TextField(blank=True)
bgneal@193 15 count = models.IntegerField(default=0)
gremmie@1 16
gremmie@1 17 def __unicode__(self):
gremmie@1 18 return self.title
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):
bgneal@191 28 return super(PublicLinkManager, self).get_query_set().filter(
bgneal@191 29 is_public=True).select_related()
gremmie@1 30
gremmie@1 31
bgneal@204 32 class LinkBase(models.Model):
bgneal@204 33 """Abstract model to aggregate common fields of a web link."""
gremmie@1 34 category = models.ForeignKey(Category)
gremmie@1 35 title = models.CharField(max_length=128)
gremmie@1 36 url = models.URLField(verify_exists=False, db_index=True)
gremmie@1 37 description = models.TextField(blank=True)
bgneal@237 38 user = models.ForeignKey(User)
bgneal@204 39 date_added = models.DateField()
bgneal@204 40
bgneal@204 41 class Meta:
bgneal@204 42 abstract = True
bgneal@204 43
bgneal@204 44
bgneal@204 45 class Link(LinkBase):
bgneal@204 46 """Model to represent a web link"""
gremmie@1 47 hits = models.IntegerField(default=0)
gremmie@1 48 is_public = models.BooleanField(default=False, db_index=True)
gremmie@1 49
gremmie@1 50 # Managers:
gremmie@1 51 objects = models.Manager()
gremmie@1 52 public_objects = PublicLinkManager()
gremmie@1 53
bgneal@20 54 class Meta:
bgneal@20 55 ordering = ('title', )
bgneal@20 56
gremmie@1 57 def __unicode__(self):
gremmie@1 58 return self.title
gremmie@1 59
bgneal@20 60 @models.permalink
bgneal@20 61 def get_absolute_url(self):
bgneal@20 62 return ('weblinks-link_detail', [str(self.id)])
gremmie@1 63
bgneal@220 64 def search_title(self):
bgneal@220 65 return self.title
bgneal@220 66
bgneal@220 67 def search_summary(self):
bgneal@220 68 return self.description
bgneal@220 69
gremmie@1 70
bgneal@204 71 class PendingLink(LinkBase):
bgneal@204 72 """This model represents links that users submit. They must be approved by
bgneal@204 73 an admin before they become visible on the site.
bgneal@204 74 """
bgneal@204 75 class Meta:
bgneal@204 76 ordering = ('date_added', )
bgneal@204 77
bgneal@204 78 def __unicode__(self):
bgneal@204 79 return self.title
bgneal@204 80
bgneal@204 81 def save(self, *args, **kwargs):
bgneal@204 82 if not self.pk:
bgneal@204 83 self.date_added = datetime.datetime.now()
bgneal@204 84 super(PendingLink, self).save(*args, **kwargs)
bgneal@204 85
bgneal@204 86
gremmie@1 87 class FlaggedLinkManager(models.Manager):
gremmie@1 88
gremmie@1 89 def create(self, link, user):
gremmie@1 90 flagged_link = FlaggedLink(link = link, user = user, approved = False)
gremmie@1 91 flagged_link.save()
gremmie@1 92
gremmie@1 93
gremmie@1 94 class FlaggedLink(models.Model):
bgneal@193 95 """Model to represent links that have been flagged as broken by users"""
gremmie@1 96 link = models.ForeignKey(Link)
bgneal@237 97 user = models.ForeignKey(User)
gremmie@1 98 date_flagged = models.DateField(auto_now_add = True)
gremmie@1 99 approved = models.BooleanField(default = False,
gremmie@1 100 help_text = 'Check this and save to remove the referenced link from the database')
gremmie@1 101
gremmie@1 102 objects = FlaggedLinkManager()
gremmie@1 103
bgneal@182 104 def save(self, *args, **kwargs):
gremmie@1 105 if self.approved:
gremmie@1 106 self.link.delete()
gremmie@1 107 self.delete()
gremmie@1 108 else:
bgneal@182 109 super(FlaggedLink, self).save(*args, **kwargs)
gremmie@1 110
gremmie@1 111 def url(self):
gremmie@1 112 return self.link.url
gremmie@1 113
bgneal@165 114 def get_link_url(self):
bgneal@165 115 return '<a href="%s">Link #%d</a>' % (self.link.get_absolute_url(),
bgneal@165 116 self.link.id)
bgneal@165 117 get_link_url.allow_tags = True
bgneal@165 118 get_link_url.short_description = "View Link on Site"
bgneal@165 119
gremmie@1 120 def __unicode__(self):
gremmie@1 121 return self.link.title
gremmie@1 122
gremmie@1 123 class Meta:
gremmie@1 124 ordering = ('-date_flagged', )