From 7182702052ca82e0b020daf0e6889851354a17c9 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Wed, 17 Nov 2010 10:32:45 +0100 Subject: [PATCH] lesmianator in search, django 1.1 compatibility --- apps/catalogue/views.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index 227f40235..38036ab3a 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -340,21 +340,42 @@ def _word_starts_with(name, prefix): return Q(**kwargs) +def _word_starts_with_regexp(prefix): + prefix = _no_diacritics_regexp(unicode_re_escape(prefix)) + return ur"(^|(?<=[^\wąćęłńóśźżĄĆĘŁŃÓŚŹŻ]))%s" % prefix + + def _sqlite_word_starts_with(name, prefix): """ version of _word_starts_with for SQLite SQLite in Django uses Python re module """ kwargs = {} - prefix = _no_diacritics_regexp(unicode_re_escape(prefix)) - kwargs['%s__iregex' % name] = ur"(^|(?<=[^\wąćęłńóśźżĄĆĘŁŃÓŚŹŻ]))%s" % prefix + kwargs['%s__iregex' % name] = _word_starts_with_regexp(prefix) return Q(**kwargs) -if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3': +if hasattr(settings, 'DATABASES'): + if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3': + _word_starts_with = _sqlite_word_starts_with +elif settings.DATABASE_ENGINE == 'sqlite3': _word_starts_with = _sqlite_word_starts_with +class App(): + def __init__(self, name, view): + self.name = name + self._view = view + self.lower = name.lower() + self.category = 'application' + def view(self): + return reverse(*self._view) + +_apps = ( + App(u'Leśmianator', (u'lesmianator', )), + ) + + def _tags_starting_with(prefix, user=None): prefix = prefix.lower() book_stubs = models.BookStub.objects.filter(_word_starts_with('title', prefix)) @@ -365,12 +386,16 @@ def _tags_starting_with(prefix, user=None): tags = tags.filter(~Q(category='book') & (~Q(category='set') | Q(user=user))) else: tags = tags.filter(~Q(category='book') & ~Q(category='set')) - return list(books) + list(tags) + list(book_stubs) + + prefix_regexp = re.compile(_word_starts_with_regexp(prefix)) + return list(books) + list(tags) + list(book_stubs) + [app for app in _apps if prefix_regexp.search(app.lower)] def _get_result_link(match, tag_list): if isinstance(match, models.Book) or isinstance(match, models.BookStub): return match.get_absolute_url() + elif isinstance(match, App): + return match.view() else: return reverse('catalogue.views.tagged_object_list', kwargs={'tags': '/'.join(tag.url_chunk for tag in tag_list + [match])} -- 2.20.1