Re: Running polls tutorial, see the error message "The empty path didn't match any of these."

2018-02-25 Thread Chunjing Jia
Thank you Dylan! Now I see what's going on there.

Best,
Chunjing

On Saturday, February 24, 2018 at 4:56:10 PM UTC-8, Chunjing Jia wrote:
>
> Hi,
>
> I am running Django 2.0 tutorial01 for polls. I am seeing this error 
> message 
>
> Using the URLconf defined in mysite.urls, Django tried these URL 
> patterns, in this order:
>
>1. polls/
>2. admin/
>
> The empty path didn't match any of these.
>
> Without adding polls, my code works fine, and I can see the rocket and 
> congratulation page. But after adding polls, I always get this error 
> message. Even if I use the example code that I found from github with the 
> all the correct setting... 
>
> Does this mean that my installation for Python3 and Django 2.0 may be 
> problematic? What other possibles error may be here? 
>
> Thanks a lot!
>
> Best,
> CJ
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c24ad298-b792-4381-afbd-2480349bd29b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels 2.0 - Exception when sending a message to client

2018-02-25 Thread muhammed
Thank you so much for the detailed explanation.

1) I now understand where my error was - the custom channel I used was 
passing events to a background worker that was not related to the websocket 
consumer. This also answers my question about how channel names must be 
included in the runworker command explicitly, similar to celery.

2) To fix my own issue, all I had to do was add the group to the right 
channel (*self.channel_name *in this case). I did read the multichat 
example a few times but missed the reference to the implicit channel name. 

I can now send messages to my front end app perfectly.


Andrew/Ken - Thank you for taking the time to assist and helping me solve 
the issue.

On Sunday, February 25, 2018 at 2:56:45 AM UTC+2, muha...@swordfish.co.za 
wrote:
>
> I'm still trying to find my way around channels 2.0, so I'm not sure if my 
> implementation is incorrect or if this is a valid issue. I'm going to post 
> as much info as possible in the hope that it will assist with finding the 
> problem.
>
> I have a single page app which opens a JS websocket connection - with 
> channels 1 I used to add a *session_key *to the querystring and that used 
> to handle the authentication.
>
> I see this is no longer the case, so I now have a custom middleware class 
> that sets the user object on the scope:
>
> from django.contrib.sessions.models import Session
>
> from users.models import User
>
>
> class QueryAuthMiddleware:
>
> def __init__(self, inner):
> # Store the ASGI application we were passed
> self.inner = inner
>
> def __call__(self, scope):
> # Look up user from query string (you should also do things like
> # check it's a valid user ID, or if scope["user"] is already 
> populated)
> if scope.get("user", None) is None:
> session_obj = 
> Session.objects.get(session_key=scope["query_string"].decode("utf-8").split("=")[1])
> session_decoded = session_obj.get_decoded()
>
> scope["user"] = 
> User.objects.get(id=session_decoded.get("_auth_user_id"))
>
> # Return the inner application directly and let it run everything 
> else
> return self.inner(scope)
>
>
>
>
> This is in turn added to my routing (channels.py):
>
> from django.conf.urls import url
> from django.conf import settings
> from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter
>
> from notifications.consumer import TestWebsocketConsumer, TestConsumer
> from notifications.middleware.query_auth_middleware import QueryAuthMiddleware
>
> ROOT_PATH = "" if settings.DEBUG else "/ws/"
>
>
> application = ProtocolTypeRouter({
>
> "websocket": QueryAuthMiddleware(
> URLRouter([
> url(f"^{ROOT_PATH}(?P[-\w]+)/$", TestWebsocketConsumer),
>
> ])
> ),
>
> "channel": ChannelNameRouter({
>  "user-notifications": TestConsumer,
> })
>
>
> })
>
>
>
>
>
> Here's my *consumers.py*:
>
> from asgiref.sync import async_to_sync
> from channels.consumer import SyncConsumer
> from channels.generic.websocket import WebsocketConsumer
>
>
> class TestWebsocketConsumer(WebsocketConsumer):
> def websocket_connect(self, message):
> 
> async_to_sync(self.channel_layer.group_add)(str(self.scope["user"].id), 
> "user-notifications")
> self.connect()
>
>
> class TestConsumer(SyncConsumer):
> def notification_handler(self, message):
>
> self.send(
> {
>  "type": "websocket.send",
>  "text": message["text"]
> }
> )
>
>
>
>
>
>
> The idea of the app is that each user that logs in on the front end is 
> able to receive messages meant only for them sent by the back end.  I have 
> been trying to test it like this:
>
> >>> channel_layer = get_channel_layer()
> >>> async_to_sync(channel_layer.send)("user-notifications", {"type": 
> >>> "notification.handler", "text": "My Message"})
>
>
>
>
> Here's the traceback in the *runworker* output:
>
> 2018-02-25 02:34:14,002 - INFO - runworker - Running worker for channels 
> ['user-notifications']
> ERROR:root:Exception inside application: You must implement 
> application_send()
>   File 
> "/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/consumer.py",
>  
> line 54, in __call__
> await await_many_dispatch([receive, self.channel_receive], 
> self.dispatch)
>   File 
> "/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/utils.py", 
> line 48, in await_many_dispatch
> await dispatch(result)
>   File 
> "/Users/muhammed/projects/xxx/lib/python3.6/site-packages/asgiref/sync.py", 
> line 110, in __call__
> return await asyncio.wait_for(future, timeout=None)
>   File 
> "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py",
>  
> line 333, in wait_for
> return (yield from fut)
>   File 
> 

Re: [django-channels] Testing events from post_save signal

2018-02-25 Thread Tomáš Ehrlich
Kudos for you and all contributors! It’s really amazing.

I’m going to send few PRs to django-channels as a payback :)

Cheers,
   Tom

