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 |
| 12 | import urllib |
| 13 | |
| 14 | import ezt |
| 15 | |
| 16 | from businesslogic import work_env |
| 17 | from framework import framework_helpers |
| 18 | from framework import permissions |
| 19 | from framework import servlet |
| 20 | from framework import template_helpers |
| 21 | from framework import urls |
| 22 | |
| 23 | |
| 24 | class UserSettings(servlet.Servlet): |
| 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 |