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