> 25. 2. 2018 v 19:46, Andrew Godwin :
> 
> No problem - glad everything else seems to work alright! Getting async 
> testing working well has been a long, hard road :)
> 
> Andrew
> 
> On Sun, Feb 25, 2018 at 10:37 AM, Tomáš Ehrlich  > wrote:
> Of course!
> 
> It works now perfectly, thank you. Sorry I missed that in docs.
> 
> Cheers,
>Tom
> 
>> 25. 2. 2018 v 19:12, Andrew Godwin > >:
>> 
>> I think the change you need to make is swapping in database_sync_to_async 
>> rather than sync_to_async - see here: 
>> http://channels.readthedocs.io/en/latest/topics/databases.html 
>> 
>> 
>> Andrew
>> 
>> On Sun, Feb 25, 2018 at 7:14 AM, Tomáš Ehrlich > > wrote:
>> Here's the gist 
>> (https://gist.github.com/tricoder42/af3d0337c1b33d82c1b32d12bd0265ec 
>> ) with 
>> consumer.
>> 
>> Dne neděle 25. února 2018 15:37:19 UTC+1 Tomáš Ehrlich napsal(a):
>> Hello,
>> I’ve just migrated my project to django-channels 2.x. Thanks to everyone 
>> involved!
>> 
>> I’m trying to write a test for a consumer.
>> I have a post_save signal receiver, which sends a message to a group. As I 
>> understand,
>> I need to wrap `group_send` with `async_to_sync` because django signals 
>> can’t be
>> async functions:
>> 
>> def notify_on_model_changes(model):
>> from django.contrib.contenttypes.models import ContentType
>> ct = ContentType.objects.get_for_model(model)
>> model_label = '.'.join([ct.app_label, ct.model])
>> 
>> channel_layer = get_channel_layer()
>> group_send = async_to_sync(channel_layer.group_send)
>> 
>> def receiver(sender, instance, **kwargs):
>> payload = {
>> 'type': 'model.changed',
>> 'pk': instance.pk ,
>> 'model': model_label
>> }
>> group_send(f'django.{model_label}', payload)
>> 
>> post_save.connect(receiver, sender=model, weak=False,
>>   dispatch_uid=f'django.{model_label}’)
>> 
>> # in AppConfig.ready:
>> # notify_on_model_changes(Conversation)
>> 
>> 
>> 
>> My test suite, however, is async function:
>> 
>> @pytest.fixture
>> async def communicator():
>> communicator = WebsocketCommunicator(GraphqlSubcriptionConsumer, "/")
>> await communicator.connect()
>> yield communicator
>> await communicator.disconnect()
>> 
>> async def test_subscription_start(communicator):
>> def make_conversation():
>> return Conversation.objects.create()
>> 
>> # function body truncated
>> # subscribe for changes in Conversation model
>> await communicator.send_json_to(data)
>> 
>> conversation = await sync_to_async(make_conversation)()
>> response = await communicator.receive_json_from()
>> assert response['type'] == ‘data'
>> 
>> I can’t use `Conversation.objects.create()` directly, because it uses 
>> `async_to_sync`.
>> First, I need to convert it to async and await the result. I kinda feel this 
>> is hackish
>> jumping from async to sync and back to async, but so far everything works as 
>> expected
>> and test works.
>> 
>> 
>> Here comes the punchline:
>> The tests fail to teardown cleanly, because apparently there’s hanging DB 
>> connection
>> and after a while pytest just fails with `There is 1 other session using the 
>> database.`.
>> 
>> Breadcrumbs:
>> 1. If I comment out last three lines of test (make_conversations and waiting 
>> for result),
>> the test exits cleanly - seems like there’s no problem with passing 
>> `sync_to_async` function
>> to `post_save.connect`.
>> 
>> 2. If I create `async_make = sync_to_async(make_conversation)`, but don’t 
>> call it at all,
>> the test exists cleanly - I thought that there might be problem with calling 
>> `async_to_sync`
>> inside code wrapped with `sync_to_async`.
>> 
>> 
>> I suspect there’s a hanging db connection which isn’t cleaned and/or garbage 
>> collected.
>> I would also appreciate any comments about structure of such tests - is 
>> there cleaner way
>> test django signals inside async test cases?
>> 
>> 
>> Cheers,
>>Tom
>> 
>> --
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-users+unsubscr...@googlegroups.com 
>> .
>> To post to this group, send email to django-users@googlegroups.com 
>> .
>> Visit this group at https://groups.google.com/group/django-users 
>> 

Re: [django-channels] Testing events from post_save signal

2018-02-25 Thread Andrew Godwin
No problem - glad everything else seems to work alright! Getting async
testing working well has been a long, hard road :)

Andrew

On Sun, Feb 25, 2018 at 10:37 AM, Tomáš Ehrlich 
wrote:

