audit-logs: added *basic* support for NOT query term in audit logs.
marcink -
r1824:fdf0761c default
Not Reviewed
Show More
Add another comment
TODOs: 0 unresolved 0 Resolved
COMMENTS: 0 General 0 Inline
@@ -1837,6 +1837,7
1837 'Example filter terms:\n' +
1837 'Example filter terms:\n' +
1838 ' repository:vcs\n' +
1838 ' repository:vcs\n' +
1839 ' username:marcin\n' +
1839 ' username:marcin\n' +
1840 ' username:(NOT marcin)\n' +
1840 ' action:*push*\n' +
1841 ' action:*push*\n' +
1841 ' ip:127.0.0.1\n' +
1842 ' ip:127.0.0.1\n' +
1842 ' date:20120101\n' +
1843 ' date:20120101\n' +
@@ -23,10 +23,10
23 from whoosh.qparser.default import QueryParser, query
23 from whoosh.qparser.default import QueryParser, query
24 from whoosh.qparser.dateparse import DateParserPlugin
24 from whoosh.qparser.dateparse import DateParserPlugin
25 from whoosh.fields import (TEXT, Schema, DATETIME)
25 from whoosh.fields import (TEXT, Schema, DATETIME)
26 from sqlalchemy.sql.expression import or_, and_, func
26 from sqlalchemy.sql.expression import or_, and_, not_, func
27
27
28 from rhodecode.model.db import UserLog
28 from rhodecode.model.db import UserLog
29 from rhodecode.lib.utils2 import remove_prefix, remove_suffix
29 from rhodecode.lib.utils2 import remove_prefix, remove_suffix, safe_unicode
30
30
31 # JOURNAL SCHEMA used only to generate queries in journal. We use whoosh
31 # JOURNAL SCHEMA used only to generate queries in journal. We use whoosh
32 # querylang to build sql queries and filter journals
32 # querylang to build sql queries and filter journals
@@ -54,7 +54,7
54 if search_term:
54 if search_term:
55 qp = QueryParser('repository', schema=JOURNAL_SCHEMA)
55 qp = QueryParser('repository', schema=JOURNAL_SCHEMA)
56 qp.add_plugin(DateParserPlugin())
56 qp.add_plugin(DateParserPlugin())
57 qry = qp.parse(unicode(search_term))
57 qry = qp.parse(safe_unicode(search_term))
58 log.debug('Filtering using parsed query %r' % qry)
58 log.debug('Filtering using parsed query %r' % qry)
59
59
60 def wildcard_handler(col, wc_term):
60 def wildcard_handler(col, wc_term):
@@ -89,16 +89,27
89 return func.lower(field).startswith(func.lower(val))
89 return func.lower(field).startswith(func.lower(val))
90 elif isinstance(term, query.DateRange):
90 elif isinstance(term, query.DateRange):
91 return and_(field >= val[0], field <= val[1])
91 return and_(field >= val[0], field <= val[1])
92 elif isinstance(term, query.Not):
93 return not_(field == val)
92 return func.lower(field) == func.lower(val)
94 return func.lower(field) == func.lower(val)
93
95
94 if isinstance(qry, (query.And, query.Term, query.Prefix, query.Wildcard,
96 if isinstance(qry, (query.And, query.Not, query.Term, query.Prefix,
95 query.DateRange)):
97 query.Wildcard, query.DateRange)):
96 if not isinstance(qry, query.And):
98 if not isinstance(qry, query.And):
97 qry = [qry]
99 qry = [qry]
100
98 for term in qry:
101 for term in qry:
99 field = term.fieldname
102 if isinstance(term, query.Not):
100 val = (term.text if not isinstance(term, query.DateRange)
103 not_term = [z for z in term.leaves()][0]
101 else [term.startdate, term.enddate])
104 field = not_term.fieldname
105 val = not_term.text
106 elif isinstance(term, query.DateRange):
107 field = term.fieldname
108 val = [term.startdate, term.enddate]
109 else:
110 field = term.fieldname
111 val = term.text
112
102 user_log = user_log.filter(get_filterion(field, val, term))
113 user_log = user_log.filter(get_filterion(field, val, term))
103 elif isinstance(qry, query.Or):
114 elif isinstance(qry, query.Or):
104 filters = []
115 filters = []
Comments 0
You need to be logged in to leave comments. Login now