hotwire-django / turbo-django

Unmaintained // An early stage integration of Hotwire Turbo with Django
https://discuss.hotwire.dev/t/django-backend-support-for-hotwire/1570/
Other
385 stars 20 forks source link

"nested" Streams app name breaks turbo? #54

Closed nerdoc closed 2 years ago

nerdoc commented 2 years ago

I am evaluating turbo-django for a project, am struggling myself throught the tutorial and creating some of the ideas in my project.

I added a (empty) Stream class named PrescriptionRequestStream, and included {% turbo_subscribe 'prescriptions:PrescriptionRequestStream' %} into my template.

Now Django complains:

TemplateSyntaxError at /dashboard/requests

Could not fetch stream with name: 'prescriptions:PrescriptionRequestStream'  
Registered streams: ['medux_online.plugins.prescriptions:PrescriptionRequestStream']

It's a very brilliant idea to list the available streams in the error message, cool to debug. BUT: I copy'n'pasted medux_online.plugins.prescriptions:PrescriptionRequestStream into the templatetag. And I get the same error for that:

Could not fetch stream with name: 'medux_online.plugins.prescriptions:PrescriptionRequestStream'  
Registered streams: ['medux_online.plugins.prescriptions:PrescriptionRequestStream']

According to stream_for_stream_name(), there is a hint that it should be >>> stream_for_stream_name("app.RegularStream") - meaning the namespace before the class is the app, and it is not a dotted path.

But then the app name generation in Turbo is done wrong when apps are not in the first level of Django's directory tree.

The problem seems to be in the autodiscover_streams() method: app_name = broadcast_module.__package__ uses a dotted path as the app name.

I replaced it with app_name = broadcast_module.__package__.split(".")[-1]and it worked instantly.

This may not be the best approach, as it's just a first glance into the code of turbo-django - maybe you have a better idea and the bigger picture.

scuml commented 2 years ago

That looks like the right fix. I added it to the latest branch which is getting merged into master now. Will have a new tag with this fix in shortly.

Thanks for the comprehensive report and digging in to find the fix.

scuml commented 2 years ago

turbo-django (0.4.0) released with this fix.