Skip to content

Commit

Permalink
Deactivate websockets for production testing
Browse files Browse the repository at this point in the history
  • Loading branch information
juliolugo96 committed Jul 15, 2019
1 parent dce6c01 commit 331c58c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 95 deletions.
12 changes: 6 additions & 6 deletions api/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from django.dispatch import receiver
from users.models import CustomUser
from api.models import *
from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer
# from asgiref.sync import async_to_sync
# from channels.layers import get_channel_layer


######################################
Expand All @@ -14,10 +14,10 @@
# 3.- Comment notification
# 4.- Finish notification

def send_notification(user, content):
group_name = 'user_%s' % user.id
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)(group_name, content)
# def send_notification(user, content):
# group_name = 'user_%s' % user.id
# channel_layer = get_channel_layer()
# async_to_sync(channel_layer.group_send)(group_name, content)


@receiver(post_save, sender=CustomUser)
Expand Down
14 changes: 7 additions & 7 deletions projexbackend/asgi.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from channels.layers import get_channel_layer
from channels.routing import get_default_application
# import os
# from channels.layers import get_channel_layer
# from channels.routing import get_default_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE',
'projexbackend.config.development')
channel_layer = get_channel_layer()
application = get_default_application()
# os.environ.setdefault('DJANGO_SETTINGS_MODULE',
# 'projexbackend.config.development')
# channel_layer = get_channel_layer()
# application = get_default_application()
20 changes: 10 additions & 10 deletions projexbackend/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

'allauth',
'allauth.account',
'channels',
# 'channels',
'corsheaders',
'django_countries',
'rest_auth',
Expand All @@ -65,16 +65,16 @@
'--cover-package=api,users',
]

ASGI_APPLICATION = 'projexbackend.routing.application'
# ASGI_APPLICATION = 'projexbackend.routing.application'

CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379), ('projexbackend.herokuapp.com', 6379)],
},
},
}
# CHANNEL_LAYERS = {
# 'default': {
# 'BACKEND': 'channels_redis.core.RedisChannelLayer',
# 'CONFIG': {
# "hosts": [('127.0.0.1', 6379), ('projexbackend.herokuapp.com', 6379)],
# },
# },
# }

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

Expand Down
110 changes: 55 additions & 55 deletions projexbackend/consumers.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
from channels.generic.websocket import JsonWebsocketConsumer
from rest_framework import serializers
from api.serializers import *
from asgiref.sync import async_to_sync
import json
# from channels.generic.websocket import JsonWebsocketConsumer
# from rest_framework import serializers
# from api.serializers import *
# from asgiref.sync import async_to_sync
# import json


class NotificationConsumer(JsonWebsocketConsumer):
def connect(self):
# We're always going to accept the connection, though we may
# close it later based on other factors.
user = self.scope.get('user')
group_name = user.get_group_name
# class NotificationConsumer(JsonWebsocketConsumer):
# def connect(self):
# # We're always going to accept the connection, though we may
# # close it later based on other factors.
# user = self.scope.get('user')
# group_name = user.get_group_name

async_to_sync(self.channel_layer.group_add(
group_name,
self.channel_name,
))
# async_to_sync(self.channel_layer.group_add(
# group_name,
# self.channel_name,
# ))

self.accept()
# self.accept()

def notify(self, event):
"""
This handles calls elsewhere in this codebase that look
like:
# def notify(self, event):
# """
# This handles calls elsewhere in this codebase that look
# like:

channel_layer.group_send(group_name, {
'type': 'notify', # This routes it to this handler.
'content': json_message,
})
# channel_layer.group_send(group_name, {
# 'type': 'notify', # This routes it to this handler.
# 'content': json_message,
# })

Don't try to directly use send_json or anything; this
decoupling will help you as things grow.
"""
# Don't try to directly use send_json or anything; this
# decoupling will help you as things grow.
# """

self.send_json(event["payload"])
# self.send_json(event["payload"])

def websocket_receive(self, content, **kwargs):
"""
This handles data sent over the wire from the client.
# def websocket_receive(self, content, **kwargs):
# """
# This handles data sent over the wire from the client.

We need to validate that the received data is of the correct
form. You can do this with a simple DRF serializer.
# We need to validate that the received data is of the correct
# form. You can do this with a simple DRF serializer.

We then need to use that validated data to confirm that the
requesting user (available in self.scope["user"] because of
the use of channels.auth.AuthMiddlewareStack in routing) is
allowed to subscribe to the requested object.
"""
#print(content)
# serializer = self.get_serializer(data=content)
# if not serializer.is_valid():
# return
# # Define this method on your serializer:
# group_name = serializer.get_group_name()
# # The AsyncJsonWebsocketConsumer parent class has a
# # self.groups list already. It uses it in cleanup.
# self.groups.append(group_name)
# # This actually subscribes the requesting socket to the
# # named group:
# await self.channel_layer.group_add(
# group_name,
# self.channel_name,
# )
# We then need to use that validated data to confirm that the
# requesting user (available in self.scope["user"] because of
# the use of channels.auth.AuthMiddlewareStack in routing) is
# allowed to subscribe to the requested object.
# """
# #print(content)
# # serializer = self.get_serializer(data=content)
# # if not serializer.is_valid():
# # return
# # # Define this method on your serializer:
# # group_name = serializer.get_group_name()
# # # The AsyncJsonWebsocketConsumer parent class has a
# # # self.groups list already. It uses it in cleanup.
# # self.groups.append(group_name)
# # # This actually subscribes the requesting socket to the
# # # named group:
# # await self.channel_layer.group_add(
# # group_name,
# # self.channel_name,
# # )

def websocket_disconnect(self):
super(self)
# def websocket_disconnect(self):
# super(self)
34 changes: 17 additions & 17 deletions projexbackend/routing.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# mysite/routing.py
from channels.routing import ProtocolTypeRouter
from django.urls import path
# # mysite/routing.py
# from channels.routing import ProtocolTypeRouter
# from django.urls import path

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
# from channels.auth import AuthMiddlewareStack
# from channels.routing import ProtocolTypeRouter, URLRouter

from .consumers import NotificationConsumer
# from .consumers import NotificationConsumer


websockets = URLRouter([
path(
"ws/notifications",
NotificationConsumer,
name="ws_notifications",
)
])
# websockets = URLRouter([
# path(
# "ws/notifications",
# NotificationConsumer,
# name="ws_notifications",
# )
# ])


application = ProtocolTypeRouter({
# (http->django views is added by default)
"websocket": AuthMiddlewareStack(websockets),
})
# application = ProtocolTypeRouter({
# # (http->django views is added by default)
# "websocket": AuthMiddlewareStack(websockets),
# })

0 comments on commit 331c58c

Please sign in to comment.