> Of course!
>
> It works now perfectly, thank you. Sorry I missed that in docs.
>
> Cheers,
>Tom
>
> 25. 2. 2018 v 19:12, Andrew Godwin :
>
> I think the change you need to make is swapping in database_sync_to_async
> rather than sync_to_async - see here: http://channels.
> readthedocs.io/en/latest/topics/databases.html
>
> Andrew
>
> On Sun, Feb 25, 2018 at 7:14 AM, Tomáš Ehrlich 
> wrote:
>
>> Here's the gist (https://gist.github.com/trico
>> der42/af3d0337c1b33d82c1b32d12bd0265ec) with consumer.
>>
>> Dne neděle 25. února 2018 15:37:19 UTC+1 Tomáš Ehrlich napsal(a):
>>
>>> Hello,
>>> I’ve just migrated my project to django-channels 2.x. Thanks to everyone
>>> involved!
>>>
>>> I’m trying to write a test for a consumer.
>>> I have a post_save signal receiver, which sends a message to a group. As
>>> I understand,
>>> I need to wrap `group_send` with `async_to_sync` because django signals
>>> can’t be
>>> async functions:
>>>
>>> def notify_on_model_changes(model):
>>> from django.contrib.contenttypes.models import ContentType
>>> ct = ContentType.objects.get_for_model(model)
>>> model_label = '.'.join([ct.app_label, ct.model])
>>>
>>> channel_layer = get_channel_layer()
>>> group_send = async_to_sync(channel_layer.group_send)
>>>
>>> def receiver(sender, instance, **kwargs):
>>> payload = {
>>> 'type': 'model.changed',
>>> 'pk': instance.pk,
>>> 'model': model_label
>>> }
>>> group_send(f'django.{model_label}', payload)
>>>
>>> post_save.connect(receiver, sender=model, weak=False,
>>>   dispatch_uid=f'django.{model_label}’)
>>>
>>> # in AppConfig.ready:
>>> # notify_on_model_changes(Conversation)
>>>
>>>
>>>
>>> My test suite, however, is async function:
>>>
>>> @pytest.fixture
>>> async def communicator():
>>> communicator = WebsocketCommunicator(Graphq
>>> lSubcriptionConsumer, "/")
>>> await communicator.connect()
>>> yield communicator
>>> await communicator.disconnect()
>>>
>>> async def test_subscription_start(communicator):
>>> def make_conversation():
>>> return Conversation.objects.create()
>>>
>>> # function body truncated
>>> # subscribe for changes in Conversation model
>>> await communicator.send_json_to(data)
>>>
>>> conversation = await sync_to_async(make_conversation)()
>>> response = await communicator.receive_json_from()
>>> assert response['type'] == ‘data'
>>>
>>> I can’t use `Conversation.objects.create()` directly, because it uses
>>> `async_to_sync`.
>>> First, I need to convert it to async and await the result. I kinda feel
>>> this is hackish
>>> jumping from async to sync and back to async, but so far everything
>>> works as expected
>>> and test works.
>>>
>>>
>>> *Here comes the punchline:*
>>> The tests fail to teardown cleanly, because apparently there’s hanging
>>> DB connection
>>> and after a while pytest just fails with `There is 1 other session using
>>> the database.`.
>>>
>>> *Breadcrumbs*:
>>> 1. If I comment out last three lines of test (make_conversations and
>>> waiting for result),
>>> the test exits cleanly - seems like there’s no problem with passing
>>> `sync_to_async` function
>>> to `post_save.connect`.
>>>
>>> 2. If I create `async_make = sync_to_async(make_conversation)`, but
>>> don’t call it at all,
>>> the test exists cleanly - I thought that there might be problem with
>>> calling `async_to_sync`
>>> inside code wrapped with `sync_to_async`.
>>>
>>>
>>> I suspect there’s a hanging db connection which isn’t cleaned and/or
>>> garbage collected.
>>> I would also appreciate any comments about structure of such tests - is
>>> there cleaner way
>>> test django signals inside async test cases?
>>>
>>>
>>> Cheers,
>>>Tom
>>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit https://groups.google.com/d/ms
>> gid/django-users/af745c4f-7571-40d3-b738-1593d4d75bbb%40googlegroups.com
>> 
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Django users" group.
> To unsubscribe from this topic, visit 

Re: [django-channels] Testing events from post_save signal

2018-02-25 Thread Tomáš Ehrlich
Of course!

It works now perfectly, thank you. Sorry I missed that in docs.

Cheers,
   Tom

> 25. 2. 2018 v 19:12, Andrew Godwin :
> 
> I think the change you need to make is swapping in database_sync_to_async 
> rather than sync_to_async - see here: 
> http://channels.readthedocs.io/en/latest/topics/databases.html 
> 
> 
> Andrew
> 
> On Sun, Feb 25, 2018 at 7:14 AM, Tomáš Ehrlich  > wrote:
> Here's the gist 
> (https://gist.github.com/tricoder42/af3d0337c1b33d82c1b32d12bd0265ec 
> ) with 
> consumer.
> 
> Dne neděle 25. února 2018 15:37:19 UTC+1 Tomáš Ehrlich napsal(a):
> Hello,
> I’ve just migrated my project to django-channels 2.x. Thanks to everyone 
> involved!
> 
> I’m trying to write a test for a consumer.
> I have a post_save signal receiver, which sends a message to a group. As I 
> understand,
> I need to wrap `group_send` with `async_to_sync` because django signals can’t 
> be
> async functions:
> 
> def notify_on_model_changes(model):
> from django.contrib.contenttypes.models import ContentType
> ct = ContentType.objects.get_for_model(model)
> model_label = '.'.join([ct.app_label, ct.model])
> 
> channel_layer = get_channel_layer()
> group_send = async_to_sync(channel_layer.group_send)
> 
> def receiver(sender, instance, **kwargs):
> payload = {
> 'type': 'model.changed',
> 'pk': instance.pk ,
> 'model': model_label
> }
> group_send(f'django.{model_label}', payload)
> 
> post_save.connect(receiver, sender=model, weak=False,
>   dispatch_uid=f'django.{model_label}’)
> 
> # in AppConfig.ready:
> # notify_on_model_changes(Conversation)
> 
> 
> 
> My test suite, however, is async function:
> 
> @pytest.fixture
> async def communicator():
> communicator = WebsocketCommunicator(GraphqlSubcriptionConsumer, "/")
> await communicator.connect()
> yield communicator
> await communicator.disconnect()
> 
> async def test_subscription_start(communicator):
> def make_conversation():
> return Conversation.objects.create()
> 
> # function body truncated
> # subscribe for changes in Conversation model
> await communicator.send_json_to(data)
> 
> conversation = await sync_to_async(make_conversation)()
> response = await communicator.receive_json_from()
> assert response['type'] == ‘data'
> 
> I can’t use `Conversation.objects.create()` directly, because it uses 
> `async_to_sync`.
> First, I need to convert it to async and await the result. I kinda feel this 
> is hackish
> jumping from async to sync and back to async, but so far everything works as 
> expected
> and test works.
> 
> 
> Here comes the punchline:
> The tests fail to teardown cleanly, because apparently there’s hanging DB 
> connection
> and after a while pytest just fails with `There is 1 other session using the 
> database.`.
> 
> Breadcrumbs:
> 1. If I comment out last three lines of test (make_conversations and waiting 
> for result),
> the test exits cleanly - seems like there’s no problem with passing 
> `sync_to_async` function
> to `post_save.connect`.
> 
> 2. If I create `async_make = sync_to_async(make_conversation)`, but don’t 
> call it at all,
> the test exists cleanly - I thought that there might be problem with calling 
> `async_to_sync`
> inside code wrapped with `sync_to_async`.
> 
> 
> I suspect there’s a hanging db connection which isn’t cleaned and/or garbage 
> collected.
> I would also appreciate any comments about structure of such tests - is there 
> cleaner way
> test django signals inside async test cases?
> 
> 
> Cheers,
>Tom
> 
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com 
> .
> To post to this group, send email to django-users@googlegroups.com 
> .
> Visit this group at https://groups.google.com/group/django-users 
> .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/af745c4f-7571-40d3-b738-1593d4d75bbb%40googlegroups.com
>  
> .
> 
> For more options, visit https://groups.google.com/d/optout 
> .
> 
> 
> --
> You received this message because you are subscribed to a topic in the Google 
> Groups "Django users" group.
> To unsubscribe from this topic, visit 
> 

Re: [django-channels] Testing events from post_save signal

2018-02-25 Thread Andrew Godwin
I think the change you need to make is swapping in database_sync_to_async
rather than sync_to_async - see here:
http://channels.readthedocs.io/en/latest/topics/databases.html

Andrew

On Sun, Feb 25, 2018 at 7:14 AM, Tomáš Ehrlich 
wrote:

> Here's the gist (https://gist.github.com/tricoder42/
> af3d0337c1b33d82c1b32d12bd0265ec) with consumer.
>
> Dne neděle 25. února 2018 15:37:19 UTC+1 Tomáš Ehrlich napsal(a):
>
>> Hello,
>> I’ve just migrated my project to django-channels 2.x. Thanks to everyone
>> involved!
>>
>> I’m trying to write a test for a consumer.
>> I have a post_save signal receiver, which sends a message to a group. As
>> I understand,
>> I need to wrap `group_send` with `async_to_sync` because django signals
>> can’t be
>> async functions:
>>
>> def notify_on_model_changes(model):
>> from django.contrib.contenttypes.models import ContentType
>> ct = ContentType.objects.get_for_model(model)
>> model_label = '.'.join([ct.app_label, ct.model])
>>
>> channel_layer = get_channel_layer()
>> group_send = async_to_sync(channel_layer.group_send)
>>
>> def receiver(sender, instance, **kwargs):
>> payload = {
>> 'type': 'model.changed',
>> 'pk': instance.pk,
>> 'model': model_label
>> }
>> group_send(f'django.{model_label}', payload)
>>
>> post_save.connect(receiver, sender=model, weak=False,
>>   dispatch_uid=f'django.{model_label}’)
>>
>> # in AppConfig.ready:
>> # notify_on_model_changes(Conversation)
>>
>>
>>
>> My test suite, however, is async function:
>>
>> @pytest.fixture
>> async def communicator():
>> communicator = WebsocketCommunicator(GraphqlSubcriptionConsumer, "/")
>> await communicator.connect()
>> yield communicator
>> await communicator.disconnect()
>>
>> async def test_subscription_start(communicator):
>> def make_conversation():
>> return Conversation.objects.create()
>>
>> # function body truncated
>> # subscribe for changes in Conversation model
>> await communicator.send_json_to(data)
>>
>> conversation = await sync_to_async(make_conversation)()
>> response = await communicator.receive_json_from()
>> assert response['type'] == ‘data'
>>
>> I can’t use `Conversation.objects.create()` directly, because it uses
>> `async_to_sync`.
>> First, I need to convert it to async and await the result. I kinda feel
>> this is hackish
>> jumping from async to sync and back to async, but so far everything works
>> as expected
>> and test works.
>>
>>
>> *Here comes the punchline:*
>> The tests fail to teardown cleanly, because apparently there’s hanging DB
>> connection
>> and after a while pytest just fails with `There is 1 other session using
>> the database.`.
>>
>> *Breadcrumbs*:
>> 1. If I comment out last three lines of test (make_conversations and
>> waiting for result),
>> the test exits cleanly - seems like there’s no problem with passing
>> `sync_to_async` function
>> to `post_save.connect`.
>>
>> 2. If I create `async_make = sync_to_async(make_conversation)`, but
>> don’t call it at all,
>> the test exists cleanly - I thought that there might be problem with
>> calling `async_to_sync`
>> inside code wrapped with `sync_to_async`.
>>
>>
>> I suspect there’s a hanging db connection which isn’t cleaned and/or
>> garbage collected.
>> I would also appreciate any comments about structure of such tests - is
>> there cleaner way
>> test django signals inside async test cases?
>>
>>
>> Cheers,
>>Tom
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/af745c4f-7571-40d3-b738-1593d4d75bbb%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFwN1upHZSCzicBndZsqbSVZyA-erHmkfNnqUr-nNFAXBs9DjQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Industrial Analytics

2018-02-25 Thread Ryan Nowakowski
I've written an email marketing analytics app in Django that I believe will 
scale. I haven't released anywhere yet though.  You might search the web for 
"write heavy Django" since most analytics is write heavy.

On February 25, 2018 9:12:24 AM CST, Raj  wrote:
>Dear All,
>
>Can we use  Django for  Industrial Analytics ? . Where data volume is
>huge 
>& with frequency in Mill seconds.
>
>Can someone name some examples of such industrial analytics application
>
>developed using Django.
>
>Thanks,
>Raj
>
>-- 
>You received this message because you are subscribed to the Google
>Groups "Django users" group.
>To unsubscribe from this group and stop receiving emails from it, send
>an email to django-users+unsubscr...@googlegroups.com.
>To post to this group, send email to django-users@googlegroups.com.
>Visit this group at https://groups.google.com/group/django-users.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/django-users/9ef756da-8b50-4575-9c54-17830f277b51%40googlegroups.com.
>For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8E4AB01B-0351-43BC-B9C3-25B832DD972D%40fattuba.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels 2.0 - Exception when sending a message to client

2018-02-25 Thread Ken Whitesell
And while we're at it, I'm going to toss out another opinion.

I've found it extremely beneficial _for me_ to not try to relate channels 
2.0 to channels 1.1. I'm approaching it as if I'm learning about a 
completely separate package - working from a clean slate so-to-speak.

Ken

On Sunday, February 25, 2018 at 5:04:13 AM UTC-5, muha...@swordfish.co.za 
wrote:
>
> While I'm at it, would you mind confirming if I understand the following 
> changes in channels 2.0 correctly ?
>
> 1) Channel names are not auto-detected and must be specified when running 
> a worker
> 2) *runserver* no long starts up a worker by default, this has be done 
> manually
>
>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/896fab41-d482-4111-8410-b9ff690a4546%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Industrial Analytics

