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