annotate comments/models.py @ 1154:4da4e32b314c

Do not restrict oEmbed support to just video. This will allow us to embed stuff from SoundClound and ReverbNation.
author Brian Neal <bgneal@gmail.com>
date Tue, 27 Dec 2016 10:21:37 -0600
parents 5ba2508939f7
children
rev   line source
gremmie@1 1 """
gremmie@1 2 Models for the comments application.
gremmie@1 3 """
bgneal@294 4 import datetime
bgneal@294 5
gremmie@1 6 from django.db import models
gremmie@1 7 from django.conf import settings
gremmie@1 8 from django.contrib.contenttypes.models import ContentType
bgneal@1028 9 from django.contrib.contenttypes.fields import GenericForeignKey
gremmie@1 10 from django.contrib.auth.models import User
bgneal@14 11 from django.core import urlresolvers
gremmie@1 12
bgneal@128 13 from core.markup import site_markup
bgneal@124 14
gremmie@1 15
gremmie@1 16 COMMENT_MAX_LENGTH = getattr(settings, 'COMMENT_MAX_LENGTH', 3000)
gremmie@1 17
gremmie@1 18 class CommentManager(models.Manager):
gremmie@1 19 """Manager for the Comment model class."""
gremmie@1 20
gremmie@1 21 def for_object(self, obj, filter_public=True):
gremmie@1 22 """QuerySet for all comments for a particular model instance."""
gremmie@1 23 ct = ContentType.objects.get_for_model(obj)
bgneal@743 24 qs = self.get_queryset().filter(content_type__pk=ct.id,
gremmie@1 25 object_id=obj.id)
gremmie@1 26 if filter_public:
gremmie@1 27 qs = qs.filter(is_public=True)
gremmie@1 28 return qs
gremmie@1 29
gremmie@1 30
gremmie@1 31 class Comment(models.Model):
gremmie@1 32 """My own version of a Comment class that can attach comments to any other model."""
gremmie@1 33 content_type = models.ForeignKey(ContentType)
bgneal@399 34 object_id = models.PositiveIntegerField(db_index=True)
bgneal@1028 35 content_object = GenericForeignKey('content_type', 'object_id')
gremmie@1 36 user = models.ForeignKey(User)
gremmie@1 37 comment = models.TextField(max_length=COMMENT_MAX_LENGTH)
gremmie@1 38 html = models.TextField(blank=True)
bgneal@294 39 creation_date = models.DateTimeField()
bgneal@1028 40 ip_address = models.GenericIPAddressField('IP Address')
bgneal@294 41 is_public = models.BooleanField(default=True,
gremmie@1 42 help_text='Uncheck this field to make the comment invisible.')
gremmie@1 43 is_removed = models.BooleanField(default=False,
gremmie@1 44 help_text='Check this field to replace the comment with a ' \
gremmie@1 45 '"This comment has been removed" message')
gremmie@1 46
gremmie@1 47 # Attach manager
gremmie@1 48 objects = CommentManager()
gremmie@1 49
bgneal@14 50 class Meta:
bgneal@14 51 ordering = ('creation_date', )
bgneal@14 52
gremmie@1 53 def __unicode__(self):
gremmie@1 54 return u'%s: %s...' % (self.user.username, self.comment[:50])
gremmie@1 55
bgneal@182 56 def save(self, *args, **kwargs):
bgneal@294 57 if not self.id:
bgneal@294 58 self.creation_date = datetime.datetime.now()
bgneal@294 59
bgneal@963 60 self.html = kwargs.pop('html', None)
bgneal@963 61 if self.html is None:
bgneal@963 62 self.html = site_markup(self.comment)
bgneal@182 63 super(Comment, self).save(*args, **kwargs)
gremmie@1 64
bgneal@14 65 def get_absolute_url(self):
bgneal@14 66 return self.get_content_object_url() + ('#c%s' % self.id)
bgneal@14 67
bgneal@14 68 def get_content_object_url(self):
bgneal@14 69 """
bgneal@14 70 Get a URL suitable for redirecting to the content object.
bgneal@14 71 """
bgneal@14 72 return urlresolvers.reverse(
bgneal@14 73 "comments-url-redirect",
bgneal@14 74 args=(self.content_type_id, self.object_id)
bgneal@14 75 )
bgneal@14 76
bgneal@140 77 def not_removed(self):
bgneal@140 78 """
bgneal@140 79 Returns not self.is_removed. Used on the admin display for
bgneal@140 80 "green board" display purposes.
bgneal@140 81 """
bgneal@140 82 return not self.is_removed
bgneal@140 83 not_removed.boolean = True
bgneal@140 84
gremmie@1 85
gremmie@1 86 class CommentFlag(models.Model):
gremmie@1 87 """This model represents a user flagging a comment as inappropriate."""
gremmie@1 88 user = models.ForeignKey(User)
gremmie@1 89 comment = models.ForeignKey(Comment)
gremmie@1 90 flag_date = models.DateTimeField(auto_now_add=True)
gremmie@1 91
gremmie@1 92 def __unicode__(self):
bgneal@98 93 return u'Comment ID %s flagged by %s' % (self.comment.id, self.user.username)
gremmie@1 94
gremmie@1 95 class Meta:
gremmie@1 96 ordering = ('flag_date', )
gremmie@1 97
gremmie@1 98 def get_comment_url(self):
gremmie@1 99 return '<a href="/admin/comments/comment/%s">Comment</a>' % self.comment.id
gremmie@1 100 get_comment_url.allow_tags = True
gremmie@1 101