bgneal@193: """ gremmie@1: This module contains the models for the weblinks application. bgneal@193: """ bgneal@204: import datetime bgneal@204: gremmie@1: from django.db import models bgneal@237: from django.contrib.auth.models import User gremmie@1: gremmie@1: gremmie@1: class Category(models.Model): bgneal@193: """Links belong to categories""" bgneal@193: title = models.CharField(max_length=64) bgneal@193: description = models.TextField(blank=True) bgneal@193: count = models.IntegerField(default=0) gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return self.title gremmie@1: gremmie@1: class Meta: gremmie@1: verbose_name_plural = 'Categories' gremmie@1: ordering = ('title', ) gremmie@1: gremmie@1: gremmie@1: class PublicLinkManager(models.Manager): gremmie@1: """The manager for all public links.""" gremmie@1: def get_query_set(self): bgneal@191: return super(PublicLinkManager, self).get_query_set().filter( bgneal@191: is_public=True).select_related() gremmie@1: gremmie@1: bgneal@204: class LinkBase(models.Model): bgneal@204: """Abstract model to aggregate common fields of a web link.""" gremmie@1: category = models.ForeignKey(Category) gremmie@1: title = models.CharField(max_length=128) gremmie@1: url = models.URLField(verify_exists=False, db_index=True) gremmie@1: description = models.TextField(blank=True) bgneal@237: user = models.ForeignKey(User) bgneal@204: date_added = models.DateField() bgneal@204: bgneal@204: class Meta: bgneal@204: abstract = True bgneal@204: bgneal@204: bgneal@204: class Link(LinkBase): bgneal@204: """Model to represent a web link""" gremmie@1: hits = models.IntegerField(default=0) gremmie@1: is_public = models.BooleanField(default=False, db_index=True) gremmie@1: gremmie@1: # Managers: gremmie@1: objects = models.Manager() gremmie@1: public_objects = PublicLinkManager() gremmie@1: bgneal@20: class Meta: bgneal@20: ordering = ('title', ) bgneal@20: gremmie@1: def __unicode__(self): gremmie@1: return self.title gremmie@1: bgneal@20: @models.permalink bgneal@20: def get_absolute_url(self): bgneal@20: return ('weblinks-link_detail', [str(self.id)]) gremmie@1: bgneal@220: def search_title(self): bgneal@220: return self.title bgneal@220: bgneal@220: def search_summary(self): bgneal@220: return self.description bgneal@220: gremmie@1: bgneal@204: class PendingLink(LinkBase): bgneal@204: """This model represents links that users submit. They must be approved by bgneal@204: an admin before they become visible on the site. bgneal@204: """ bgneal@204: class Meta: bgneal@204: ordering = ('date_added', ) bgneal@204: bgneal@204: def __unicode__(self): bgneal@204: return self.title bgneal@204: bgneal@204: def save(self, *args, **kwargs): bgneal@204: if not self.pk: bgneal@204: self.date_added = datetime.datetime.now() bgneal@204: super(PendingLink, self).save(*args, **kwargs) bgneal@204: bgneal@204: gremmie@1: class FlaggedLinkManager(models.Manager): gremmie@1: gremmie@1: def create(self, link, user): gremmie@1: flagged_link = FlaggedLink(link = link, user = user, approved = False) gremmie@1: flagged_link.save() gremmie@1: gremmie@1: gremmie@1: class FlaggedLink(models.Model): bgneal@193: """Model to represent links that have been flagged as broken by users""" gremmie@1: link = models.ForeignKey(Link) bgneal@237: user = models.ForeignKey(User) gremmie@1: date_flagged = models.DateField(auto_now_add = True) gremmie@1: approved = models.BooleanField(default = False, gremmie@1: help_text = 'Check this and save to remove the referenced link from the database') gremmie@1: gremmie@1: objects = FlaggedLinkManager() gremmie@1: bgneal@182: def save(self, *args, **kwargs): gremmie@1: if self.approved: gremmie@1: self.link.delete() gremmie@1: self.delete() gremmie@1: else: bgneal@182: super(FlaggedLink, self).save(*args, **kwargs) gremmie@1: gremmie@1: def url(self): gremmie@1: return self.link.url gremmie@1: bgneal@165: def get_link_url(self): bgneal@165: return 'Link #%d' % (self.link.get_absolute_url(), bgneal@165: self.link.id) bgneal@165: get_link_url.allow_tags = True bgneal@165: get_link_url.short_description = "View Link on Site" bgneal@165: gremmie@1: def __unicode__(self): gremmie@1: return self.link.title gremmie@1: gremmie@1: class Meta: gremmie@1: ordering = ('-date_flagged', )