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 | """Helpers for showing alerts at the top of the page. |
| 7 | |
| 8 | These alerts are then displayed by alerts.ezt. |
| 9 | """ |
| 10 | from __future__ import print_function |
| 11 | from __future__ import division |
| 12 | from __future__ import absolute_import |
| 13 | |
| 14 | import time |
| 15 | |
| 16 | import ezt |
| 17 | |
| 18 | # Expiration time for special features of timestamped links. |
| 19 | # This is not for security, just for informational messages that |
| 20 | # make sense in the context of a user session, but that should |
| 21 | # not appear days later if the user follows a bookmarked link. |
| 22 | _LINK_EXPIRATION_SEC = 8 |
| 23 | |
| 24 | |
| 25 | class AlertsView(object): |
| 26 | """EZT object for showing alerts at the top of the page.""" |
| 27 | |
| 28 | def __init__(self, mr): |
| 29 | # Used to show message confirming item was updated |
| 30 | self.updated = mr.GetIntParam('updated') |
| 31 | |
| 32 | # Used to show message confirming item was moved and the location of the new |
| 33 | # item. |
| 34 | self.moved_to_project = mr.GetParam('moved_to_project') |
| 35 | self.moved_to_id = mr.GetIntParam('moved_to_id') |
| 36 | self.moved = self.moved_to_project and self.moved_to_id |
| 37 | |
| 38 | # Used to show message confirming item was copied and the location of the |
| 39 | # new item. |
| 40 | self.copied_from_id = mr.GetIntParam('copied_from_id') |
| 41 | self.copied_to_project = mr.GetParam('copied_to_project') |
| 42 | self.copied_to_id = mr.GetIntParam('copied_to_id') |
| 43 | self.copied = self.copied_to_project and self.copied_to_id |
| 44 | |
| 45 | # Used to show message confirming items deleted |
| 46 | self.deleted = mr.GetParam('deleted') |
| 47 | |
| 48 | # If present, we will show message confirming that data was saved |
| 49 | self.saved = mr.GetParam('saved') |
| 50 | |
| 51 | link_generation_timestamp = mr.GetIntParam('ts', default_value=0) |
| 52 | now = int(time.time()) |
| 53 | ts_links_are_valid = now - link_generation_timestamp < _LINK_EXPIRATION_SEC |
| 54 | |
| 55 | show_alert = ts_links_are_valid and ( |
| 56 | self.updated or self.moved or self.copied or self.deleted or self.saved) |
| 57 | self.show = ezt.boolean(show_alert) |