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 | """Classes for the user settings (preferences) page.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import time |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 12 | from six.moves import urllib |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 13 | |
| 14 | import ezt |
| 15 | |
| 16 | from businesslogic import work_env |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 17 | from framework import flaskservlet |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 18 | from framework import framework_helpers |
| 19 | from framework import permissions |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 20 | from framework import template_helpers |
| 21 | from framework import urls |
| 22 | |
| 23 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 24 | class UserSettings(flaskservlet.FlaskServlet): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 25 | """Shows a page with a simple form to edit user preferences.""" |
| 26 | |
| 27 | _PAGE_TEMPLATE = 'sitewide/user-settings-page.ezt' |
| 28 | |
| 29 | def AssertBasePermission(self, mr): |
| 30 | """Assert that the user has the permissions needed to view this page.""" |
| 31 | super(UserSettings, self).AssertBasePermission(mr) |
| 32 | |
| 33 | if not mr.auth.user_id: |
| 34 | raise permissions.PermissionException( |
| 35 | 'Anonymous users are not allowed to edit user settings') |
| 36 | |
| 37 | def GatherPageData(self, mr): |
| 38 | """Build up a dictionary of data values to use when rendering the page.""" |
| 39 | page_data = { |
| 40 | 'user_tab_mode': 'st3', |
| 41 | 'logged_in_user_pb': template_helpers.PBProxy(mr.auth.user_pb), |
| 42 | # When on /hosting/settings, the logged-in user is the viewed user. |
| 43 | 'viewed_user': mr.auth.user_view, |
| 44 | 'offer_saved_queries_subtab': ezt.boolean(True), |
| 45 | 'viewing_self': ezt.boolean(True), |
| 46 | } |
| 47 | with work_env.WorkEnv(mr, self.services) as we: |
| 48 | settings_user_prefs = we.GetUserPrefs(mr.auth.user_id) |
| 49 | page_data.update( |
| 50 | framework_helpers.UserSettings.GatherUnifiedSettingsPageData( |
| 51 | mr.auth.user_id, mr.auth.user_view, mr.auth.user_pb, |
| 52 | settings_user_prefs)) |
| 53 | return page_data |
| 54 | |
| 55 | def ProcessFormData(self, mr, post_data): |
| 56 | """Process the posted form.""" |
| 57 | with work_env.WorkEnv(mr, self.services) as we: |
| 58 | framework_helpers.UserSettings.ProcessSettingsForm( |
| 59 | we, post_data, mr.auth.user_pb) |
| 60 | |
| 61 | url = framework_helpers.FormatAbsoluteURL( |
| 62 | mr, urls.USER_SETTINGS, include_project=False, |
| 63 | saved=1, ts=int(time.time())) |
| 64 | |
| 65 | return url |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 66 | |
| 67 | # pylint: disable=unused-argument |
| 68 | def GetUserSetting(self, **kwargs): |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 69 | return self.handler(**kwargs) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 70 | |
| 71 | # pylint: disable=unused-argument |
| 72 | def PostUserSetting(self, **kwargs): |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 73 | return self.handler(**kwargs) |