annotate gpp/weblinks/models.py @ 204:b4305e18d3af

Resolve ticket #74. Add user badges. Some extra credit was done here: also refactored how pending news, links, and downloads are handled.
author Brian Neal <bgneal@gmail.com>
date Sat, 01 May 2010 21:53:59 +0000
parents fa7d82bfb100
children 71fd8454688b
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
gremmie@1 7 from django.contrib import auth
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@193 13 description = models.TextField(blank=True)
bgneal@193 14 count = models.IntegerField(default=0)
gremmie@1 15
gremmie@1 16 def __unicode__(self):
gremmie@1 17 return self.title
gremmie@1 18
gremmie@1 19 class Meta:
gremmie@1 20 verbose_name_plural = 'Categories'
gremmie@1 21 ordering = ('title', )
gremmie@1 22
gremmie@1 23
gremmie@1 24 class PublicLinkManager(models.Manager):
gremmie@1 25 """The manager for all public links."""
gremmie@1 26 def get_query_set(self):
bgneal@191 27 return super(PublicLinkManager, self).get_query_set().filter(
bgneal@191 28 is_public=True).select_related()
gremmie@1 29
gremmie@1 30
bgneal@204 31 class LinkBase(models.Model):
bgneal@204 32 """Abstract model to aggregate common fields of 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)
bgneal@204 38 date_added = models.DateField()
bgneal@204 39
bgneal@204 40 class Meta:
bgneal@204 41 abstract = True
bgneal@204 42
bgneal@204 43
bgneal@204 44 class Link(LinkBase):
bgneal@204 45 """Model to represent a web link"""
gremmie@1 46 hits = models.IntegerField(default=0)
gremmie@1 47 is_public = models.BooleanField(default=False, db_index=True)
gremmie@1 48
gremmie@1 49 # Managers:
gremmie@1 50 objects = models.Manager()
gremmie@1 51 public_objects = PublicLinkManager()
gremmie@1 52
bgneal@20 53 class Meta:
bgneal@20 54 ordering = ('title', )
bgneal@20 55
gremmie@1 56 def __unicode__(self):
gremmie@1 57 return self.title
gremmie@1 58
bgneal@20 59 @models.permalink
bgneal@20 60 def get_absolute_url(self):
bgneal@20 61 return ('weblinks-link_detail', [str(self.id)])
gremmie@1 62
gremmie@1 63
bgneal@204 64 class PendingLink(LinkBase):
bgneal@204 65 """This model represents links that users submit. They must be approved by
bgneal@204 66 an admin before they become visible on the site.
bgneal@204 67 """
bgneal@204 68 class Meta:
bgneal@204 69 ordering = ('date_added', )
bgneal@204 70
bgneal@204 71 def __unicode__(self):
bgneal@204 72 return self.title
bgneal@204 73
bgneal@204 74 def save(self, *args, **kwargs):
bgneal@204 75 if not self.pk:
bgneal@204 76 self.date_added = datetime.datetime.now()
bgneal@204 77 super(PendingLink, self).save(*args, **kwargs)
bgneal@204 78
bgneal@204 79
gremmie@1 80 class FlaggedLinkManager(models.Manager):
gremmie@1 81
gremmie@1 82 def create(self, link, user):
gremmie@1 83 flagged_link = FlaggedLink(link = link, user = user, approved = False)
gremmie@1 84 flagged_link.save()
gremmie@1 85
gremmie@1 86
gremmie@1 87 class FlaggedLink(models.Model):
bgneal@193 88 """Model to represent links that have been flagged as broken by users"""
gremmie@1 89 link = models.ForeignKey(Link)
gremmie@1 90 user = models.ForeignKey(auth.models.User)
gremmie@1 91 date_flagged = models.DateField(auto_now_add = True)
gremmie@1 92 approved = models.BooleanField(default = False,
gremmie@1 93 help_text = 'Check this and save to remove the referenced link from the database')
gremmie@1 94
gremmie@1 95 objects = FlaggedLinkManager()
gremmie@1 96
bgneal@182 97 def save(self, *args, **kwargs):
gremmie@1 98 if self.approved:
gremmie@1 99 self.link.delete()
gremmie@1 100 self.delete()
gremmie@1 101 else:
bgneal@182 102 super(FlaggedLink, self).save(*args, **kwargs)
gremmie@1 103
gremmie@1 104 def url(self):
gremmie@1 105 return self.link.url
gremmie@1 106
bgneal@165 107 def get_link_url(self):
bgneal@165 108 return '<a href="%s">Link #%d</a>' % (self.link.get_absolute_url(),
bgneal@165 109 self.link.id)
bgneal@165 110 get_link_url.allow_tags = True
bgneal@165 111 get_link_url.short_description = "View Link on Site"
bgneal@165 112
gremmie@1 113 def __unicode__(self):
gremmie@1 114 return self.link.title
gremmie@1 115
gremmie@1 116 class Meta:
gremmie@1 117 ordering = ('-date_flagged', )