blob: fb99fcf947d5b1cd2958b0b97d0c2cd8f66c7310 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Page for showing a user's saved queries and subscription options."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import logging
12import time
13
14import ezt
15
16from features import savedqueries_helpers
17from framework import framework_helpers
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020018from framework import flaskservlet
Copybara854996b2021-09-07 19:36:02 +000019from framework import permissions
20from framework import servlet
21from framework import urls
22
23
24class SavedQueries(servlet.Servlet):
25 """A page class that shows the user's saved queries."""
26
27 _PAGE_TEMPLATE = 'features/saved-queries-page.ezt'
28
29 def AssertBasePermission(self, mr):
30 super(SavedQueries, self).AssertBasePermission(mr)
31 viewing_self = mr.viewed_user_auth.user_id == mr.auth.user_id
32 if not mr.auth.user_pb.is_site_admin and not viewing_self:
33 raise permissions.PermissionException(
34 'User not allowed to edit this user\'s saved queries')
35
36 def GatherPageData(self, mr):
37 """Build up a dictionary of data values to use when rendering the page."""
38 saved_queries = self.services.features.GetSavedQueriesByUserID(
39 mr.cnxn, mr.viewed_user_auth.user_id)
40 saved_query_views = [
41 savedqueries_helpers.SavedQueryView(
42 sq, idx + 1, mr.cnxn, self.services.project)
43 for idx, sq in enumerate(saved_queries)]
44
45 page_data = {
46 'canned_queries': saved_query_views,
47 'new_query_indexes': (
48 list(range(len(saved_queries) + 1,
49 savedqueries_helpers.MAX_QUERIES + 1))),
50 'max_queries': savedqueries_helpers.MAX_QUERIES,
51 'user_tab_mode': 'st4',
52 'viewing_user_page': ezt.boolean(True),
53 }
54 return page_data
55
56 def ProcessFormData(self, mr, post_data):
57 """Validate and store the contents of the issues tracker admin page.
58
59 Args:
60 mr: commonly used info parsed from the request.
61 post_data: HTML form data from the request.
62
63 Returns:
64 String URL to redirect the user to, or None if response was already sent.
65 """
66 existing_queries = savedqueries_helpers.ParseSavedQueries(
67 mr.cnxn, post_data, self.services.project)
68 added_queries = savedqueries_helpers.ParseSavedQueries(
69 mr.cnxn, post_data, self.services.project, prefix='new_')
70 saved_queries = existing_queries + added_queries
71
72 self.services.features.UpdateUserSavedQueries(
73 mr.cnxn, mr.viewed_user_auth.user_id, saved_queries)
74
75 return framework_helpers.FormatAbsoluteURL(
76 mr, '/u/%s%s' % (mr.viewed_username, urls.SAVED_QUERIES),
77 include_project=False, saved=1, ts=int(time.time()))
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020078
79 # def GetSavedQueriesPage(self, **kwargs):
80 # return self.handler(**kwargs)
81
82 # def PostSavedQueriesPage(self, **kwargs):
83 # return self.handler(**kwargs)