]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/pgfulltext/handlers.py
Several improvements in the node bulk management feature. Improved filters and added...
[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 True, 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 def delete_docs(node):
32     cursor = connection.cursor()
33     cursor.execute("DELETE FROM forum_rootnode_doc WHERE node_id = %s" % (node.id))
34
35     for n in node.children.all():
36         delete_docs(n)
37
38
39 #@decorate(Node.delete)
40 def delete(origin, self, *args, **kwargs):
41     delete_docs(self)
42     transaction.commit_unless_managed()
43     origin(self, *args, **kwargs)
44
45