gremmie@1: """ gremmie@1: Models for the comments application. gremmie@1: """ bgneal@294: import datetime bgneal@294: gremmie@1: from django.db import models gremmie@1: from django.conf import settings gremmie@1: from django.contrib.contenttypes.models import ContentType gremmie@1: from django.contrib.contenttypes import generic gremmie@1: from django.contrib.auth.models import User bgneal@14: from django.core import urlresolvers gremmie@1: bgneal@128: from core.markup import site_markup bgneal@124: gremmie@1: gremmie@1: COMMENT_MAX_LENGTH = getattr(settings, 'COMMENT_MAX_LENGTH', 3000) gremmie@1: gremmie@1: class CommentManager(models.Manager): gremmie@1: """Manager for the Comment model class.""" gremmie@1: gremmie@1: def for_object(self, obj, filter_public=True): gremmie@1: """QuerySet for all comments for a particular model instance.""" gremmie@1: ct = ContentType.objects.get_for_model(obj) bgneal@743: qs = self.get_queryset().filter(content_type__pk=ct.id, gremmie@1: object_id=obj.id) gremmie@1: if filter_public: gremmie@1: qs = qs.filter(is_public=True) gremmie@1: return qs gremmie@1: gremmie@1: gremmie@1: class Comment(models.Model): gremmie@1: """My own version of a Comment class that can attach comments to any other model.""" gremmie@1: content_type = models.ForeignKey(ContentType) bgneal@399: object_id = models.PositiveIntegerField(db_index=True) gremmie@1: content_object = generic.GenericForeignKey('content_type', 'object_id') gremmie@1: user = models.ForeignKey(User) gremmie@1: comment = models.TextField(max_length=COMMENT_MAX_LENGTH) gremmie@1: html = models.TextField(blank=True) bgneal@294: creation_date = models.DateTimeField() gremmie@1: ip_address = models.IPAddressField('IP Address') bgneal@294: is_public = models.BooleanField(default=True, gremmie@1: help_text='Uncheck this field to make the comment invisible.') gremmie@1: is_removed = models.BooleanField(default=False, gremmie@1: help_text='Check this field to replace the comment with a ' \ gremmie@1: '"This comment has been removed" message') gremmie@1: gremmie@1: # Attach manager gremmie@1: objects = CommentManager() gremmie@1: bgneal@14: class Meta: bgneal@14: ordering = ('creation_date', ) bgneal@14: gremmie@1: def __unicode__(self): gremmie@1: return u'%s: %s...' % (self.user.username, self.comment[:50]) gremmie@1: bgneal@182: def save(self, *args, **kwargs): bgneal@294: if not self.id: bgneal@294: self.creation_date = datetime.datetime.now() bgneal@294: bgneal@128: self.html = site_markup(self.comment) bgneal@182: super(Comment, self).save(*args, **kwargs) gremmie@1: bgneal@14: def get_absolute_url(self): bgneal@14: return self.get_content_object_url() + ('#c%s' % self.id) bgneal@14: bgneal@14: def get_content_object_url(self): bgneal@14: """ bgneal@14: Get a URL suitable for redirecting to the content object. bgneal@14: """ bgneal@14: return urlresolvers.reverse( bgneal@14: "comments-url-redirect", bgneal@14: args=(self.content_type_id, self.object_id) bgneal@14: ) bgneal@14: bgneal@140: def not_removed(self): bgneal@140: """ bgneal@140: Returns not self.is_removed. Used on the admin display for bgneal@140: "green board" display purposes. bgneal@140: """ bgneal@140: return not self.is_removed bgneal@140: not_removed.boolean = True bgneal@140: gremmie@1: gremmie@1: class CommentFlag(models.Model): gremmie@1: """This model represents a user flagging a comment as inappropriate.""" gremmie@1: user = models.ForeignKey(User) gremmie@1: comment = models.ForeignKey(Comment) gremmie@1: flag_date = models.DateTimeField(auto_now_add=True) gremmie@1: gremmie@1: def __unicode__(self): bgneal@98: return u'Comment ID %s flagged by %s' % (self.comment.id, self.user.username) gremmie@1: gremmie@1: class Meta: gremmie@1: ordering = ('flag_date', ) gremmie@1: gremmie@1: def get_comment_url(self): gremmie@1: return 'Comment' % self.comment.id gremmie@1: get_comment_url.allow_tags = True gremmie@1: