File: notification.py

package info (click to toggle)
freedombox 26.2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 82,976 kB
  • sloc: python: 48,504; javascript: 1,736; xml: 481; makefile: 290; sh: 167; php: 32
file content (375 lines) | stat: -rw-r--r-- 15,481 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Module to provide API for showing notifications."""

import copy
import logging

from django.core.exceptions import ValidationError
from django.db.models import Q
from django.template.exceptions import TemplateDoesNotExist
from django.template.response import SimpleTemplateResponse
from django.utils import timezone
from django.utils.translation import gettext

from plinth import cfg
from plinth.utils import SafeFormatter

from . import db, models

severities = {'exception': 5, 'error': 4, 'warning': 3, 'info': 2, 'debug': 1}
logger = logging.getLogger(__name__)


class Notification(models.StoredNotification):
    """API to create persistent global notifications to users.

    The number of notifications is shown on the top navigation bar of every
    page that the user loads. On clicking the notification icon, a full list of
    notifications is shown as a drop down that works on both on Desktop and
    Mobile interfaces.

    The notification API provides the following functionality.

    * Store the notifications on disk and persist them across restarts.

    * Update a notification for any of its stored details.

    * Show icon and name of the app that shows the notification.

    * Present the notification title and message.

    * Present actions that user can perform on the notification.

    * Allow for full customization of content and actions of the notification.
      This allows for use cases such as showing progress bar. This is done by
      allowing a custom template to be used for rendering.

    * Internationalization and localization of displayed notification.

    * Allow the notification to be dismissed and resurrected after dismissal.

    * Store arbitrary data as a dictionary along with the notification. This
      data is used for formatting localized strings and for rendering the
      custom template.

    * Limit a notification to a group of users or to a single user.

    * Retrieve a notification using specific string identifier.

    * Iterate all the notifications.

    * Filter notifications by app that created them, by whether the
      notification meant for a particular user and by whether a notification is
      dismissed or not.

    * Provide a convenient way to obtain the context for rendering templates
      that intend to show notifications.

    This class is a wrapper over Notification model to provide a simplified
    API. So, all of the Django model API is available on the class. The
    following fields are present in the model:

    'id' is a unique string identifier for the notification and acts as the
    primary key for the stored database table. Only the following chars are
    currently allowed: A-Z, a-z, 0-9, - and =. If other chars must be used, it
    is recommended to use base32 encoding.

    'app_id' is the unique ID of the app showing the notification.

    'severity' is a string indicating the severity of the notification which
    can take one of the following values: 'exception', 'error', 'warning',
    'info' and 'debug'.

    'title' is a user visible string that is the main heading of the
    notification. 'title' may be used to show a summary of notifications even
    'body_template' is used for customization. So, it is mandatory. The user
    who requested the operation that caused the notification may be different
    from the user that sees the notification. Each user wishes to see the
    notification in their own locale. This string is stored to the disk as it
    is. It is translated and then formatted with 'data' dictionary just before
    showing it to the user. So, it must be marked for translation with
    (u)gettext_noop but must not be translated using (u)gettext or similar
    methods.

    'message' is a user visible string that is the main body of the
    notification. It is formatted as a single paragraph. It is not used and
    must not be provided when 'body_template' is provided. It must be marked
    for translation but not translated similar to 'title'.

    'actions' is a list of dictionaries containing information about what
    operations, such as dismiss, that the user may perform on the notification.
    When not provided, no actions are shown and the notification can't be
    dismissed. It is not used and must not be provided when 'body_template' is
    provided. When 'body_template' is provided, the template is expected to
    list its actions inside the template.

    A example of 'actions' structure is as follows: [{'type': 'dismiss'},
    {'type': 'link', 'class': 'warning', 'text': 'Cleanup', 'url':
    'storage:index'}]. The 'type' parameter can be 'dismiss' or 'link'. When
    'dismiss' is the type, no other information is necessary and a simple
    action to dismiss the notification is provided. When the 'type' is 'link',
    a button is shown with label provided by 'text'. The 'text' property is
    must be marked for translation but not translated similar to 'title'. When
    the button is clicked, it will direct the user to the Django URL indicated
    by 'url'. 'class' is used for CSS styling of the button. It can taken on
    any of the following values; 'primary', 'default', 'warning', 'danger',
    'success', 'info'.

    'body_template' is the name of the Django template to use instead of
    'title', 'message' and 'actions'. It enables the the notification creator
    to go beyond the simple presentation offered by default and include rich
    content such as progress bars and forms inside a notification. The template
    itself is expected to contain a meaningful title, body and operations to be
    performed on the notification. It is an error to provide 'body_template'
    and any one of 'message' and 'actions' at the same time. 'title' may still
    be used when notifications are presented in other situations. The template
    is rendered with 'data' as it's context. So, any key/values to be included
    in the context must become part of the 'data'.

    'data' is a custom dictionary that is serialized and stored along with the
    notification. It is has three purposes; to format the translated strings
    before showing to the user, to be used as context for rendering a custom
    template and to store any extra data associated with the notification on
    behalf of the it's creator for future reference. Any string value in the
    dictionary, even in deeper levels, if prefixed by 'translate:' will be
    translated before being used.

    'created_time' is a date/time field that is automatically populated when
    the notification is created.

    'last_update_time' is a date/time field that is automatically modified when
    the notification is updated with newer properties, including changes to the
    'dismissed' property.

    'user' is the username of the account to which this notification should be
    shown to. When not provided or set to None, it means that the notification
    is meant for all the users (subject to 'group' restriction).

    'group' is the group of users to which this notification should be shown
    to. When not provided or set to None, it means that the notification is
    meant for all the users (subject to 'user' restriction). If a user in the
    group dismisses the notification, it is no longer shown to other users. To
    let each user see the notification, create multiple notifications each
    restricted to a single user account instead.

    'dismissed' is a boolean flag that indicates whether the notification has
    been dismissed by the user.

    """

    class Meta:  # pylint: disable=too-few-public-methods
        """Meta properties of the Notification model."""
        proxy = True

    @property
    def severity_value(self):
        """Return severity as a numeric value suitable for comparison."""
        try:
            return severities[self.severity]
        except KeyError:
            return severities['info']

    def dismiss(self, should_dismiss=True):
        """Mark the notification as read or unread.

        If 'should_dismiss' is True, the notification is dismissed else the
        notification is resurrected even after being dismissed.

        """
        self.dismissed = should_dismiss
        with db.lock:
            super().save()

    def clean(self):
        """Perform additional validations on the model."""
        if self.severity not in ('exception', 'error', 'warning', 'info',
                                 'debug'):
            raise ValidationError('Invalid severity')

        if self.actions:
            self._validate_actions(self.actions)

        if (self.message or self.actions) and self.body_template:
            raise ValidationError(
                'Either body_template or message and actions must exist')

    @staticmethod
    def _validate_actions(actions):
        """Check that actions structure is valid."""
        if not isinstance(actions, list):
            raise ValidationError('Actions must be a list')

        for action in actions:
            if not isinstance(action, dict):
                raise ValidationError('Action must a dictionary')

            if 'type' not in action:
                raise ValidationError('Action must have a type')

            if action['type'] not in ('dismiss', 'link'):
                raise ValidationError('Action type must be dismiss or link')

            if action['type'] == 'dismiss':
                continue

            if 'text' not in action or 'url' not in action:
                raise ValidationError('Action must have text and url')

            if 'class' in action and action['class'] not in (
                    'primary', 'default', 'warning', 'danger', 'success',
                    'info'):
                raise ValidationError('Invalid action class')

    @staticmethod
    def update_or_create(**kwargs):
        """Update a notification or create one if necessary.

        'id' field will be used to retrieve the notification and all other
        fields values will be updated after retrieval. If not match is found, a
        new notification is created and returned.

        """
        id = kwargs.pop('id')
        fields_to_update = kwargs.copy()
        fields_to_update['last_update_time'] = timezone.now()
        with db.lock:
            return Notification.objects.update_or_create(
                defaults=fields_to_update, id=id)[0]

    @staticmethod
    def get(key):  # pylint: disable=redefined-builtin
        """Return a notification object with a matching ID."""
        # pylint: disable=no-member
        with db.lock:
            try:
                return Notification.objects.get(pk=key)
            except Notification.DoesNotExist:
                raise KeyError('No such notification')

    @staticmethod
    def list(key=None, app_id=None, user=None, dismissed=False):
        """Return a list of notifications for a user.

        'key' if provided, return only notifications that match this value as
        their 'id'.

        'app_id' if provided, return only notifications that match this
        app_id.

        'user' must be a Django request.user structure. If provided, only
        notifications that are meant for this user account specifically or
        meant for any of groups that this user belongs to will be returned.

        'dismissed' is the a boolean flag or None. If not None, only
        notifications with the matching in their 'dismissed' field will be
        returned.

        """
        filters = []
        if key:
            filters.append(Q(id=key))

        if app_id:
            filters.append(Q(app_id=app_id))

        if user:
            # XXX: Consider implementing caching for user groups
            groups = user.groups.values_list('name', flat=True)
            filters.append(Q(user__isnull=True) | Q(user=user.username))
            filters.append(Q(group__isnull=True) | Q(group__in=groups))

        if dismissed is not None:
            filters.append(Q(dismissed=dismissed))

        with db.lock:
            return Notification.objects.filter(*filters)[0:10]

    @staticmethod
    def _translate(string_, data=None):
        """Translate a string for final display using data dict."""
        if not string_:
            return None

        string_ = gettext(string_)
        try:
            string_ = str(string_)
            if data:
                string_ = SafeFormatter().vformat(string_, [], data)
        except (KeyError, AttributeError) as error:
            logger.warning(
                'Notification missing required key during translation: %s',
                error)

        return string_

    @staticmethod
    def _translate_dict(data_dict, data=None):
        """Translate strings inside a data dict for display."""
        if not data_dict:
            return data_dict

        new_dict = {}
        for key, value in data_dict.items():
            if isinstance(value, str) and value.startswith('translate:'):
                value = value.split(':', maxsplit=1)[1]
                value = Notification._translate(value, data)
            elif isinstance(value, dict):
                value = Notification._translate_dict(value, data)
            else:
                value = copy.deepcopy(value)

            new_dict[key] = value

        return new_dict

    @staticmethod
    def _render(request, template, context):
        """Use the template name and render it."""
        if not template:
            return None

        context = dict(context, box_name=gettext(cfg.box_name),
                       request=request)
        try:
            return SimpleTemplateResponse(template, context).render()
        except TemplateDoesNotExist:
            # Developer only error, no i18n
            return {'content': f'Template {template} does not exist.'.encode()}

    @staticmethod
    def get_display_context(request, user):
        """Return a list of notifications meant for display to a user."""
        notifications = Notification.list(user=user)
        max_severity = max(notifications, default=None,
                           key=lambda note: note.severity_value)
        max_severity = max_severity.severity if max_severity else None

        notes = []
        for note in notifications:
            data = Notification._translate_dict(note.data, note.data)
            actions = copy.deepcopy(note.actions)
            for action in actions:
                if 'text' in action:
                    action['text'] = Notification._translate(
                        action['text'], data)

            note_context = {
                'id': note.id,
                'app_id': note.app_id,
                'severity': note.severity,
                'title': Notification._translate(note.title, data),
                'message': Notification._translate(note.message, data),
                'actions': actions,
                'data': data,
                'created_time': note.created_time,
                'last_update_time': note.last_update_time,
                'user': note.user,
                'group': note.group,
                'dismissed': note.dismissed,
            }
            body = Notification._render(request, note.body_template,
                                        note_context)
            note_context['body'] = body
            notes.append(note_context)

        return {'notifications': notes, 'max_severity': max_severity}