Fixed #128 -- Added support to Python 3.

This commit is contained in:
Jorge C. Leitão 2015-11-14 10:07:28 +01:00
parent 63cfcc1d73
commit cec243cffd
2 changed files with 22 additions and 15 deletions

View file

@ -33,27 +33,26 @@ Requirements
- Python 2.7 or 3.3
- Django 1.6+
- Django-Haystack 2
- Xapian 1.2.13+
- Xapian 1.2.19+
In particular, we build this backend on `Travis`_ using:
- Python 2.7 and 3.3
- Django 1.6, 1.7 and 1.8
- Django-Haystack (master)
- Xapian 1.2.21 and 1.3.3
- Xapian 1.2.19 (in Python 2) and 1.3.3 (in both)
Installation
------------
First you need to install Xapian in your machine.
We recommend installing it on the virtual environment using
`this gist <https://gist.github.com/jleclanche/ea0bc333b20ef6aa749c>`_:
activate the virtual environment and run the script.
First, install Xapian in your machine e.g. with the script provided,
`install_xapian.sh`. Call it after activating the virtual environment to install::
You can test if the installation was successful by running::
source <path>/bin/activate
./install_xapian.sh <version>
python -c "import xapian"
`<version>` must be >=1.3.0 for Python 3 envs. This takes around 10 minutes.
Finally, install Xapian-Haystack by running::

View file

@ -150,7 +150,7 @@ class XHExpandDecider(xapian.ExpandDecider):
Ignore terms related with the content type of objects.
"""
if term.startswith(TERM_PREFIXES['django_ct']):
if term.decode('utf-8').startswith(TERM_PREFIXES['django_ct']):
return False
return True
@ -929,6 +929,7 @@ class XapianSearchBackend(BaseSearchBackend):
`text` -- The text to be highlighted
"""
for term in query:
term = term.decode('utf-8')
for match in re.findall('[^A-Z]+', term): # Ignore field identifiers
match_re = re.compile(match, re.I)
content = match_re.sub('<%s>%s</%s>' % (tag, term, tag), content)
@ -962,7 +963,14 @@ class XapianSearchBackend(BaseSearchBackend):
facet_dict[field_name] = []
for facet in list(spy.values()):
facet_dict[field_name].append((_from_xapian_value(facet.term, field_type),
if field_type == 'float':
# the float term is a Xapian serialized object, which is
# in bytes.
term = facet.term
else:
term = facet.term.decode('utf-8')
facet_dict[field_name].append((_from_xapian_value(term, field_type),
facet.termfreq))
return facet_dict
@ -1030,7 +1038,7 @@ class XapianSearchBackend(BaseSearchBackend):
else:
next = previous.replace(
month=((month + gap_value) % 12),
year=(year + (month + gap_value) / 12)
year=(year + (month + gap_value) // 12)
)
elif gap_type == 'day':
next = previous + datetime.timedelta(days=gap_value)
@ -1120,14 +1128,14 @@ class XapianSearchBackend(BaseSearchBackend):
"""
if spelling_query:
if ' ' in spelling_query:
return ' '.join([database.get_spelling_suggestion(term) for term in spelling_query.split()])
return ' '.join([database.get_spelling_suggestion(term).decode('utf-8') for term in spelling_query.split()])
else:
return database.get_spelling_suggestion(spelling_query)
return database.get_spelling_suggestion(spelling_query).decode('utf-8')
term_set = set()
for term in query:
for match in re.findall('[^A-Z]+', term): # Ignore field identifiers
term_set.add(database.get_spelling_suggestion(match))
for match in re.findall('[^A-Z]+', term.decode('utf-8')): # Ignore field identifiers
term_set.add(database.get_spelling_suggestion(match).decode('utf-8'))
return ' '.join(term_set)