Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # 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. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Page for showing a user's saved queries and subscription options.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import logging |
| 11 | import time |
| 12 | |
| 13 | import ezt |
| 14 | |
| 15 | from features import savedqueries_helpers |
| 16 | from framework import framework_helpers |
| 17 | from framework import permissions |
| 18 | from framework import servlet |
| 19 | from framework import urls |
| 20 | |
| 21 | |
| 22 | class 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ínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 76 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 77 | def GetSavedQueriesPage(self, **kwargs): |
| 78 | return self.handler(**kwargs) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 79 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 80 | def PostSavedQueriesPage(self, **kwargs): |
| 81 | return self.handler(**kwargs) |