예제 #1
0
 def test__notification_can_be_updated(self):
     notification = factory.make_Notification()
     user = factory.make_User()
     data = {
         "ident":
         factory.make_name("ident"),
         "user":
         str(user.id),
         "users":
         "false" if notification.users else "true",
         "admins":
         "false" if notification.admins else "true",
         "message":
         factory.make_name("message"),
         "context":
         json.dumps({factory.make_name("key"): factory.make_name("value")}),
         "category":
         random.choice(
             [c for c in categories if c != notification.category]),
     }
     form = NotificationForm(instance=notification, data=data)
     self.assertTrue(form.is_valid(), form.errors)
     notification_saved = form.save()
     self.assertThat(notification_saved, Equals(notification))
     expected = dict(
         data,
         user=user,
         users=(data["users"] == "true"),
         admins=(data["admins"] == "true"),
         context=json.loads(data["context"]),
     )
     self.assertThat(notification_saved,
                     MatchesStructure.byEquality(**expected))
예제 #2
0
 def test__notification_can_be_created_with_all_fields(self):
     user = factory.make_User()
     data = {
         "ident":
         factory.make_name("ident"),
         "user":
         str(user.id),
         "users":
         random.choice(["true", "false"]),
         "admins":
         random.choice(["true", "false"]),
         "message":
         factory.make_name("message"),
         "context":
         json.dumps({factory.make_name("key"): factory.make_name("value")}),
         "category":
         random.choice(categories),
     }
     form = NotificationForm(data)
     self.assertTrue(form.is_valid(), form.errors)
     notification = form.save()
     expected = dict(
         data,
         user=user,
         users=(data["users"] == "true"),
         admins=(data["admins"] == "true"),
         context=json.loads(data["context"]),
     )
     self.assertThat(notification, MatchesStructure.byEquality(**expected))
예제 #3
0
 def test__notification_can_be_created_with_empty_fields(self):
     notification_message = factory.make_name("message")
     form = NotificationForm({
         "ident": "",
         "user": "",
         "users": "",
         "admins": "",
         "message": notification_message,
         "context": "",
         "category": "",
     })
     self.assertTrue(form.is_valid(), form.errors)
     notification = form.save()
     self.assertThat(
         notification,
         MatchesStructure.byEquality(
             ident=None,
             message=notification_message,
             user=None,
             users=False,
             admins=False,
             category="info",
             context={},
         ),
     )
예제 #4
0
    def create(self, request):
        """Create a notification.

        This is available to admins *only*.

        :param message: The message for this notification. May contain basic
            HTML; this will be sanitised before display.
        :param context: Optional JSON context. The root object *must* be an
            object (i.e. a mapping). The values herein can be referenced by
            `message` with Python's "format" (not %) codes.
        :param category: Optional category. Choose from: error, warning,
            success, or info. Defaults to info.

        :param ident: Optional unique identifier for this notification.
        :param user: Optional user ID this notification is intended for. By
            default it will not be targeted to any individual user.
        :param users: Optional boolean, true to notify all users, defaults to
            false, i.e. not targeted to all users.
        :param admins: Optional boolean, true to notify all admins, defaults to
            false, i.e. not targeted to all admins.

        Note: if neither user nor users nor admins is set, the notification
        will not be seen by anyone.
        """
        form = NotificationForm(data=request.data)
        if form.is_valid():
            return form.save()
        else:
            raise MAASAPIValidationError(form.errors)
예제 #5
0
    def update(self, request, id):
        """@description-title Update a notification
        @description Update a notification with a given id.

        This is available to admins *only*.

        Note: One of the ``user``, ``users`` or ``admins`` parameters must be
        set to True for the notification to be visible to anyone.

        @param (int) "{id}" [required=true] The notification id.

        @param (string) "message" [required=true] The message for this
        notification. May contain basic HTML, such as formatting. This string
        will be sanitised before display so that it doesn't break MAAS HTML.

        @param (string) "context" [required=false] Optional JSON context. The
        root object *must* be an object (i.e. a mapping). The values herein can
        be referenced by ``message`` with Python's "format" (not %) codes.

        @param (string) "category" [required=false] Choose from: ``error``,
        ``warning``, ``success``, or ``info``. Defaults to ``info``.

        @param (string) "ident" [required=false] Unique identifier for this
        notification.

        @param (string) "user" [required=false] User ID this notification is
        intended for. By default it will not be targeted to any individual
        user.

        @param (boolean) "users" [required=false] True to notify all users,
        defaults to false, i.e. not targeted to all users.

        @param (boolean) "admins" [required=false] True to notify all admins,
        defaults to false, i.e. not targeted to all admins.

        @param (boolean) "dismissable" [required=false] True to allow users
        dimissing the notification. Defaults to true.

        @success (http-status-code) "server-success" 200
        @success (json) "success-json" A JSON object containing the updated
        notification object.
        @success-example "success-json" [exkey=notifications-update]
        placeholder text

        @error (http-status-code) "404" 404
        @error (content) "not-found" The requested notification is not found.
        @error-example "not-found"
            Not Found
        """
        notification = get_object_or_404(Notification, id=id)
        form = NotificationForm(data=request.data, instance=notification)
        if form.is_valid():
            return form.save()
        else:
            raise MAASAPIValidationError(form.errors)
예제 #6
0
    def update(self, request, id):
        """Update a specific notification.

        See `NotificationsHandler.create` for field information.
        """
        notification = get_object_or_404(Notification, id=id)
        form = NotificationForm(data=request.data, instance=notification)
        if form.is_valid():
            return form.save()
        else:
            raise MAASAPIValidationError(form.errors)