annotate weblinks/models.py @ 1081:ecb8f07d937b

Added countdown application.
author Brian Neal <bgneal@gmail.com>
date Sun, 24 Apr 2016 12:27:26 -0500
parents eeaf387803c6
children
rev   line source
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
bgneal@1035 6 from django.contrib.auth.models import User
bgneal@1035 7 from django.core.urlresolvers import reverse
gremmie@1 8 from django.db import models
gremmie@1 9
gremmie@1 10
gremmie@1 11 class Category(models.Model):
bgneal@193 12 """Links belong to categories"""
bgneal@193 13 title = models.CharField(max_length=64)
bgneal@242 14 slug = models.SlugField(max_length=64)
bgneal@193 15 description = models.TextField(blank=True)
bgneal@193 16 count = models.IntegerField(default=0)
gremmie@1 17
gremmie@1 18 def __unicode__(self):
gremmie@1 19 return self.title
gremmie@1 20
gremmie@1 21 class Meta:
gremmie@1 22 verbose_name_plural = 'Categories'
gremmie@1 23 ordering = ('title', )
gremmie@1 24
gremmie@1 25
gremmie@1 26 class PublicLinkManager(models.Manager):
gremmie@1 27 """The manager for all public links."""
bgneal@740 28 def get_queryset(self):
bgneal@740 29 return super(PublicLinkManager, self).get_queryset().filter(
bgneal@191 30 is_public=True).select_related()
gremmie@1 31
gremmie@1 32
bgneal@204 33 class LinkBase(models.Model):
bgneal@204 34 """Abstract model to aggregate common fields of a web link."""
gremmie@1 35 category = models.ForeignKey(Category)
gremmie@1 36 title = models.CharField(max_length=128)
bgneal@572 37 url = models.URLField(db_index=True)
gremmie@1 38 description = models.TextField(blank=True)
bgneal@237 39 user = models.ForeignKey(User)
bgneal@277 40 date_added = models.DateTimeField(db_index=True)
bgneal@277 41 update_date = models.DateTimeField(db_index=True, blank=True)
bgneal@204 42
bgneal@204 43 class Meta:
bgneal@204 44 abstract = True
bgneal@204 45
bgneal@204 46
bgneal@204 47 class Link(LinkBase):
bgneal@204 48 """Model to represent a web link"""
gremmie@1 49 hits = models.IntegerField(default=0)
gremmie@1 50 is_public = models.BooleanField(default=False, db_index=True)
gremmie@1 51
gremmie@1 52 # Managers:
gremmie@1 53 objects = models.Manager()
gremmie@1 54 public_objects = PublicLinkManager()
gremmie@1 55
bgneal@20 56 class Meta:
bgneal@20 57 ordering = ('title', )
bgneal@20 58
gremmie@1 59 def __unicode__(self):
gremmie@1 60 return self.title
gremmie@1 61
bgneal@277 62 def save(self, *args, **kwargs):
bgneal@277 63 if not self.pk:
bgneal@277 64 if not self.date_added:
bgneal@277 65 self.date_added = datetime.datetime.now()
bgneal@277 66 self.update_date = self.date_added
bgneal@277 67 else:
bgneal@277 68 self.update_date = datetime.datetime.now()
bgneal@277 69
bgneal@277 70 super(Link, self).save(*args, **kwargs)
bgneal@277 71
bgneal@20 72 def get_absolute_url(self):
bgneal@1035 73 return reverse('weblinks-link_detail', args=[str(self.pk)])
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', )