]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/pgfulltext/handlers.py
Simplify registry logic
[osqa.git] / forum_modules / pgfulltext / handlers.py
1 import re
2 from django.db import connection, transaction
3 from django.db.models import Q
4 from forum.models.question import Question, QuestionManager
5 from forum.models.node import Node
6 from forum.modules import decorate
7
8 word_re = re.compile(r'\w+', re.UNICODE)
9
10 @decorate(QuestionManager.search, needs_origin=False)
11 def question_search(self, keywords):
12     tsquery = " | ".join(word_re.findall(keywords))
13     ilike = keywords + u"%%"
14
15     return '-ranking', self.extra(
16             tables = ['forum_rootnode_doc'],
17             select={
18             'ranking': """
19                                 rank_exact_matches(ts_rank_cd('{0.1, 0.2, 0.8, 1.0}'::float4[], "forum_rootnode_doc"."document", to_tsquery('english', %s), 32))
20                                 """,
21             },
22             where=["""
23                            "forum_rootnode_doc"."node_id" = "forum_node"."id" AND ("forum_rootnode_doc"."document" @@ to_tsquery('english', %s) OR
24                            "forum_node"."title" ILIKE %s)
25                            """],
26             params=[tsquery, ilike],
27             select_params=[tsquery],
28             )
29
30
31 @decorate(Node.delete)
32 def delete(origin, self, *args, **kwargs):
33     cursor = connection.cursor()
34     cursor.execute("DELETE FROM forum_rootnode_doc WHERE node_id = %s" % (self.id))
35     transaction.commit_unless_managed()
36     return origin(self, *args, **kwargs)
37
38