Google Groups Home
Help | Sign in
Patch: Remove `index_model` requirement
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
wnielson  
View profile
 More options Feb 21, 3:06 pm
From: wnielson <wniel...@gmail.com>
Date: Thu, 21 Feb 2008 12:06:27 -0800 (PST)
Local: Thurs, Feb 21 2008 3:06 pm
Subject: Patch: Remove `index_model` requirement
Hi,

I made a post about this on the issue tracker and here is a patch for
it.  It seems like a better idea to decouple index_model from the
model itself for a number of reasons, the most important being that
this allows you to index models which you can/don't want to modify
(django.contrib, for example).

The patch below works, provided you define a new item in your settings
file called INDEXER_INDEXES, like so:

INDEXER_INDEXES = {
    'test.test': 'test_djapian.test.models.Test_index'
    'comments.comment: 'test_djapian.test.indexes.Comment_index'

}

Index: djapian/signals.py
===================================================================
--- djapian/signals.py  (revision 38)
+++ djapian/signals.py  (working copy)
@@ -3,11 +3,24 @@
 Here are the post_save and the pre_delete signals
 """
 from djapian.models import Change
+from django.conf import settings

+def get_index_model(model):
+    '''Return the `index_model` value from
settings.INDEXER_INDEXES'''
+    key = "%s.%s" % (model._meta.app_label, model._meta.module_name)
+    try:
+        return settings.INDEXER_INDEXES[key]
+    except KeyError, e:
+        error_msg = "The object `%s` is not defined in your settings
file\n" % (unicode(model))
+        err = open('djapian_error.log','a')
+        err.write(error_msg)
+        err.close()
+        print error_msg
+
 def post_save(sender, instance):
     '''Create the Change object to update the index'''
-    Change(model=sender.index_model, did=instance.id).save()
+    Change(model=get_index_model(sender), did=instance.id).save()

 def pre_delete(sender, instance):
     '''Create the Change object to update the index'''
-    Change(model=sender.index_model, did=instance.id,
is_deleted=True).save()
+    Change(model=get_index_model(sender), did=instance.id,
is_deleted=True).save()


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google