blob: 163d731865571e8fdab4f46884d4eb8cb4bbfb42 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# 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."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import time
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020012from six.moves import urllib
Copybara854996b2021-09-07 19:36:02 +000013
14import ezt
15
16from businesslogic import work_env
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020017from framework import flaskservlet
Copybara854996b2021-09-07 19:36:02 +000018from framework import framework_helpers
19from framework import permissions
Copybara854996b2021-09-07 19:36:02 +000020from framework import template_helpers
21from framework import urls
22
23
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020024class UserSettings(flaskservlet.FlaskServlet):
Copybara854996b2021-09-07 19:36:02 +000025 """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ínezde942802022-07-15 14:06:55 +020066
67 # pylint: disable=unused-argument
68 def GetUserSetting(self, **kwargs):
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020069 return self.handler(**kwargs)
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020070
71 # pylint: disable=unused-argument
72 def PostUserSetting(self, **kwargs):
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020073 return self.handler(**kwargs)