blob: b74cf73619dfb5a0b39d2758a4c1e607fc807f86 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2016 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Utility functions and classes for dealing with saved queries.
6
7Saved queries can be part of the project issue config, where they are
8called "canned queries". Or, they can be personal saved queries that
9may appear in the search scope drop-down, on the user's dashboard, or
10in the user's subscription.
11"""
12from __future__ import print_function
13from __future__ import division
14from __future__ import absolute_import
15
16import logging
17import re
18
19from framework import template_helpers
20from tracker import tracker_bizobj
21from tracker import tracker_constants
22
23
24MAX_QUERIES = 100
25
26
27def ParseSavedQueries(cnxn, post_data, project_service, prefix=''):
28 """Parse form data for the Saved Queries part of an admin form."""
29 saved_queries = []
30 for i in range(1, MAX_QUERIES + 1):
31 if ('%ssavedquery_name_%s' % (prefix, i)) not in post_data:
32 continue # skip any entries that are blank or have no predicate.
33
34 name = post_data['%ssavedquery_name_%s' % (prefix, i)].strip()
35 if not name:
36 continue # skip any blank entries
37
38 if '%ssavedquery_id_%s' % (prefix, i) in post_data:
39 query_id = int(post_data['%ssavedquery_id_%s' % (prefix, i)])
40 else:
41 query_id = None # a new query_id will be generated by the DB.
42
43 project_names_str = post_data.get(
44 '%ssavedquery_projects_%s' % (prefix, i), '')
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010045 project_names = [
46 pn.strip().lower()
47 for pn in re.split(r'[],;\s]+', project_names_str)
48 if pn.strip()
49 ]
Copybara854996b2021-09-07 19:36:02 +000050 project_ids = list(project_service.LookupProjectIDs(
51 cnxn, project_names).values())
52
53 base_id = int(post_data['%ssavedquery_base_%s' % (prefix, i)])
54 query = post_data['%ssavedquery_query_%s' % (prefix, i)].strip()
55
56 subscription_mode_field = '%ssavedquery_sub_mode_%s' % (prefix, i)
57 if subscription_mode_field in post_data:
58 subscription_mode = post_data[subscription_mode_field].strip()
59 else:
60 subscription_mode = None
61
62 saved_queries.append(tracker_bizobj.MakeSavedQuery(
63 query_id, name, base_id, query, subscription_mode=subscription_mode,
64 executes_in_project_ids=project_ids))
65
66 return saved_queries
67
68
69class SavedQueryView(template_helpers.PBProxy):
70 """Wrapper class that makes it easier to display SavedQuery via EZT."""
71
72 def __init__(self, sq, idx, cnxn, project_service):
73 """Store relevant values for later display by EZT.
74
75 Args:
76 sq: A SavedQuery protocol buffer.
77 idx: Int index of this saved query in the list.
78 cnxn: connection to SQL database.
79 project_service: persistence layer for project data.
80 """
81 super(SavedQueryView, self).__init__(sq)
82
83 self.idx = idx
84 base_query_name = 'All issues'
85 for canned in tracker_constants.DEFAULT_CANNED_QUERIES:
86 qid, name, _base_id, _query = canned
87 if qid == sq.base_query_id:
88 base_query_name = name
89
90 if cnxn:
91 project_names = sorted(project_service.LookupProjectNames(
92 cnxn, sq.executes_in_project_ids).values())
93 self.projects = ', '.join(project_names)
94 else:
95 self.projects = ''
96
97 self.docstring = '[%s] %s' % (base_query_name, sq.query)
98
99
100def SavedQueryToCond(saved_query):
101 """Convert a SavedQuery PB to a user query condition string."""
102 if saved_query is None:
103 return ''
104
105 base_cond = tracker_bizobj.GetBuiltInQuery(saved_query.base_query_id)
106 cond = '%s %s' % (base_cond, saved_query.query)
107 return cond.strip()
108
109
110def SavedQueryIDToCond(cnxn, features_service, query_id):
111 """Convert a can/query ID to a user query condition string."""
112 built_in = tracker_bizobj.GetBuiltInQuery(query_id)
113 if built_in:
114 return built_in
115
116 saved_query = features_service.GetSavedQuery(cnxn, query_id)
117 return SavedQueryToCond(saved_query)