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)
|
bgneal@572
|
36 url = models.URLField(db_index=True)
|
gremmie@1
|
37 description = models.TextField(blank=True)
|
bgneal@237
|
38 user = models.ForeignKey(User)
|
bgneal@277
|
39 date_added = models.DateTimeField(db_index=True)
|
bgneal@277
|
40 update_date = models.DateTimeField(db_index=True, blank=True)
|
bgneal@204
|
41
|
bgneal@204
|
42 class Meta:
|
bgneal@204
|
43 abstract = True
|
bgneal@204
|
44
|
bgneal@204
|
45
|
bgneal@204
|
46 class Link(LinkBase):
|
bgneal@204
|
47 """Model to represent a web link"""
|
gremmie@1
|
48 hits = models.IntegerField(default=0)
|
gremmie@1
|
49 is_public = models.BooleanField(default=False, db_index=True)
|
gremmie@1
|
50
|
gremmie@1
|
51 # Managers:
|
gremmie@1
|
52 objects = models.Manager()
|
gremmie@1
|
53 public_objects = PublicLinkManager()
|
gremmie@1
|
54
|
bgneal@20
|
55 class Meta:
|
bgneal@20
|
56 ordering = ('title', )
|
bgneal@20
|
57
|
gremmie@1
|
58 def __unicode__(self):
|
gremmie@1
|
59 return self.title
|
gremmie@1
|
60
|
bgneal@277
|
61 def save(self, *args, **kwargs):
|
bgneal@277
|
62 if not self.pk:
|
bgneal@277
|
63 if not self.date_added:
|
bgneal@277
|
64 self.date_added = datetime.datetime.now()
|
bgneal@277
|
65 self.update_date = self.date_added
|
bgneal@277
|
66 else:
|
bgneal@277
|
67 self.update_date = datetime.datetime.now()
|
bgneal@277
|
68
|
bgneal@277
|
69 super(Link, self).save(*args, **kwargs)
|
bgneal@277
|
70
|
bgneal@20
|
71 @models.permalink
|
bgneal@20
|
72 def get_absolute_url(self):
|
bgneal@20
|
73 return ('weblinks-link_detail', [str(self.id)])
|
gremmie@1
|
74
|
bgneal@220
|
75 def search_title(self):
|
bgneal@220
|
76 return self.title
|
bgneal@220
|
77
|
bgneal@220
|
78 def search_summary(self):
|
bgneal@220
|
79 return self.description
|
bgneal@220
|
80
|
gremmie@1
|
81
|
bgneal@204
|
82 class PendingLink(LinkBase):
|
bgneal@204
|
83 """This model represents links that users submit. They must be approved by
|
bgneal@204
|
84 an admin before they become visible on the site.
|
bgneal@204
|
85 """
|
bgneal@204
|
86 class Meta:
|
bgneal@204
|
87 ordering = ('date_added', )
|
bgneal@204
|
88
|
bgneal@204
|
89 def __unicode__(self):
|
bgneal@204
|
90 return self.title
|
bgneal@204
|
91
|
bgneal@204
|
92 def save(self, *args, **kwargs):
|
bgneal@204
|
93 if not self.pk:
|
bgneal@204
|
94 self.date_added = datetime.datetime.now()
|
bgneal@277
|
95 self.update_date = self.date_added
|
bgneal@277
|
96 else:
|
bgneal@277
|
97 self.update_date = datetime.datetime.now()
|
bgneal@277
|
98
|
bgneal@204
|
99 super(PendingLink, self).save(*args, **kwargs)
|
bgneal@204
|
100
|
bgneal@204
|
101
|
gremmie@1
|
102 class FlaggedLinkManager(models.Manager):
|
gremmie@1
|
103
|
gremmie@1
|
104 def create(self, link, user):
|
gremmie@1
|
105 flagged_link = FlaggedLink(link = link, user = user, approved = False)
|
gremmie@1
|
106 flagged_link.save()
|
gremmie@1
|
107
|
gremmie@1
|
108
|
gremmie@1
|
109 class FlaggedLink(models.Model):
|
bgneal@193
|
110 """Model to represent links that have been flagged as broken by users"""
|
gremmie@1
|
111 link = models.ForeignKey(Link)
|
bgneal@237
|
112 user = models.ForeignKey(User)
|
gremmie@1
|
113 date_flagged = models.DateField(auto_now_add = True)
|
bgneal@675
|
114 approved = models.BooleanField(default = False,
|
gremmie@1
|
115 help_text = 'Check this and save to remove the referenced link from the database')
|
gremmie@1
|
116
|
gremmie@1
|
117 objects = FlaggedLinkManager()
|
gremmie@1
|
118
|
bgneal@182
|
119 def save(self, *args, **kwargs):
|
gremmie@1
|
120 if self.approved:
|
gremmie@1
|
121 self.link.delete()
|
gremmie@1
|
122 self.delete()
|
gremmie@1
|
123 else:
|
bgneal@182
|
124 super(FlaggedLink, self).save(*args, **kwargs)
|
gremmie@1
|
125
|
gremmie@1
|
126 def url(self):
|
gremmie@1
|
127 return self.link.url
|
gremmie@1
|
128
|
bgneal@165
|
129 def get_link_url(self):
|
bgneal@165
|
130 return '<a href="%s">Link #%d</a>' % (self.link.get_absolute_url(),
|
bgneal@165
|
131 self.link.id)
|
bgneal@165
|
132 get_link_url.allow_tags = True
|
bgneal@165
|
133 get_link_url.short_description = "View Link on Site"
|
bgneal@165
|
134
|
gremmie@1
|
135 def __unicode__(self):
|
gremmie@1
|
136 return self.link.title
|
gremmie@1
|
137
|
gremmie@1
|
138 class Meta:
|
gremmie@1
|
139 ordering = ('-date_flagged', )
|