blob: 427faa31f815aec6445ca9db443c41a31f43c25e [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"""Page for showing a user's saved queries and subscription options."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import logging
11import time
12
13import ezt
14
15from features import savedqueries_helpers
16from framework import framework_helpers
17from framework import permissions
18from framework import servlet
19from framework import urls
20
21
22class SavedQueries(servlet.Servlet):
23 """A page class that shows the user's saved queries."""
24
25 _PAGE_TEMPLATE = 'features/saved-queries-page.ezt'
26
27 def AssertBasePermission(self, mr):
28 super(SavedQueries, self).AssertBasePermission(mr)
29 viewing_self = mr.viewed_user_auth.user_id == mr.auth.user_id
30 if not mr.auth.user_pb.is_site_admin and not viewing_self:
31 raise permissions.PermissionException(
32 'User not allowed to edit this user\'s saved queries')
33
34 def GatherPageData(self, mr):
35 """Build up a dictionary of data values to use when rendering the page."""
36 saved_queries = self.services.features.GetSavedQueriesByUserID(
37 mr.cnxn, mr.viewed_user_auth.user_id)
38 saved_query_views = [
39 savedqueries_helpers.SavedQueryView(
40 sq, idx + 1, mr.cnxn, self.services.project)
41 for idx, sq in enumerate(saved_queries)]
42
43 page_data = {
44 'canned_queries': saved_query_views,
45 'new_query_indexes': (
46 list(range(len(saved_queries) + 1,
47 savedqueries_helpers.MAX_QUERIES + 1))),
48 'max_queries': savedqueries_helpers.MAX_QUERIES,
49 'user_tab_mode': 'st4',
50 'viewing_user_page': ezt.boolean(True),
51 }
52 return page_data
53
54 def ProcessFormData(self, mr, post_data):
55 """Validate and store the contents of the issues tracker admin page.
56
57 Args:
58 mr: commonly used info parsed from the request.
59 post_data: HTML form data from the request.
60
61 Returns:
62 String URL to redirect the user to, or None if response was already sent.
63 """
64 existing_queries = savedqueries_helpers.ParseSavedQueries(
65 mr.cnxn, post_data, self.services.project)
66 added_queries = savedqueries_helpers.ParseSavedQueries(
67 mr.cnxn, post_data, self.services.project, prefix='new_')
68 saved_queries = existing_queries + added_queries
69
70 self.services.features.UpdateUserSavedQueries(
71 mr.cnxn, mr.viewed_user_auth.user_id, saved_queries)
72
73 return framework_helpers.FormatAbsoluteURL(
74 mr, '/u/%s%s' % (mr.viewed_username, urls.SAVED_QUERIES),
75 include_project=False, saved=1, ts=int(time.time()))
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020076
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010077 def GetSavedQueriesPage(self, **kwargs):
78 return self.handler(**kwargs)
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020079
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010080 def PostSavedQueriesPage(self, **kwargs):
81 return self.handler(**kwargs)