blob: 67c8cea608b66ac01e480eae04747d33da9a3aef [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Classes for the user settings (preferences) page."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import time
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020011from six.moves import urllib
Copybara854996b2021-09-07 19:36:02 +000012
13import ezt
14
15from businesslogic import work_env
16from framework import framework_helpers
17from framework import permissions
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010018from framework import servlet
Copybara854996b2021-09-07 19:36:02 +000019from framework import template_helpers
20from framework import urls
21
22
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010023class UserSettings(servlet.Servlet):
Copybara854996b2021-09-07 19:36:02 +000024 """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ínezde942802022-07-15 14:06:55 +020065
66 # pylint: disable=unused-argument
67 def GetUserSetting(self, **kwargs):
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020068 return self.handler(**kwargs)
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020069
70 # pylint: disable=unused-argument
71 def PostUserSetting(self, **kwargs):
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020072 return self.handler(**kwargs)