Mercurial > public > sg101
annotate weblinks/receivers.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 | 6d08b1476a52 |
children |
rev | line source |
---|---|
bgneal@921 | 1 """Signal handlers for the weblinks application. |
bgneal@921 | 2 |
bgneal@202 | 3 We use signals to compute the denormalized category counts whenever a weblink |
bgneal@921 | 4 is saved. |
bgneal@921 | 5 """ |
bgneal@202 | 6 from django.db.models.signals import post_save |
bgneal@202 | 7 from django.db.models.signals import post_delete |
bgneal@202 | 8 |
bgneal@202 | 9 from weblinks.models import Category, Link |
bgneal@202 | 10 |
bgneal@202 | 11 |
bgneal@202 | 12 def on_link_save(sender, **kwargs): |
bgneal@202 | 13 """This function updates the count field for all categories. |
bgneal@202 | 14 It is called whenever a link is saved via a signal. |
bgneal@202 | 15 """ |
bgneal@202 | 16 if kwargs['created']: |
bgneal@202 | 17 # we only have to update the parent category |
bgneal@202 | 18 link = kwargs['instance'] |
bgneal@202 | 19 cat = link.category |
bgneal@202 | 20 cat.count = Link.public_objects.filter(category=cat).count() |
bgneal@202 | 21 cat.save() |
bgneal@202 | 22 else: |
bgneal@202 | 23 # update all categories just to be safe (an existing link could |
bgneal@202 | 24 # have been moved from one category to another |
bgneal@202 | 25 cats = Category.objects.all() |
bgneal@202 | 26 for cat in cats: |
bgneal@202 | 27 cat.count = Link.public_objects.filter(category=cat).count() |
bgneal@202 | 28 cat.save() |
bgneal@202 | 29 |
bgneal@202 | 30 |
bgneal@202 | 31 def on_link_delete(sender, **kwargs): |
bgneal@202 | 32 """This function updates the count field for the link's parent |
bgneal@202 | 33 category. It is called when a link is deleted via a signal. |
bgneal@202 | 34 """ |
bgneal@202 | 35 # update the parent category |
bgneal@202 | 36 link = kwargs['instance'] |
bgneal@202 | 37 cat = link.category |
bgneal@202 | 38 cat.count = Link.public_objects.filter(category=cat).count() |
bgneal@202 | 39 cat.save() |
bgneal@202 | 40 |
bgneal@202 | 41 |
bgneal@921 | 42 post_save.connect(on_link_save, sender=Link, dispatch_uid='weblinks.receivers') |
bgneal@921 | 43 post_delete.connect(on_link_delete, sender=Link, dispatch_uid='weblinks.receivers') |