comparison 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
comparison
equal deleted inserted replaced
203:40e5903903e1 204:b4305e18d3af
1 """ 1 """
2 This module contains the models for the weblinks application. 2 This module contains the models for the weblinks application.
3 """ 3 """
4 import datetime
5
4 from django.db import models 6 from django.db import models
5 from django.contrib import auth 7 from django.contrib import auth
6 8
7 9
8 class Category(models.Model): 10 class Category(models.Model):
24 def get_query_set(self): 26 def get_query_set(self):
25 return super(PublicLinkManager, self).get_query_set().filter( 27 return super(PublicLinkManager, self).get_query_set().filter(
26 is_public=True).select_related() 28 is_public=True).select_related()
27 29
28 30
29 class Link(models.Model): 31 class LinkBase(models.Model):
30 """Model to represent a web link""" 32 """Abstract model to aggregate common fields of a web link."""
31 category = models.ForeignKey(Category) 33 category = models.ForeignKey(Category)
32 title = models.CharField(max_length=128) 34 title = models.CharField(max_length=128)
33 url = models.URLField(verify_exists=False, db_index=True) 35 url = models.URLField(verify_exists=False, db_index=True)
34 description = models.TextField(blank=True) 36 description = models.TextField(blank=True)
35 user = models.ForeignKey(auth.models.User) 37 user = models.ForeignKey(auth.models.User)
36 date_added = models.DateField(auto_now_add=True) 38 date_added = models.DateField()
39
40 class Meta:
41 abstract = True
42
43
44 class Link(LinkBase):
45 """Model to represent a web link"""
37 hits = models.IntegerField(default=0) 46 hits = models.IntegerField(default=0)
38 is_public = models.BooleanField(default=False, db_index=True) 47 is_public = models.BooleanField(default=False, db_index=True)
39 48
40 # Managers: 49 # Managers:
41 objects = models.Manager() 50 objects = models.Manager()
48 return self.title 57 return self.title
49 58
50 @models.permalink 59 @models.permalink
51 def get_absolute_url(self): 60 def get_absolute_url(self):
52 return ('weblinks-link_detail', [str(self.id)]) 61 return ('weblinks-link_detail', [str(self.id)])
62
63
64 class PendingLink(LinkBase):
65 """This model represents links that users submit. They must be approved by
66 an admin before they become visible on the site.
67 """
68 class Meta:
69 ordering = ('date_added', )
70
71 def __unicode__(self):
72 return self.title
73
74 def save(self, *args, **kwargs):
75 if not self.pk:
76 self.date_added = datetime.datetime.now()
77 super(PendingLink, self).save(*args, **kwargs)
53 78
54 79
55 class FlaggedLinkManager(models.Manager): 80 class FlaggedLinkManager(models.Manager):
56 81
57 def create(self, link, user): 82 def create(self, link, user):