2018-02-25 Thread Etienne Robillard

Hi Raj,

Can you please give us a little background on what is industrial analytics?

Thank you,

Etienne


Le 2018-02-25 à 10:12, Raj a écrit :

Dear All,

Can we use  Django for  Industrial Analytics ? . Where data volume is 
huge & with frequency in Mill seconds.


Can someone name some examples of such industrial analytics 
application developed using Django.


Thanks,
Raj

--
You received this message because you are subscribed to the Google 
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to django-users+unsubscr...@googlegroups.com 
.
To post to this group, send email to django-users@googlegroups.com 
.

Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/9ef756da-8b50-4575-9c54-17830f277b51%40googlegroups.com 
.

For more options, visit https://groups.google.com/d/optout.


--
Etienne Robillard
tkad...@yandex.com
https://www.isotopesoftware.ca/

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/918cc308-ba6e-a71c-3a21-0da8f7840598%40yandex.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels 2.0 - Exception when sending a message to client

2018-02-25 Thread Ken Whitesell
In so far as you are talking about background worker tasks, you are correct.

But those statements do not pertain to a websocket consumer that accepts 
connections from a browser. You can run an application without ever 
creating a worker task. (Again, I'll refer you to the channels-examples app 
for such a demonstration.)

Ken


On Sunday, February 25, 2018 at 5:04:13 AM UTC-5, muha...@swordfish.co.za 
wrote:
>
> While I'm at it, would you mind confirming if I understand the following 
> changes in channels 2.0 correctly ?
>
> 1) Channel names are not auto-detected and must be specified when running 
> a worker
> 2) *runserver* no long starts up a worker by default, this has be done 
> manually
>
> On Sunday, February 25, 2018 at 2:56:45 AM UTC+2, muha...@swordfish.co.za 
> wrote:
>>
>> I'm still trying to find my way around channels 2.0, so I'm not sure if 
>> my implementation is incorrect or if this is a valid issue. I'm going to 
>> post as much info as possible in the hope that it will assist with finding 
>> the problem.
>>
>> I have a single page app which opens a JS websocket connection - with 
>> channels 1 I used to add a *session_key *to the querystring and that 
>> used to handle the authentication.
>>
>> I see this is no longer the case, so I now have a custom middleware class 
>> that sets the user object on the scope:
>>
>> from django.contrib.sessions.models import Session
>>
>> from users.models import User
>>
>>
>> class QueryAuthMiddleware:
>>
>> def __init__(self, inner):
>> # Store the ASGI application we were passed
>> self.inner = inner
>>
>> def __call__(self, scope):
>> # Look up user from query string (you should also do things like
>> # check it's a valid user ID, or if scope["user"] is already 
>> populated)
>> if scope.get("user", None) is None:
>> session_obj = 
>> Session.objects.get(session_key=scope["query_string"].decode("utf-8").split("=")[1])
>> session_decoded = session_obj.get_decoded()
>>
>> scope["user"] = 
>> User.objects.get(id=session_decoded.get("_auth_user_id"))
>>
>> # Return the inner application directly and let it run 
>> everything else
>> return self.inner(scope)
>>
>>
>>
>>
>> This is in turn added to my routing (channels.py):
>>
>> from django.conf.urls import url
>> from django.conf import settings
>> from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter
>>
>> from notifications.consumer import TestWebsocketConsumer, TestConsumer
>> from notifications.middleware.query_auth_middleware import 
>> QueryAuthMiddleware
>>
>> ROOT_PATH = "" if settings.DEBUG else "/ws/"
>>
>>
>> application = ProtocolTypeRouter({
>>
>> "websocket": QueryAuthMiddleware(
>> URLRouter([
>> url(f"^{ROOT_PATH}(?P[-\w]+)/$", TestWebsocketConsumer),
>>
>> ])
>> ),
>>
>> "channel": ChannelNameRouter({
>>  "user-notifications": TestConsumer,
>> })
>>
>>
>> })
>>
>>
>>
>>
>>
>> Here's my *consumers.py*:
>>
>> from asgiref.sync import async_to_sync
>> from channels.consumer import SyncConsumer
>> from channels.generic.websocket import WebsocketConsumer
>>
>>
>> class TestWebsocketConsumer(WebsocketConsumer):
>> def websocket_connect(self, message):
>> 
>> async_to_sync(self.channel_layer.group_add)(str(self.scope["user"].id), 
>> "user-notifications")
>> self.connect()
>>
>>
>> class TestConsumer(SyncConsumer):
>> def notification_handler(self, message):
>>
>> self.send(
>> {
>>  "type": "websocket.send",
>>  "text": message["text"]
>> }
>> )
>>
>>
>>
>>
>>
>>
>> The idea of the app is that each user that logs in on the front end is 
>> able to receive messages meant only for them sent by the back end.  I have 
>> been trying to test it like this:
>>
>> >>> channel_layer = get_channel_layer()
>> >>> async_to_sync(channel_layer.send)("user-notifications", {"type": 
>> >>> "notification.handler", "text": "My Message"})
>>
>>
>>
>>
>> Here's the traceback in the *runworker* output:
>>
>> 2018-02-25 02:34:14,002 - INFO - runworker - Running worker for channels 
>> ['user-notifications']
>> ERROR:root:Exception inside application: You must implement 
>> application_send()
>>   File 
>> "/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/consumer.py",
>>  
>> line 54, in __call__
>> await await_many_dispatch([receive, self.channel_receive], 
>> self.dispatch)
>>   File 
>> "/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/utils.py",
>>  
>> line 48, in await_many_dispatch
>> await dispatch(result)
>>   File 
>> "/Users/muhammed/projects/xxx/lib/python3.6/site-packages/asgiref/sync.py", 
>> line 110, in __call__
>> return await asyncio.wait_for(future, timeout=None)
>>   File 
>> "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py",

