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
|
bgneal@220
|
63 def search_title(self):
|
bgneal@220
|
64 return self.title
|
bgneal@220
|
65
|
bgneal@220
|
66 def search_summary(self):
|
bgneal@220
|
67 return self.description
|
bgneal@220
|
68
|
gremmie@1
|
69
|
bgneal@204
|
70 class PendingLink(LinkBase):
|
bgneal@204
|
71 """This model represents links that users submit. They must be approved by
|
bgneal@204
|
72 an admin before they become visible on the site.
|
bgneal@204
|
73 """
|
bgneal@204
|
74 class Meta:
|
bgneal@204
|
75 ordering = ('date_added', )
|
bgneal@204
|
76
|
bgneal@204
|
77 def __unicode__(self):
|
bgneal@204
|
78 return self.title
|
bgneal@204
|
79
|
bgneal@204
|
80 def save(self, *args, **kwargs):
|
bgneal@204
|
81 if not self.pk:
|
bgneal@204
|
82 self.date_added = datetime.datetime.now()
|
bgneal@204
|
83 super(PendingLink, self).save(*args, **kwargs)
|
bgneal@204
|
84
|
bgneal@204
|
85
|
gremmie@1
|
86 class FlaggedLinkManager(models.Manager):
|
gremmie@1
|
87
|
gremmie@1
|
88 def create(self, link, user):
|
gremmie@1
|
89 flagged_link = FlaggedLink(link = link, user = user, approved = False)
|
gremmie@1
|
90 flagged_link.save()
|
gremmie@1
|
91
|
gremmie@1
|
92
|
gremmie@1
|
93 class FlaggedLink(models.Model):
|
bgneal@193
|
94 """Model to represent links that have been flagged as broken by users"""
|
gremmie@1
|
95 link = models.ForeignKey(Link)
|
gremmie@1
|
96 user = models.ForeignKey(auth.models.User)
|
gremmie@1
|
97 date_flagged = models.DateField(auto_now_add = True)
|
gremmie@1
|
98 approved = models.BooleanField(default = False,
|
gremmie@1
|
99 help_text = 'Check this and save to remove the referenced link from the database')
|
gremmie@1
|
100
|
gremmie@1
|
101 objects = FlaggedLinkManager()
|
gremmie@1
|
102
|
bgneal@182
|
103 def save(self, *args, **kwargs):
|
gremmie@1
|
104 if self.approved:
|
gremmie@1
|
105 self.link.delete()
|
gremmie@1
|
106 self.delete()
|
gremmie@1
|
107 else:
|
bgneal@182
|
108 super(FlaggedLink, self).save(*args, **kwargs)
|
gremmie@1
|
109
|
gremmie@1
|
110 def url(self):
|
gremmie@1
|
111 return self.link.url
|
gremmie@1
|
112
|
bgneal@165
|
113 def get_link_url(self):
|
bgneal@165
|
114 return '<a href="%s">Link #%d</a>' % (self.link.get_absolute_url(),
|
bgneal@165
|
115 self.link.id)
|
bgneal@165
|
116 get_link_url.allow_tags = True
|
bgneal@165
|
117 get_link_url.short_description = "View Link on Site"
|
bgneal@165
|
118
|
gremmie@1
|
119 def __unicode__(self):
|
gremmie@1
|
120 return self.link.title
|
gremmie@1
|
121
|
gremmie@1
|
122 class Meta:
|
gremmie@1
|
123 ordering = ('-date_flagged', )
|