jonathan-s / django-sockpuppet

Build reactive applications with the django tooling you already know and love.
https://github.com/jonathan-s/django-sockpuppet
MIT License
450 stars 22 forks source link

Django message framework not usable from Reflex #101

Closed nerdoc closed 3 years ago

nerdoc commented 3 years ago

Bug Report

Describe the bug

When adding a message using Djangos (per default enabled) message framework, e.g.

message.error(self.request, "Value could not be solved")

The Django message framework must be installed properly of course. The Reflex class produces an Error (visible on the server output):

MessageFailure: You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware'

To Reproduce

in a reflex, add that to a stimulus-called Reflex method and trigger the reflex from the frontend.:

from django.contrib import messages

class FooReflex(Reflex):
    def foo(self):
        message.error(self.request, "test message")

Expected behavior

the message should be processed and displayed normally. However, the user must take care that the message part is within the rendered HTML part, to be shown instantly. I know this adds some overhead (which is unwanted). Maybe it could be made optional.

A completely (and maybe better) other approach would be a working example of a push notification using a Reflex using Sockpuppet.

Versions

Django-sockpuppet

External tools

jonathan-s commented 3 years ago

Yup, I'm aware of this. Since the reflex is as a matter of fact happening outside of the response / request cycle I'm not sure there is a good solution for this if you specifically want to use django's messages. The message is fetched when the next response is loaded.

At the same time you should be able to modify the html any way you want to, so that it works as a notification. So perhaps all that is needed is an example of how that would look like?

nerdoc commented 3 years ago

Definitely. A working example is ok, the messaging framework is not even mandatory in Django. but could be removed with three lines. AFAIK, Sockpuppet is able to do Push notofications. The best example would be something like a long running task which is finished, and then the user gets a message. For the example this could be shown by hooking onto a Django event? iIs this possible?

zodman commented 3 years ago

@nerdoc simple solution communicates with redis/memcached ... create a messages cache like

on the reflex: cache.set(f"msg_{user}", [message1])

on the view:

messages = cache.get(f"msg_{user}")
for message  in messages:
message.info(self.request, message)

take a look the chat example:

https://django-sockpuppet-expo.python3.ninja/chat/

nerdoc commented 3 years ago

Cool, thanks! That's fine for me I think.