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 |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 18 | from framework import flaskservlet |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 19 | from framework import permissions |
| 20 | from framework import servlet |
| 21 | from framework import urls |
| 22 | |
| 23 | |
| 24 | class 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ínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 78 | |
| 79 | # def GetSavedQueriesPage(self, **kwargs): |
| 80 | # return self.handler(**kwargs) |
| 81 | |
| 82 | # def PostSavedQueriesPage(self, **kwargs): |
| 83 | # return self.handler(**kwargs) |