Mercurial > public > sg101
comparison gpp/weblinks/models.py @ 1:dbd703f7d63a
Initial import of sg101 stuff from private repository.
author | gremmie |
---|---|
date | Mon, 06 Apr 2009 02:43:12 +0000 |
parents | |
children | c0d0779b266f |
comparison
equal
deleted
inserted
replaced
0:900ba3c7b765 | 1:dbd703f7d63a |
---|---|
1 ''' | |
2 This module contains the models for the weblinks application. | |
3 ''' | |
4 | |
5 from django.db import models | |
6 from django.contrib import auth | |
7 | |
8 | |
9 class Category(models.Model): | |
10 '''Links belong to categories''' | |
11 title = models.CharField(max_length = 64) | |
12 description = models.TextField(blank = True) | |
13 | |
14 def __unicode__(self): | |
15 return self.title | |
16 | |
17 def num_links(self): | |
18 return Link.public_objects.filter(category = self.pk).count() | |
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(is_public=True) | |
29 | |
30 | |
31 class Link(models.Model): | |
32 '''Model to represent a web link''' | |
33 category = models.ForeignKey(Category) | |
34 title = models.CharField(max_length=128) | |
35 url = models.URLField(verify_exists=False, db_index=True) | |
36 description = models.TextField(blank=True) | |
37 user = models.ForeignKey(auth.models.User) | |
38 date_added = models.DateField(auto_now_add=True) | |
39 hits = models.IntegerField(default=0) | |
40 is_public = models.BooleanField(default=False, db_index=True) | |
41 | |
42 # Managers: | |
43 objects = models.Manager() | |
44 public_objects = PublicLinkManager() | |
45 | |
46 def __unicode__(self): | |
47 return self.title | |
48 | |
49 class Meta: | |
50 ordering = ('title', ) | |
51 | |
52 | |
53 class FlaggedLinkManager(models.Manager): | |
54 | |
55 def create(self, link, user): | |
56 flagged_link = FlaggedLink(link = link, user = user, approved = False) | |
57 flagged_link.save() | |
58 | |
59 | |
60 class FlaggedLink(models.Model): | |
61 '''Model to represent links that have been flagged as broken by users''' | |
62 link = models.ForeignKey(Link) | |
63 user = models.ForeignKey(auth.models.User) | |
64 date_flagged = models.DateField(auto_now_add = True) | |
65 approved = models.BooleanField(default = False, | |
66 help_text = 'Check this and save to remove the referenced link from the database') | |
67 | |
68 objects = FlaggedLinkManager() | |
69 | |
70 def save(self, force_insert = False, force_update = False): | |
71 if self.approved: | |
72 self.link.delete() | |
73 self.delete() | |
74 else: | |
75 super(FlaggedLink, self).save(force_insert, force_update) | |
76 | |
77 def url(self): | |
78 return self.link.url | |
79 | |
80 def __unicode__(self): | |
81 return self.link.title | |
82 | |
83 class Meta: | |
84 ordering = ('-date_flagged', ) |