Mercurial > public > sg101
annotate forums/attachments.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 |
rev | line source |
---|---|
bgneal@285 | 1 """ |
bgneal@285 | 2 This module contains a class for handling attachments on forum posts. |
bgneal@285 | 3 """ |
bgneal@285 | 4 from oembed.models import Oembed |
bgneal@285 | 5 from forums.models import Attachment |
bgneal@285 | 6 |
bgneal@285 | 7 |
bgneal@285 | 8 class AttachmentProcessor(object): |
bgneal@285 | 9 """ |
bgneal@285 | 10 This class is aggregated by various form classes to handle |
bgneal@285 | 11 attachments on forum posts. New posts can receive attachments and edited |
bgneal@285 | 12 posts can have their attachments replaced, augmented, or deleted. |
bgneal@285 | 13 |
bgneal@285 | 14 """ |
bgneal@285 | 15 def __init__(self, ids): |
bgneal@285 | 16 """ |
bgneal@285 | 17 This class is constructed with a list of Oembed ids. We retrieve the |
bgneal@285 | 18 actual Oembed objects associated with these keys for use in subsequent |
bgneal@285 | 19 operations. |
bgneal@286 | 20 |
bgneal@285 | 21 """ |
bgneal@285 | 22 # ensure all ids are integers |
bgneal@285 | 23 self.pks = [] |
bgneal@285 | 24 for pk in ids: |
bgneal@285 | 25 try: |
bgneal@285 | 26 pk = int(pk) |
bgneal@285 | 27 except ValueError: |
bgneal@285 | 28 continue |
bgneal@285 | 29 self.pks.append(pk) |
bgneal@285 | 30 |
bgneal@285 | 31 self.embeds = [] |
bgneal@285 | 32 if self.pks: |
bgneal@285 | 33 self.embeds = Oembed.objects.in_bulk(self.pks) |
bgneal@285 | 34 |
bgneal@285 | 35 def save_attachments(self, post): |
bgneal@285 | 36 """ |
bgneal@285 | 37 Create and save attachments to the supplied post object. |
bgneal@285 | 38 Any existing attachments on the post are removed first. |
bgneal@285 | 39 |
bgneal@285 | 40 """ |
bgneal@285 | 41 post.attachments.clear() |
bgneal@285 | 42 |
bgneal@285 | 43 for n, pk in enumerate(self.pks): |
bgneal@285 | 44 attachment = Attachment(post=post, embed=self.embeds[pk], order=n) |
bgneal@285 | 45 attachment.save() |
bgneal@286 | 46 |
bgneal@286 | 47 def has_attachments(self): |
bgneal@286 | 48 """ |
bgneal@286 | 49 Return true if we have valid pending attachments. |
bgneal@286 | 50 |
bgneal@286 | 51 """ |
bgneal@286 | 52 return len(self.embeds) > 0 |
bgneal@286 | 53 |
bgneal@286 | 54 def get_ids(self): |
bgneal@286 | 55 """ |
bgneal@286 | 56 Return the list of Oembed ids. |
bgneal@286 | 57 |
bgneal@286 | 58 """ |
bgneal@286 | 59 return self.pks |