1# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Provides the web interface for setting a warning message for all users.
6
7This should be used by perf sheriffs and speed infra team to set appropriate
8warning messages when parts of our infrastructure are down.
9"""
10
11from dashboard import layered_cache
12from dashboard import request_handler
13from dashboard import utils
14
15_DAYS_TO_SHOW_MESSAGE = 3
16
17
18class SetWarningMessageHandler(request_handler.RequestHandler):
19  """Allows internal users to set and delete warning messages.
20
21  Warning messages are shown on /report page, to warn users of outages and
22  planned maintenance.
23  """
24
25  def get(self):
26    """Renders the UI for setting the warning message."""
27    if self._ShowErrorIfNotLoggedIn():
28      return
29    self.RenderHtml('set_warning_message.html', {
30        'warning_message': layered_cache.Get('warning_message') or '',
31        'warning_bug': layered_cache.Get('warning_bug') or '',
32    })
33
34  def post(self):
35    """Handles a request to set the warning message."""
36    if self._ShowErrorIfNotLoggedIn():
37      return
38    message = self.request.get('warning_message')
39    if not message:
40      layered_cache.Delete('warning_message')
41      layered_cache.Delete('warning_bug')
42      self.RenderHtml('result.html', {'headline': 'Warning message cleared.'})
43    else:
44      results = [{'name': 'Warning message', 'value': message}]
45      layered_cache.Set('warning_message', message, _DAYS_TO_SHOW_MESSAGE)
46      bug = self.request.get('warning_bug')
47      if bug:
48        layered_cache.Set('warning_bug', bug, _DAYS_TO_SHOW_MESSAGE)
49        results.append({'name': 'Bug ID', 'value': bug})
50      else:
51        layered_cache.Delete('warning_bug')
52      self.RenderHtml('result.html', {
53          'headline': 'Warning message set.',
54          'results': results
55      })
56
57  def _ShowErrorIfNotLoggedIn(self):
58    """Shows an error message if not logged in with an internal account.
59
60    Returns:
61      True if error message shown, False otherwise.
62    """
63    if not utils.IsInternalUser():
64      self.RenderHtml('result.html', {
65          'errors': ['Only logged-in internal users can set warnings.']
66      })
67      return True
68    return False
69