def SetUpUpdateSavedQueries(self, commit=True): query1 = tracker_bizobj.MakeSavedQuery(1, 'query1', 100, 'owner:me') query2 = tracker_bizobj.MakeSavedQuery(None, 'query2', 100, 'status:New') saved_queries = [query1, query2] savedquery_rows = [ (sq.query_id or None, sq.name, sq.base_query_id, sq.query) for sq in saved_queries] self.features_service.savedquery_tbl.Delete( self.cnxn, id=[1], commit=commit) self.features_service.savedquery_tbl.InsertRows( self.cnxn, features_svc.SAVEDQUERY_COLS, savedquery_rows, commit=commit, return_generated_ids=True).AndReturn([11, 12]) return saved_queries
def testMakeSavedQuery(self): sq = tracker_bizobj.MakeSavedQuery( 100, 'my query', 2, 'priority:high', subscription_mode='immediate', executes_in_project_ids=[789]) self.assertEqual(100, sq.query_id) self.assertEqual('immediate', sq.subscription_mode) self.assertEqual([789], sq.executes_in_project_ids)
def testNotifyBulkChangeTask_SubscriberGetsEmail(self): """If a user subscription matches the issue, notify that user.""" task = notify.NotifyBulkChangeTask(request=None, response=None, services=self.services) params = { 'send_email': 1, 'issue_ids': '%d' % (self.issue1.issue_id), 'seq': 0, 'old_owner_ids': '1', 'commenter_id': 1 } mr = testing_helpers.MakeMonorailRequest(user_info={'user_id': 1}, params=params, method='POST', services=self.services) self.services.user.TestAddUser('*****@*****.**', 4) sq = tracker_bizobj.MakeSavedQuery( 1, 'all open issues', 2, '', subscription_mode='immediate', executes_in_project_ids=[self.issue1.project_id]) self.services.features.UpdateUserSavedQueries('cnxn', 4, [sq]) result = task.HandleRequest(mr) self.VerifyParams(result, params) tasks = self.taskqueue_stub.get_filtered_tasks( url=urls.OUTBOUND_EMAIL_TASK + '.do') self.assertEqual(2, len(tasks))
def testComputeGroupReasonList_Subscribers(self): """Bob subscribed.""" sq = tracker_bizobj.MakeSavedQuery( 1, 'freds issues', 1, 'owner:[email protected]', subscription_mode='immediate', executes_in_project_ids=[789]) self.services.features.UpdateUserSavedQueries( 'cnxn', self.bob.user_id, [sq]) actual = notify_reasons.ComputeGroupReasonList( 'cnxn', self.services, self.project, self.issue, self.config, self.users_by_id, [], True) self.CheckGroupReasonList( actual, owner_apl=[notify_reasons.AddrPerm( False, self.fred.email, self.fred, REPLY_NOT_ALLOWED)], subscriber_apl=[notify_reasons.AddrPerm( False, self.bob.email, self.bob, REPLY_NOT_ALLOWED)]) # Now with subscriber notifications disabled. actual = notify_reasons.ComputeGroupReasonList( 'cnxn', self.services, self.project, self.issue, self.config, self.users_by_id, [], True, include_subscribers=False) self.CheckGroupReasonList( actual, owner_apl=[notify_reasons.AddrPerm( False, self.fred.email, self.fred, REPLY_NOT_ALLOWED)])
def _GetUsersSavedQueriesDict(self, cnxn, user_ids): """Return a dict of all SavedQuery PBs for the specified users.""" results_dict, missed_uids = self.saved_query_cache.GetAll(user_ids) if missed_uids: savedquery_rows = self.user2savedquery_tbl.Select( cnxn, cols=SAVEDQUERY_COLS + ['user_id', 'subscription_mode'], left_joins=[('SavedQuery ON query_id = id', [])], order_by=[('rank', [])], user_id=missed_uids) sqeip_rows = self.savedqueryexecutesinproject_tbl.Select( cnxn, cols=SAVEDQUERYEXECUTESINPROJECT_COLS, query_id={row[0] for row in savedquery_rows}) sqeip_dict = {} for qid, pid in sqeip_rows: sqeip_dict.setdefault(qid, []).append(pid) for saved_query_tuple in savedquery_rows: query_id, name, base_id, query, uid, sub_mode = saved_query_tuple sq = tracker_bizobj.MakeSavedQuery( query_id, name, base_id, query, subscription_mode=sub_mode, executes_in_project_ids=sqeip_dict.get(query_id, [])) results_dict.setdefault(uid, []).append(sq) self.saved_query_cache.CacheAll(results_dict) return results_dict
def SetUpUsersSavedQueries(self): query = tracker_bizobj.MakeSavedQuery(1, 'query1', 100, 'owner:me') self.features_service.saved_query_cache.CacheItem(1, [query]) self.features_service.user2savedquery_tbl.Select( self.cnxn, cols=features_svc.SAVEDQUERY_COLS + ['user_id', 'subscription_mode'], left_joins=[('SavedQuery ON query_id = id', [])], order_by=[('rank', [])], user_id=[2]).AndReturn( [(2, 'query2', 100, 'status:New', 2, 'Sub_Mode')]) self.features_service.savedqueryexecutesinproject_tbl.Select( self.cnxn, cols=features_svc.SAVEDQUERYEXECUTESINPROJECT_COLS, query_id=set([2])).AndReturn([(2, 12345)])
def GetCannedQueriesForProjects(self, cnxn, project_ids): """Return a dict {project_id: [saved_query]} for the specified projects.""" # TODO(jrobbins): caching cannedquery_rows = self.project2savedquery_tbl.Select( cnxn, cols=['project_id'] + SAVEDQUERY_COLS, left_joins=[('SavedQuery ON query_id = id', [])], order_by=[('rank', [])], project_id=project_ids) result_dict = collections.defaultdict(list) for cq_row in cannedquery_rows: project_id = cq_row[0] canned_query_tuple = cq_row[1:] result_dict[project_id].append( tracker_bizobj.MakeSavedQuery(*canned_query_tuple)) return result_dict
def GetSavedQueries(self, cnxn, query_ids): """Retrieve the specified SaveQuery PBs.""" # TODO(jrobbins): RAM cache saved_queries = {} savedquery_rows = self.savedquery_tbl.Select(cnxn, cols=SAVEDQUERY_COLS, id=query_ids) for saved_query_tuple in savedquery_rows: qid, name, base_id, query = saved_query_tuple saved_queries[qid] = tracker_bizobj.MakeSavedQuery( qid, name, base_id, query) sqeip_rows = self.savedqueryexecutesinproject_tbl.Select( cnxn, cols=SAVEDQUERYEXECUTESINPROJECT_COLS, query_id=query_ids) for query_id, project_id in sqeip_rows: saved_queries[query_id].executes_in_project_ids.append(project_id) return saved_queries
def ParseSavedQueries(cnxn, post_data, project_service, prefix=''): """Parse form data for the Saved Queries part of an admin form.""" saved_queries = [] for i in range(1, MAX_QUERIES + 1): if ('%ssavedquery_name_%s' % (prefix, i)) not in post_data: continue # skip any entries that are blank or have no predicate. name = post_data['%ssavedquery_name_%s' % (prefix, i)].strip() if not name: continue # skip any blank entries if '%ssavedquery_id_%s' % (prefix, i) in post_data: query_id = int(post_data['%ssavedquery_id_%s' % (prefix, i)]) else: query_id = None # a new query_id will be generated by the DB. project_names_str = post_data.get( '%ssavedquery_projects_%s' % (prefix, i), '') project_names = [ pn.strip().lower() for pn in re.split('[],;\s]+', project_names_str) if pn.strip() ] project_ids = list( project_service.LookupProjectIDs(cnxn, project_names).values()) base_id = int(post_data['%ssavedquery_base_%s' % (prefix, i)]) query = post_data['%ssavedquery_query_%s' % (prefix, i)].strip() subscription_mode_field = '%ssavedquery_sub_mode_%s' % (prefix, i) if subscription_mode_field in post_data: subscription_mode = post_data[subscription_mode_field].strip() else: subscription_mode = None saved_queries.append( tracker_bizobj.MakeSavedQuery(query_id, name, base_id, query, subscription_mode=subscription_mode, executes_in_project_ids=project_ids)) return saved_queries
def testNotifyBulkChangeTask_CCAndSubscriberListsIssueOnce(self): """If a user both CCs and subscribes, include issue only once.""" task = notify.NotifyBulkChangeTask(request=None, response=None, services=self.services) params = { 'send_email': 1, 'issue_ids': '%d' % (self.issue1.issue_id), 'seq': 0, 'old_owner_ids': '1', 'commenter_id': 1 } mr = testing_helpers.MakeMonorailRequest(user_info={'user_id': 1}, params=params, method='POST', services=self.services) self.services.user.TestAddUser('*****@*****.**', 4) self.issue1.cc_ids = [4] sq = tracker_bizobj.MakeSavedQuery( 1, 'all open issues', 2, '', subscription_mode='immediate', executes_in_project_ids=[self.issue1.project_id]) self.services.features.UpdateUserSavedQueries('cnxn', 4, [sq]) result = task.HandleRequest(mr) self.VerifyParams(result, params) tasks = self.taskqueue_stub.get_filtered_tasks( url=urls.OUTBOUND_EMAIL_TASK + '.do') self.assertEqual(2, len(tasks)) found = False for task in tasks: task_params = json.loads(task.payload) if task_params['to'] == '*****@*****.**': found = True body = task_params['body'] self.assertEqual(1, body.count('Issue %d' % self.issue1.local_id)) self.assertTrue(found)
def testMakeSavedQuery_WithNone(self): sq = tracker_bizobj.MakeSavedQuery( None, 'my query', 2, 'priority:high') self.assertEqual(None, sq.query_id) self.assertEqual(None, sq.subscription_mode) self.assertEqual([], sq.executes_in_project_ids)