Re: Channels 2.0 - Exception when sending a message to client

2018-02-25 Thread Ken Whitesell
Before I get to the guts of my answer, I'd suggest you get Andrew Godwin's 
"channels-examples" application and read it completely until you believe 
you understand everything that it's doing. 
(https://github.com/andrewgodwin/channels-examples)

Now, to the extent of my understanding, the "ChannelNameRouter" is used for 
background tasks - it's not used for connections by a websocket client- so 
the API is different than the router used for a websocket.

The applicable excerpt from the routing.py file in the channels-examples 
app (comments removed - see the full file for more details):
application = ProtocolTypeRouter({
"websocket": AuthMiddlewareStack(
URLRouter([
# URLRouter just takes standard Django path() or url() entries.
path("chat/stream/", ChatConsumer),
]),
),
"channel": ChannelNameRouter({
"mpge-timer": TimerConsumer
}),

})

What this is saying to me is that in the channels world, there are (at 
least) two fundamentally different types of objects - 
1) There are websocket consumers, which are connected to by an external 
client using a websocket connection, _and_ can send and receive messages on 
the internal channel. These are defined in the "websocket" key of the 
ProtocolTypeRouter object.
When they are created by a connection being made, they are given a "scope" 
which exists for the life of the connection. This scope include a 
system-generated channel name - but it's implicit in the API that that 
consumer uses to send & receive messages - you don't specify it when making 
calls. (Don't know if that's universally true, but it's my current 
impression.)

2) There are worker tasks, which do _not_ create or use websockets to make 
a connection to the outside world. They exist to send and receive events 
via channels. (They can also make changes to your models, so strictly 
speaking, channels aren't their only means of communication to other tasks 
or processes.)

So for your task, you'll want to define your TestWebSocketConsumer within 
the websocket key of the ProtocolTypeRouter

Keep in mind that a websocket is created as an "upgrade" of a regular HTTP 
connection - hence the requirement of a url as part of the routing. 
Effectively, the client is issuing an HTTP GET request for a url, and also 
says, "Oh, by the way, I want to upgrade this connection to be a 
websocket". (The actual protocol is a little bit more complex than that - 
but not too much.) So the server gets the GET, and needs to route it to 
something that will handle that URL and provide the upgraded protocol. That 
is handled by Daphne at the lowest layers - this definition is just 
information that Daphne uses to figure out what object gets to handle each 
url.

Hope this helps.
Ken

Disclaimer: I am not Andrew Godwin, I do not play him on TV, and my code 
will never be mistaken for his. In any situation where there's a 
discrepancy between anything he has written and my comments, I am wrong. At 
this point I'm guessing I'm just about a step and a half ahead of you in 
working my way though this.


