Copyright 2008 Optaros Inc django_settings so when we do solango impor

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#
# Copyright 2008 Optaros, Inc.
#
# django_settings so when we do solango import settings we don't get confused
from django.conf import settings as django_settings
from django.db.models import signals
from django.db.models.base import ModelBase
registry = {}
#Fields so we can do run things like solango.CharField
from solango.solr import fields
from solango.solr import get_model_key
from solango.solr.connection import SearchWrapper
from solango.solr.documents import SearchDocument
class AlreadyRegistered(Exception):
pass
class NotRegistered(Exception):
pass
connection = SearchWrapper()
SearchDocument = SearchDocument
def post_save(sender, instance, created, *args, **kwargs):
"""
Apply any necessary pre-save moderation steps to new
comments.
"""
key = get_model_key(instance)
if key not in registry.keys():
return None
document = registry[key](instance)
# Could be a pain, but only way to make sure the index is updated.
if document.is_indexable(instance):
#Note adding and updating a document in solr uses the same command
connection.add([document])
else:
connection.delete([document])
def post_delete( sender, instance, *args, **kwargs):
"""
Apply any necessary post-save moderation steps to new
comments.
"""
key = get_model_key(instance)
if key not in registry.keys():
return None
document = registry[key](instance)
connection.delete([document,])
def register(model_or_iterable, search_document=None):
if isinstance(model_or_iterable, ModelBase):
model_or_iterable = [model_or_iterable]
for model in model_or_iterable:
#Register the model
if model in registry:
raise AlreadyRegistered('%s has already been registered by search' % model)
if not search_document:
#Default Search Document if no document is specified.
search_document = SearchDocument
key = get_model_key(model)
registry[key] = search_document
#Hook Up The Signals
signals.post_save.connect(post_save, model)
signals.post_delete.connect(post_delete, model)
for a in django_settings.INSTALLED_APPS:
try:
"""
This will call all the fun things in the search documents
"""
module = __import__(a + '.search', {}, {}, [''])
except ImportError, e:
pass
def get_document(instance):
"""
Helper to get document from instance
"""
key = get_model_key(instance)
if key not in registry.keys():
raise NotRegistered('Instance not reqistered with Solango')
return registry[key](instance)