Skip to content

Commit

Permalink
Merge branch 'master' into pr/17
Browse files Browse the repository at this point in the history
  • Loading branch information
gyllstromk committed Jan 4, 2015
2 parents 70dc6f4 + ecaffee commit 461a201
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 38 deletions.
24 changes: 11 additions & 13 deletions flask_whooshalchemy.py
Expand Up @@ -42,18 +42,15 @@ class _QueryProxy(flask_sqlalchemy.BaseQuery):
# thing this proxy does is override the __iter__ method so that results are
# returned in the order of the whoosh score to reflect text-based ranking.

def __init__(self, query_obj, primary_key_name, whoosh_searcher, model):
def __init__(self, entities, session=None):
super(_QueryProxy, self).__init__(entities, session)

# Make this a pure copy of the original Query object.
self.__dict__ = query_obj.__dict__.copy()

self._primary_key_name = primary_key_name
self._whoosh_searcher = whoosh_searcher
self._modelclass = model
self._modelclass = self._mapper_zero().class_
self._primary_key_name = self._modelclass.whoosh_primary_key
self._whoosh_searcher = self._modelclass.pure_whoosh

# Stores whoosh results from query. If ``None``, indicates that no
# whoosh query was performed.

self._whoosh_rank = None

def __iter__(self):
Expand Down Expand Up @@ -102,7 +99,7 @@ def whoosh_search(self, query, limit=None, fields=None, or_=False):
parameter to ``True``.
'''

if not isinstance(query, unicode):
query = unicode(query)

Expand Down Expand Up @@ -199,12 +196,13 @@ def _create_index(app, model):
indx = whoosh.index.create_in(wi, schema)

app.whoosh_indexes[model.__name__] = indx
searcher = _Searcher(primary_key, indx)
model.query = _QueryProxy(model.query, primary_key,
searcher, model)

model.pure_whoosh = searcher
model.pure_whoosh = _Searcher(primary_key, indx)
model.whoosh_primary_key = primary_key

# change the query class of this model to our own
model.query_class = _QueryProxy

return indx


Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
@@ -1,4 +1,4 @@
Flask-SQLAlchemy
SQLAlchemy
Whoosh
blinker
Flask==0.10.1
Flask-SQLAlchemy==1.0
Whoosh==2.6.0
blinker==1.3
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -11,7 +11,7 @@

setup(
name='Flask-WhooshAlchemy',
version='0.55',
version='0.56',
url='https://github.com/gyllstromk/Flask-WhooshAlchemy',
license='BSD',
author='Karl Gyllstrom',
Expand All @@ -25,7 +25,7 @@
platforms='any',
install_requires=[x.strip() for x in
open(os.path.join(os.path.dirname(__file__),
'requirements.txt')).xreadlines()],
'requirements.txt'))],
tests_require=['Flask-Testing'],

classifiers=[
Expand Down
23 changes: 4 additions & 19 deletions test/test_all.py
Expand Up @@ -85,20 +85,6 @@ def tearDown(self):

db.drop_all()

def test_flask_fail(self):
# XXX This fails due to a bug in Flask-SQLAlchemy that affects
# Flask-WhooshAlchemy. I submitted a pull request with a fix that is
# pending.

from flask.ext.sqlalchemy import before_models_committed, models_committed

before_models_committed.connect(_after_flush)
models_committed.connect(_after_flush)
db.session.add(ObjectB(title=u'my title', content=u'hello world'))
db.session.add(ObjectA(title=u'a title', content=u'hello world'))
db.session.flush()
db.session.commit()

def test_all(self):
title1 = u'a slightly long title'
title2 = u'another title'
Expand All @@ -123,11 +109,6 @@ def test_all(self):
db.session.add(ObjectB(title=u'my title', content=u'hello world'))
db.session.commit()

db.session.add(ObjectC(title=u'my title', content=u'hello world'))
self.assertRaises(AttributeError, db.session.commit)
db.session.rollback()


# make sure does not interfere with ObjectA's results
self.assertEqual(len(list(ObjectA.query.whoosh_search(u'what'))), 0)
self.assertEqual(len(list(ObjectA.query.whoosh_search(u'title'))), 1)
Expand Down Expand Up @@ -275,6 +256,10 @@ def test_all(self):
# self.assertEqual(len(old), 1)
# self.assertEqual(old[0].title, a.title)

def test_invalid_attribute(self):
db.session.add(ObjectC(title=u'my title', content=u'hello world'))
self.assertRaises(AttributeError, db.session.commit)


if __name__ == '__main__':
import unittest
Expand Down

0 comments on commit 461a201

Please sign in to comment.