On Sunday, February 25, 2018 at 4:42:27 AM UTC-5, muha...@swordfish.co.za 
wrote:
>
> Thanks for the update - I think I understand now.
>
> I updated my channel name routing and pointed it at the Websocket consumer:
>
> "channel": ChannelNameRouter({
> "user-notifications": TestWebsocketConsumer,
> })
>
>
>
> Moved the message handler into *TestWebsocketConsumer:*
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *from asgiref.sync import async_to_syncfrom channels.generic.websocket 
> import WebsocketConsumerclass TestWebsocketConsumer(WebsocketConsumer):
> def websocket_connect(self, message):
> async_to_sync(self.channel_layer.group_add)(str(self.scope["user"].id), 
> "user-notifications")self.connect()def 
> notification_handler(self, message):self.send({
>  "text": message["text"]})*
> This raises the same exception:
>
> ERROR:root:Exception inside application: You must implement 
> application_send()
>   File 
> "/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/consumer.py",
>  
> line 54, in __call__
> await await_many_dispatch([receive, self.channel_receive], 
> self.dispatch)
>   File 
> "/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/utils.py", 
> line 48, in await_many_dispatch
> await dispatch(result)
>   File 
> "/Users/muhammed/projects/xxx/lib/python3.6/site-packages/asgiref/sync.py", 
> line 110, in __call__
> return await asyncio.wait_for(future, timeout=None)
>   File 
> "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py",
>  
> line 333, in wait_for
> return (yield from fut)
>   File 
> "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/concurrent/futures/thread.py",
>  
> line 55, in run
> result = self.fn(*self.args, 

Re: Simple Search Feature

2018-02-25 Thread José Sánchez Moreno
I would do something simitar to:

class SongListView(ListView):
 model = Song
 context_object_name = 'songs'
 template_name = 'songs/song_list.html'
 
 def get_queryset(self):
   qs = super().get_queryset()
   name = self.request.get("name")
   if name:
  qs = qs.filter(name__icontains=name)  
   return qs
   
El dom, feb 25 2018 a las 09:17:07 , tango ward 
escribió:
> Hi,
>
> I am playing around on adding a search feature to a website. I am currently
> encountering a problem when the page should load a list of songs and after
> typing in a song title in the search box:
>
>
> views.py
>
> class SongListView(ListView):
> model = Song
> context_object_name = 'songs'
> template_name = 'songs/song_list.html'
>
> def get(self, request, *args, **kwargs):
>
> if request.method == 'GET':
> song_name = request.GET.get('name', "Error")
> songs = self.model.objects.filter(name__icontains=song_name)
> else:
> songs = self.models.all()
> return render(request, self.template_name, {'songs': songs})
>
> problem is when I click the search button with the text box empty, I am
> getting the list of all the song (url: /songs/?name=) but if I just load
> the page wihout clicking the submit button (url: /songs/), it doesn't give
> me the list all the songs. The search box works if I type the correct song
> name as it shows the song title.  Problem is the page should load all the
> songs before I search a particular song.
>
> Any suggestions so I can enhance my code?
>
>
> Thanks,
> Jarvis
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/CAA6wQLJ5bnMzR1etSRY191KL3fWkMpPsvhKnFBKtAuc1iLNKFg%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
José Sánchez Moreno 
O2W Leading Software - www.o2w.es
C/ Trovero Marin, 5 30730 San Javier (Murcia)
Tlf: 968 192 905 - 656 817 548
Condiciones Legales de este e-mail en: http://www.o2w.es/email

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/87h8q5h1v7.fsf%40o2w.es.
For more options, visit https://groups.google.com/d/optout.


Industrial Analytics

2018-02-25 Thread Raj
Dear All,

Can we use  Django for  Industrial Analytics ? . Where data volume is huge 
& with frequency in Mill seconds.

Can someone name some examples of such industrial analytics application 
developed using Django.

Thanks,
Raj

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/9ef756da-8b50-4575-9c54-17830f277b51%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [django-channels] Testing events from post_save signal

2018-02-25 Thread Tomáš Ehrlich
Here's the gist 
(https://gist.github.com/tricoder42/af3d0337c1b33d82c1b32d12bd0265ec) with 
consumer.

Dne neděle 25. února 2018 15:37:19 UTC+1 Tomáš Ehrlich napsal(a):
>
> Hello,
> I’ve just migrated my project to django-channels 2.x. Thanks to everyone 
> involved!
>
> I’m trying to write a test for a consumer.
> I have a post_save signal receiver, which sends a message to a group. As I 
> understand,
> I need to wrap `group_send` with `async_to_sync` because django signals 
> can’t be
> async functions:
>
> def notify_on_model_changes(model):
> from django.contrib.contenttypes.models import ContentType
> ct = ContentType.objects.get_for_model(model)
> model_label = '.'.join([ct.app_label, ct.model])
>
> channel_layer = get_channel_layer()
> group_send = async_to_sync(channel_layer.group_send)
>
> def receiver(sender, instance, **kwargs):
> payload = {
> 'type': 'model.changed',
> 'pk': instance.pk,
> 'model': model_label
> } 
> group_send(f'django.{model_label}', payload)
>
> post_save.connect(receiver, sender=model, weak=False,
>   dispatch_uid=f'django.{model_label}’)
>
> # in AppConfig.ready:
> # notify_on_model_changes(Conversation)
>
>
>
> My test suite, however, is async function:
>
> @pytest.fixture
> async def communicator():
> communicator = WebsocketCommunicator(GraphqlSubcriptionConsumer, "/")
> await communicator.connect()
> yield communicator
> await communicator.disconnect()
> 
> async def test_subscription_start(communicator):
> def make_conversation():
> return Conversation.objects.create()
>
> # function body truncated
> # subscribe for changes in Conversation model
> await communicator.send_json_to(data)
>
> conversation = await sync_to_async(make_conversation)()
> response = await communicator.receive_json_from()
> assert response['type'] == ‘data'
>
> I can’t use `Conversation.objects.create()` directly, because it uses 
> `async_to_sync`.
> First, I need to convert it to async and await the result. I kinda feel 
> this is hackish
> jumping from async to sync and back to async, but so far everything works 
> as expected
> and test works.
>
>
> *Here comes the punchline:*
> The tests fail to teardown cleanly, because apparently there’s hanging DB 
> connection
> and after a while pytest just fails with `There is 1 other session using 
> the database.`.
>
> *Breadcrumbs*:
> 1. If I comment out last three lines of test (make_conversations and 
> waiting for result),
> the test exits cleanly - seems like there’s no problem with passing 
> `sync_to_async` function
> to `post_save.connect`.
>
> 2. If I create `async_make = sync_to_async(make_conversation)`, but don’t 
> call it at all,
> the test exists cleanly - I thought that there might be problem with 
> calling `async_to_sync`
> inside code wrapped with `sync_to_async`.
>
>
> I suspect there’s a hanging db connection which isn’t cleaned and/or 
> garbage collected.
> I would also appreciate any comments about structure of such tests - is 
> there cleaner way
> test django signals inside async test cases?
>
>
> Cheers,
>Tom
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/af745c4f-7571-40d3-b738-1593d4d75bbb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[django-channels] Testing events from post_save signal

2018-02-25 Thread Tomáš Ehrlich
Hello,
I’ve just migrated my project to django-channels 2.x. Thanks to everyone 
involved!

I’m trying to write a test for a consumer.
I have a post_save signal receiver, which sends a message to a group. As I 
understand,
I need to wrap `group_send` with `async_to_sync` because django signals can’t be
async functions:

def notify_on_model_changes(model):
from django.contrib.contenttypes.models import ContentType
ct = ContentType.objects.get_for_model(model)
model_label = '.'.join([ct.app_label, ct.model])

channel_layer = get_channel_layer()
group_send = async_to_sync(channel_layer.group_send)

def receiver(sender, instance, **kwargs):
payload = {
'type': 'model.changed',
'pk': instance.pk,
'model': model_label
}
group_send(f'django.{model_label}', payload)

post_save.connect(receiver, sender=model, weak=False,
  dispatch_uid=f'django.{model_label}’)

# in AppConfig.ready:
# notify_on_model_changes(Conversation)



My test suite, however, is async function:

@pytest.fixture
async def communicator():
communicator = WebsocketCommunicator(GraphqlSubcriptionConsumer, "/")
await communicator.connect()
yield communicator
await communicator.disconnect()

async def test_subscription_start(communicator):
def make_conversation():
return Conversation.objects.create()

# function body truncated
# subscribe for changes in Conversation model
await communicator.send_json_to(data)

conversation = await sync_to_async(make_conversation)()
response = await communicator.receive_json_from()
assert response['type'] == ‘data'

I can’t use `Conversation.objects.create()` directly, because it uses 
`async_to_sync`.
First, I need to convert it to async and await the result. I kinda feel this is 
hackish
jumping from async to sync and back to async, but so far everything works as 
expected
and test works.


Here comes the punchline:
The tests fail to teardown cleanly, because apparently there’s hanging DB 
connection
and after a while pytest just fails with `There is 1 other session using the 
database.`.

Breadcrumbs:
1. If I comment out last three lines of test (make_conversations and waiting 
for result),
the test exits cleanly - seems like there’s no problem with passing 
`sync_to_async` function
to `post_save.connect`.

2. If I create `async_make = sync_to_async(make_conversation)`, but don’t call 
it at all,
the test exists cleanly - I thought that there might be problem with calling 
`async_to_sync`
inside code wrapped with `sync_to_async`.


I suspect there’s a hanging db connection which isn’t cleaned and/or garbage 
collected.
I would also appreciate any comments about structure of such tests - is there 
cleaner way
test django signals inside async test cases?


Cheers,
   Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/6F600904-9697-47A6-A813-9FBB6A69E7C9%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


signature.asc
Description: Message signed with OpenPGP


Re: Simple Search Feature

2018-02-25 Thread Jani Tiainen
Hi,

"Is it safe"? Well that is your call to make. Depending number of songs it
might not be meanful for enduser to see whole list which leads to things
like pagination and restricting maximum number of returned entries.

On Sun, Feb 25, 2018 at 8:10 AM, tango ward  wrote:

> thanks for the suggestions Ken. I just want to ask too if it's safe to
> display the list of songs even if the textbox is empty?
>
> On Sun, Feb 25, 2018 at 10:21 AM, Ken Whitesell 
> wrote:
>
>> One of the issues is here:
>> if request.method == 'GET':
>> song_name = request.GET.get('name', "Error")
>> songs = self.model.objects.filter(name__icontains=song_name)
>> else:
>> songs = self.models.all()
>> return render(request, self.template_name, {'songs': songs})
>>
>> When no parameter is passed, request.method is still "GET" - In fact,
>> this method should only be called on a get method, making this test
>> irrelevant.
>>
>> You _could_ change it to look something like this:
>> song_name = request.GET.get('name', None)
>> if song_name:
>> songs = self.model.objects.filter(name__icontains=song_name)
>> else:
>> songs = self.models.all()
>> return render(request, self.template_name, {'songs': songs})
>>
>> Hope this helps,
>>  Ken
>>
>>
>> On Saturday, February 24, 2018 at 8:18:25 PM UTC-5, tangoward15 wrote:
>>>
>>> Hi,
>>>
>>> I am playing around on adding a search feature to a website. I am
>>> currently encountering a problem when the page should load a list of songs
>>> and after typing in a song title in the search box:
>>>
>>>
>>> views.py
>>>
>>> class SongListView(ListView):
>>> model = SongOne
>>> context_object_name = 'songs'
>>> template_name = 'songs/song_list.html'
>>>
>>> def get(self, request, *args, **kwargs):
>>>
>>> if request.method == 'GET':
>>> song_name = request.GET.get('name', "Error")
>>> songs = self.model.objects.filter(name__icontains=song_name)
>>> else:
>>> songs = self.models.all()
>>> return render(request, self.template_name, {'songs': songs})
>>>
>>> problem is when I click the search button with the text box empty, I am
>>> getting the list of all the song (url: /songs/?name=) but if I just load
>>> the page wihout clicking the submit button (url: /songs/), it doesn't give
>>> me the list all the songs. The search box works if I type the correct song
>>> name as it shows the song title.  Problem is the page should load all the
>>> songs before I search a particular song.
>>>
>>> Any suggestions so I can enhance my code?
>>>
>>>
>>> Thanks,
>>> Jarvis
>>>
>>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit https://groups.google.com/d/ms
>> gid/django-users/fee7baf6-7dbe-4a33-a281-f7c8600f73cc%40googlegroups.com
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/CAA6wQLK03p7hofrCRAGYQsUxo4NuY
> ddTN5e3rmmOdrUSV-uOUQ%40mail.gmail.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHn91ofQqYe-V-eoP9n2-VuE3a3YHQpDKpKE4%3Dqjr_2-8hDxPw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Channels 2.0 - Exception when sending a message to client

2018-02-25 Thread muhammed
While I'm at it, would you mind confirming if I understand the following 
changes in channels 2.0 correctly ?

1) Channel names are not auto-detected and must be specified when running a 
worker
2) *runserver* no long starts up a worker by default, this has be done 
manually

On Sunday, February 25, 2018 at 2:56:45 AM UTC+2, muha...@swordfish.co.za 
wrote:
>
> I'm still trying to find my way around channels 2.0, so I'm not sure if my 
> implementation is incorrect or if this is a valid issue. I'm going to post 
> as much info as possible in the hope that it will assist with finding the 
> problem.
>
> I have a single page app which opens a JS websocket connection - with 
> channels 1 I used to add a *session_key *to the querystring and that used 
> to handle the authentication.
>
> I see this is no longer the case, so I now have a custom middleware class 
> that sets the user object on the scope:
>
> from django.contrib.sessions.models import Session
>
> from users.models import User
>
>
> class QueryAuthMiddleware:
>
> def __init__(self, inner):
> # Store the ASGI application we were passed
> self.inner = inner
>
> def __call__(self, scope):
> # Look up user from query string (you should also do things like
> # check it's a valid user ID, or if scope["user"] is already 
> populated)
> if scope.get("user", None) is None:
> session_obj = 
> Session.objects.get(session_key=scope["query_string"].decode("utf-8").split("=")[1])
> session_decoded = session_obj.get_decoded()
>
> scope["user"] = 
> User.objects.get(id=session_decoded.get("_auth_user_id"))
>
> # Return the inner application directly and let it run everything 
> else
> return self.inner(scope)
>
>
>
>
> This is in turn added to my routing (channels.py):
>
> from django.conf.urls import url
> from django.conf import settings
> from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter
>
> from notifications.consumer import TestWebsocketConsumer, TestConsumer
> from notifications.middleware.query_auth_middleware import QueryAuthMiddleware
>
> ROOT_PATH = "" if settings.DEBUG else "/ws/"
>
>
> application = ProtocolTypeRouter({
>
> "websocket": QueryAuthMiddleware(
> URLRouter([
> url(f"^{ROOT_PATH}(?P[-\w]+)/$", TestWebsocketConsumer),
>
> ])
> ),
>
> "channel": ChannelNameRouter({
>  "user-notifications": TestConsumer,
> })
>
>
> })
>
>
>
>
>
> Here's my *consumers.py*:
>
> from asgiref.sync import async_to_sync
> from channels.consumer import SyncConsumer
> from channels.generic.websocket import WebsocketConsumer
>
>
> class TestWebsocketConsumer(WebsocketConsumer):
> def websocket_connect(self, message):
> 
> async_to_sync(self.channel_layer.group_add)(str(self.scope["user"].id), 
> "user-notifications")
> self.connect()
>
>
> class TestConsumer(SyncConsumer):
> def notification_handler(self, message):
>
> self.send(
> {
>  "type": "websocket.send",
>  "text": message["text"]
> }
> )
>
>
>
>
>
>
> The idea of the app is that each user that logs in on the front end is 
> able to receive messages meant only for them sent by the back end.  I have 
> been trying to test it like this:
>
> >>> channel_layer = get_channel_layer()
> >>> async_to_sync(channel_layer.send)("user-notifications", {"type": 
> >>> "notification.handler", "text": "My Message"})
>
>
>
>
> Here's the traceback in the *runworker* output:
>
> 2018-02-25 02:34:14,002 - INFO - runworker - Running worker for channels 
> ['user-notifications']
> ERROR:root:Exception inside application: You must implement 
> application_send()
>   File 
> "/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/consumer.py",
>  
> line 54, in __call__
> await await_many_dispatch([receive, self.channel_receive], 
> self.dispatch)
>   File 
> "/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/utils.py", 
> line 48, in await_many_dispatch
> await dispatch(result)
>   File 
> "/Users/muhammed/projects/xxx/lib/python3.6/site-packages/asgiref/sync.py", 
> line 110, in __call__
> return await asyncio.wait_for(future, timeout=None)
>   File 
> "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py",
>  
> line 333, in wait_for
> return (yield from fut)
>   File 
> "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/concurrent/futures/thread.py",
>  
> line 55, in run
> result = self.fn(*self.args, **self.kwargs)
>   File 
> "/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/db.py", 
> line 13, in thread_handler
> return super().thread_handler(loop, *args, **kwargs)
>   File 
> "/Users/muhammed/projects/xxx/lib/python3.6/site-packages/asgiref/sync.py", 
> line 125, in thread_handler
> return 

Re: Channels 2.0 - Exception when sending a message to client

2018-02-25 Thread muhammed
Thanks for the update - I think I understand now.

I updated my channel name routing and pointed it at the Websocket consumer:

"channel": ChannelNameRouter({
"user-notifications": TestWebsocketConsumer,
})



Moved the message handler into *TestWebsocketConsumer:*



















*from asgiref.sync import async_to_syncfrom channels.generic.websocket 
import WebsocketConsumerclass TestWebsocketConsumer(WebsocketConsumer):
def websocket_connect(self, message):
async_to_sync(self.channel_layer.group_add)(str(self.scope["user"].id), 
"user-notifications")self.connect()def 
notification_handler(self, message):self.send({
 "text": message["text"]})*
This raises the same exception:

ERROR:root:Exception inside application: You must implement 
application_send()
  File 
"/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/consumer.py",
 
line 54, in __call__
await await_many_dispatch([receive, self.channel_receive], 
self.dispatch)
  File 
"/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/utils.py", 
line 48, in await_many_dispatch
await dispatch(result)
  File 
"/Users/muhammed/projects/xxx/lib/python3.6/site-packages/asgiref/sync.py", 
line 110, in __call__
return await asyncio.wait_for(future, timeout=None)
  File 
"/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py",
 
line 333, in wait_for
return (yield from fut)
  File 
"/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/concurrent/futures/thread.py",
 
line 55, in run
result = self.fn(*self.args, **self.kwargs)
  File 
"/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/db.py", 
line 13, in thread_handler
return super().thread_handler(loop, *args, **kwargs)
  File 
"/Users/muhammed/projects/xxx/lib/python3.6/site-packages/asgiref/sync.py", 
line 125, in thread_handler
return self.func(*args, **kwargs)
  File 
"/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/consumer.py",
 
line 99, in dispatch
handler(message)
  File "/Users/muhammed/projects/xxx/myapp/app/notifications/consumer.py", 
line 15, in notification_handler
"text": message["text"]
  File 
"/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/generic/websocket.py",
 
line 56, in send
{"type": "websocket.send", "text": text_data},
  File 
"/Users/muhammed/projects/xxx/lib/python3.6/site-packages/channels/consumer.py",
 
line 107, in send
self.base_send(message)
  File 
"/Users/muhammed/projects/xxx/lib/python3.6/site-packages/asgiref/sync.py", 
line 64, in __call__
return call_result.result()
  File 
"/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/concurrent/futures/_base.py",
 
line 405, in result
return self.__get_result()
  File 
"/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/concurrent/futures/_base.py",
 
line 357, in __get_result
raise self._exception
  File 
"/Users/muhammed/projects/xxx/lib/python3.6/site-packages/asgiref/sync.py", 
line 78, in main_wrap
result = await self.awaitable(*args, **kwargs)
  File 
"/Users/muhammed/projects/xxx/lib/python3.6/site-packages/asgiref/server.py", 
line 71, in application_send
raise NotImplementedError("You must implement application_send()")
  You must implement application_send()

In my channels 1.x implementation, the channel name was routed to a method 
where I did a group send. I also tried this implementation in the websocket 
consumer:

def notification_handler(self, message):
async_to_sync(self.channel_layer.group_send)(
"1", # The group id
{
 "text": message["text"]
}
)
print("done")



In my runworker output I see the print statement output, however on my 
front end app, the websocket *onmessage *callback is not fired.

Apologies again if it's something obvious.


On Sunday, February 25, 2018 at 2:56:45 AM UTC+2, muha...@swordfish.co.za 
wrote:
>
> I'm still trying to find my way around channels 2.0, so I'm not sure if my 
> implementation is incorrect or if this is a valid issue. I'm going to post 
> as much info as possible in the hope that it will assist with finding the 
> problem.
>
> I have a single page app which opens a JS websocket connection - with 
> channels 1 I used to add a *session_key *to the querystring and that used 
> to handle the authentication.
>
> I see this is no longer the case, so I now have a custom middleware class 
> that sets the user object on the scope:
>
> from django.contrib.sessions.models import Session
>
> from users.models import User
>
>
> class QueryAuthMiddleware:
>
> def __init__(self, inner):
> # Store the ASGI application we were passed
> self.inner = inner
>
> def __call__(self, scope):
> # Look up user from query string (you should