Mercurial > public > sg101
view comments/models.py @ 943:cf9918328c64
Haystack tweaks for Django 1.7.7.
I had to upgrade to Haystack 2.3.1 to get it to work with Django
1.7.7. I also had to update the Xapian backend. But I ran into
problems.
On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms
are greater than 245 chars (or something) when indexing. So I created
a custom field that would simply omit terms greater than 64 chars and
used this field everywhere I previously used a CharField.
Secondly, the custom search form was broken now. Something changed in
the Xapian backend and exact searches stopped working. Fortunately the
auto_query (which I was using originally and broke during an upgrade)
started working again. So I cut the search form back over to doing an
auto_query. I kept the form the same (3 fields) because I didn't want
to change the form and I think it's better that way.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 13 May 2015 20:25:07 -0500 |
parents | 66d46d31d543 |
children | 4619290d171d |
line wrap: on
line source
""" Models for the comments application. """ import datetime from django.db import models from django.conf import settings from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import generic from django.contrib.auth.models import User from django.core import urlresolvers from core.markup import site_markup COMMENT_MAX_LENGTH = getattr(settings, 'COMMENT_MAX_LENGTH', 3000) class CommentManager(models.Manager): """Manager for the Comment model class.""" def for_object(self, obj, filter_public=True): """QuerySet for all comments for a particular model instance.""" ct = ContentType.objects.get_for_model(obj) qs = self.get_queryset().filter(content_type__pk=ct.id, object_id=obj.id) if filter_public: qs = qs.filter(is_public=True) return qs class Comment(models.Model): """My own version of a Comment class that can attach comments to any other model.""" content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField(db_index=True) content_object = generic.GenericForeignKey('content_type', 'object_id') user = models.ForeignKey(User) comment = models.TextField(max_length=COMMENT_MAX_LENGTH) html = models.TextField(blank=True) creation_date = models.DateTimeField() ip_address = models.IPAddressField('IP Address') is_public = models.BooleanField(default=True, help_text='Uncheck this field to make the comment invisible.') is_removed = models.BooleanField(default=False, help_text='Check this field to replace the comment with a ' \ '"This comment has been removed" message') # Attach manager objects = CommentManager() class Meta: ordering = ('creation_date', ) def __unicode__(self): return u'%s: %s...' % (self.user.username, self.comment[:50]) def save(self, *args, **kwargs): if not self.id: self.creation_date = datetime.datetime.now() self.html = site_markup(self.comment) super(Comment, self).save(*args, **kwargs) def get_absolute_url(self): return self.get_content_object_url() + ('#c%s' % self.id) def get_content_object_url(self): """ Get a URL suitable for redirecting to the content object. """ return urlresolvers.reverse( "comments-url-redirect", args=(self.content_type_id, self.object_id) ) def not_removed(self): """ Returns not self.is_removed. Used on the admin display for "green board" display purposes. """ return not self.is_removed not_removed.boolean = True class CommentFlag(models.Model): """This model represents a user flagging a comment as inappropriate.""" user = models.ForeignKey(User) comment = models.ForeignKey(Comment) flag_date = models.DateTimeField(auto_now_add=True) def __unicode__(self): return u'Comment ID %s flagged by %s' % (self.comment.id, self.user.username) class Meta: ordering = ('flag_date', ) def get_comment_url(self): return '<a href="/admin/comments/comment/%s">Comment</a>' % self.comment.id get_comment_url.allow_tags = True