Mercurial > public > sg101
view comments/forms.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 | ee87ea74d46b |
children | 4619290d171d |
line wrap: on
line source
""" Forms for the comments application. """ import datetime from django import forms from django.conf import settings from django.contrib.contenttypes.models import ContentType from comments.models import Comment COMMENT_MAX_LENGTH = getattr(settings, 'COMMENT_MAX_LENGTH', 3000) class CommentForm(forms.Form): comment = forms.CharField(label='', min_length=1, max_length=COMMENT_MAX_LENGTH, widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'})) content_type = forms.CharField(widget=forms.HiddenInput) object_pk = forms.CharField(widget=forms.HiddenInput) def __init__(self, target_object, data=None, initial=None): self.target_object = target_object if initial is None: initial = {} initial.update({ 'content_type': str(self.target_object._meta), 'object_pk': str(self.target_object.pk), }) super(CommentForm, self).__init__(data=data, initial=initial) def get_comment_object(self, user, ip_address): """ Return a new (unsaved) comment object based on the information in this form. Assumes that the form is already validated and will throw a ValueError if not. """ if not self.is_valid(): raise ValueError("get_comment_object may only be called on valid forms") new = Comment( content_type = ContentType.objects.get_for_model(self.target_object), object_id = self.target_object.pk, user = user, comment = self.cleaned_data["comment"], ip_address = ip_address, is_public = True, is_removed = False, ) # Check that this comment isn't duplicate. (Sometimes people post comments # twice by mistake.) If it is, fail silently by returning the old comment. today = datetime.date.today() possible_duplicates = Comment.objects.filter( content_type = new.content_type, object_id = new.object_id, user = new.user, creation_date__year = today.year, creation_date__month = today.month, creation_date__day = today.day, ) for old in possible_duplicates: if old.comment == new.comment: return old return new class Media: css = { 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] + settings.GPP_THIRD_PARTY_CSS['jquery-ui']), } js = (settings.GPP_THIRD_PARTY_JS['markitup'] + settings.GPP_THIRD_PARTY_JS['jquery-ui'] + ['js/comments.js'])