forked from mparisot-wescale/memopol2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
96 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# -*- coding: utf-8 -*- | ||
import os | ||
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' | ||
import logging | ||
from django.db.models import signals | ||
from django.conf import settings | ||
from whoosh import fields, index | ||
from whoosh.filedb.filestore import FileStorage | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
WHOOSH_SCHEMA = fields.Schema(title=fields.TEXT(stored=True), | ||
content=fields.TEXT, | ||
url=fields.ID(stored=True, unique=True)) | ||
|
||
def create_index(sender=None, **kwargs): | ||
if not os.path.exists(settings.WHOOSH_INDEX): | ||
os.mkdir(settings.WHOOSH_INDEX) | ||
storage = FileStorage(settings.WHOOSH_INDEX) | ||
storage.create_index(WHOOSH_SCHEMA, indexname='memopol') | ||
|
||
signals.post_syncdb.connect(create_index) | ||
|
||
def update_index(sender, instance, created, **kwargs): | ||
storage = FileStorage(settings.WHOOSH_INDEX) | ||
ix = storage.open_index(indexname='memopol') | ||
writer = ix.writer() | ||
content = getattr(instance, 'content', None) | ||
if content is None: | ||
content = unicode(instance) | ||
elif callable(content): | ||
content = content() | ||
if created: | ||
writer.add_document(title=unicode(instance), content=content, | ||
url=unicode(instance.get_absolute_url())) | ||
writer.commit() | ||
else: | ||
writer.update_document(title=unicode(instance), content=content, | ||
url=unicode(instance.get_absolute_url())) | ||
writer.commit() | ||
|
||
_searchables = [] | ||
def searchable(klass): | ||
if hasattr(klass, 'get_absolute_url'): | ||
signals.post_save.connect(update_index, sender=klass) | ||
_searchables.append(klass) | ||
if not hasattr(klass, 'content'): | ||
log.warn('%s is declared as searchable but has no content attribute' % klass) | ||
else: | ||
log.warn('%s is declared as searchable but has no get_absolute_url' % klass) | ||
return klass | ||
|
||
def update(): | ||
from meps import models | ||
from mps import models | ||
from reps import models | ||
create_index() | ||
for klass in _searchables: | ||
for i in klass.objects.all(): | ||
update_index(None, i, created=False) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters