comparison weblinks/models.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/weblinks/models.py@368d731af479
children 6e6492468bb8
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 This module contains the models for the weblinks application.
3 """
4 import datetime
5
6 from django.db import models
7 from django.contrib.auth.models import User
8
9
10 class Category(models.Model):
11 """Links belong to categories"""
12 title = models.CharField(max_length=64)
13 slug = models.SlugField(max_length=64)
14 description = models.TextField(blank=True)
15 count = models.IntegerField(default=0)
16
17 def __unicode__(self):
18 return self.title
19
20 class Meta:
21 verbose_name_plural = 'Categories'
22 ordering = ('title', )
23
24
25 class PublicLinkManager(models.Manager):
26 """The manager for all public links."""
27 def get_query_set(self):
28 return super(PublicLinkManager, self).get_query_set().filter(
29 is_public=True).select_related()
30
31
32 class LinkBase(models.Model):
33 """Abstract model to aggregate common fields of a web link."""
34 category = models.ForeignKey(Category)
35 title = models.CharField(max_length=128)
36 url = models.URLField(db_index=True)
37 description = models.TextField(blank=True)
38 user = models.ForeignKey(User)
39 date_added = models.DateTimeField(db_index=True)
40 update_date = models.DateTimeField(db_index=True, blank=True)
41
42 class Meta:
43 abstract = True
44
45
46 class Link(LinkBase):
47 """Model to represent a web link"""
48 hits = models.IntegerField(default=0)
49 is_public = models.BooleanField(default=False, db_index=True)
50
51 # Managers:
52 objects = models.Manager()
53 public_objects = PublicLinkManager()
54
55 class Meta:
56 ordering = ('title', )
57
58 def __unicode__(self):
59 return self.title
60
61 def save(self, *args, **kwargs):
62 if not self.pk:
63 if not self.date_added:
64 self.date_added = datetime.datetime.now()
65 self.update_date = self.date_added
66 else:
67 self.update_date = datetime.datetime.now()
68
69 super(Link, self).save(*args, **kwargs)
70
71 @models.permalink
72 def get_absolute_url(self):
73 return ('weblinks-link_detail', [str(self.id)])
74
75 def search_title(self):
76 return self.title
77
78 def search_summary(self):
79 return self.description
80
81
82 class PendingLink(LinkBase):
83 """This model represents links that users submit. They must be approved by
84 an admin before they become visible on the site.
85 """
86 class Meta:
87 ordering = ('date_added', )
88
89 def __unicode__(self):
90 return self.title
91
92 def save(self, *args, **kwargs):
93 if not self.pk:
94 self.date_added = datetime.datetime.now()
95 self.update_date = self.date_added
96 else:
97 self.update_date = datetime.datetime.now()
98
99 super(PendingLink, self).save(*args, **kwargs)
100
101
102 class FlaggedLinkManager(models.Manager):
103
104 def create(self, link, user):
105 flagged_link = FlaggedLink(link = link, user = user, approved = False)
106 flagged_link.save()
107
108
109 class FlaggedLink(models.Model):
110 """Model to represent links that have been flagged as broken by users"""
111 link = models.ForeignKey(Link)
112 user = models.ForeignKey(User)
113 date_flagged = models.DateField(auto_now_add = True)
114 approved = models.BooleanField(default = False,
115 help_text = 'Check this and save to remove the referenced link from the database')
116
117 objects = FlaggedLinkManager()
118
119 def save(self, *args, **kwargs):
120 if self.approved:
121 self.link.delete()
122 self.delete()
123 else:
124 super(FlaggedLink, self).save(*args, **kwargs)
125
126 def url(self):
127 return self.link.url
128
129 def get_link_url(self):
130 return '<a href="%s">Link #%d</a>' % (self.link.get_absolute_url(),
131 self.link.id)
132 get_link_url.allow_tags = True
133 get_link_url.short_description = "View Link on Site"
134
135 def __unicode__(self):
136 return self.link.title
137
138 class Meta:
139 ordering = ('-date_flagged', )