bgneal@193
|
1 """
|
gremmie@1
|
2 This module contains the models for the weblinks application.
|
bgneal@193
|
3 """
|
gremmie@1
|
4 from django.db import models
|
gremmie@1
|
5 from django.contrib import auth
|
gremmie@1
|
6
|
gremmie@1
|
7
|
gremmie@1
|
8 class Category(models.Model):
|
bgneal@193
|
9 """Links belong to categories"""
|
bgneal@193
|
10 title = models.CharField(max_length=64)
|
bgneal@193
|
11 description = models.TextField(blank=True)
|
bgneal@193
|
12 count = models.IntegerField(default=0)
|
gremmie@1
|
13
|
gremmie@1
|
14 def __unicode__(self):
|
gremmie@1
|
15 return self.title
|
gremmie@1
|
16
|
gremmie@1
|
17 class Meta:
|
gremmie@1
|
18 verbose_name_plural = 'Categories'
|
gremmie@1
|
19 ordering = ('title', )
|
gremmie@1
|
20
|
gremmie@1
|
21
|
gremmie@1
|
22 class PublicLinkManager(models.Manager):
|
gremmie@1
|
23 """The manager for all public links."""
|
gremmie@1
|
24 def get_query_set(self):
|
bgneal@191
|
25 return super(PublicLinkManager, self).get_query_set().filter(
|
bgneal@191
|
26 is_public=True).select_related()
|
gremmie@1
|
27
|
gremmie@1
|
28
|
gremmie@1
|
29 class Link(models.Model):
|
bgneal@193
|
30 """Model to represent a web link"""
|
gremmie@1
|
31 category = models.ForeignKey(Category)
|
gremmie@1
|
32 title = models.CharField(max_length=128)
|
gremmie@1
|
33 url = models.URLField(verify_exists=False, db_index=True)
|
gremmie@1
|
34 description = models.TextField(blank=True)
|
gremmie@1
|
35 user = models.ForeignKey(auth.models.User)
|
gremmie@1
|
36 date_added = models.DateField(auto_now_add=True)
|
gremmie@1
|
37 hits = models.IntegerField(default=0)
|
gremmie@1
|
38 is_public = models.BooleanField(default=False, db_index=True)
|
gremmie@1
|
39
|
gremmie@1
|
40 # Managers:
|
gremmie@1
|
41 objects = models.Manager()
|
gremmie@1
|
42 public_objects = PublicLinkManager()
|
gremmie@1
|
43
|
bgneal@20
|
44 class Meta:
|
bgneal@20
|
45 ordering = ('title', )
|
bgneal@20
|
46
|
gremmie@1
|
47 def __unicode__(self):
|
gremmie@1
|
48 return self.title
|
gremmie@1
|
49
|
bgneal@20
|
50 @models.permalink
|
bgneal@20
|
51 def get_absolute_url(self):
|
bgneal@20
|
52 return ('weblinks-link_detail', [str(self.id)])
|
gremmie@1
|
53
|
gremmie@1
|
54
|
gremmie@1
|
55 class FlaggedLinkManager(models.Manager):
|
gremmie@1
|
56
|
gremmie@1
|
57 def create(self, link, user):
|
gremmie@1
|
58 flagged_link = FlaggedLink(link = link, user = user, approved = False)
|
gremmie@1
|
59 flagged_link.save()
|
gremmie@1
|
60
|
gremmie@1
|
61
|
gremmie@1
|
62 class FlaggedLink(models.Model):
|
bgneal@193
|
63 """Model to represent links that have been flagged as broken by users"""
|
gremmie@1
|
64 link = models.ForeignKey(Link)
|
gremmie@1
|
65 user = models.ForeignKey(auth.models.User)
|
gremmie@1
|
66 date_flagged = models.DateField(auto_now_add = True)
|
gremmie@1
|
67 approved = models.BooleanField(default = False,
|
gremmie@1
|
68 help_text = 'Check this and save to remove the referenced link from the database')
|
gremmie@1
|
69
|
gremmie@1
|
70 objects = FlaggedLinkManager()
|
gremmie@1
|
71
|
bgneal@182
|
72 def save(self, *args, **kwargs):
|
gremmie@1
|
73 if self.approved:
|
gremmie@1
|
74 self.link.delete()
|
gremmie@1
|
75 self.delete()
|
gremmie@1
|
76 else:
|
bgneal@182
|
77 super(FlaggedLink, self).save(*args, **kwargs)
|
gremmie@1
|
78
|
gremmie@1
|
79 def url(self):
|
gremmie@1
|
80 return self.link.url
|
gremmie@1
|
81
|
bgneal@165
|
82 def get_link_url(self):
|
bgneal@165
|
83 return '<a href="%s">Link #%d</a>' % (self.link.get_absolute_url(),
|
bgneal@165
|
84 self.link.id)
|
bgneal@165
|
85 get_link_url.allow_tags = True
|
bgneal@165
|
86 get_link_url.short_description = "View Link on Site"
|
bgneal@165
|
87
|
gremmie@1
|
88 def __unicode__(self):
|
gremmie@1
|
89 return self.link.title
|
gremmie@1
|
90
|
gremmie@1
|
91 class Meta:
|
gremmie@1
|
92 ordering = ('-date_flagged', )
|