Got channels2 working.

This commit is contained in:
Víðir Valberg Guðmundsson 2018-04-03 20:52:39 +02:00
parent ff758b15ff
commit aae3d7e3c9
4 changed files with 42 additions and 22 deletions

View File

@ -28,7 +28,7 @@ CHANNEL_LAYERS = {
},
}
ASGI_APPLICATION = "bornhack.routing.channel_routing"
ASGI_APPLICATION = "bornhack.routing.application"
# start redirecting to the next camp instead of the previous camp after
# this much of the time between the camps has passed

View File

@ -1,8 +1,14 @@
from django.conf.urls import url
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from program.consumers import ScheduleConsumer
channel_routing = [
ScheduleConsumer.as_route(path=r"^/schedule/"),
]
application = ProtocolTypeRouter({
"websocket": AuthMiddlewareStack(
URLRouter([
url(r"^schedule/", ScheduleConsumer)
])
)
})

View File

@ -1,4 +1,4 @@
from channels.generic.websockets import JsonWebsocketConsumer
from channels.generic.websocket import JsonWebsocketConsumer
from camps.models import Camp
from .models import (
@ -12,13 +12,11 @@ from .models import (
class ScheduleConsumer(JsonWebsocketConsumer):
http_user = True
groups = ['schedule_users']
def connection_groups(self, **kwargs):
return ['schedule_users']
def raw_receive(self, message, **kwargs):
content = self.decode_json(message['text'])
def receive(self, text_data, **kwargs):
user = self.scope['user']
content = self.decode_json(text_data)
action = content.get('action')
data = {}
@ -39,14 +37,27 @@ class ScheduleConsumer(JsonWebsocketConsumer):
events_query_set = Event.objects.filter(camp=camp)
events = list([x.serialize() for x in events_query_set])
event_instances_query_set = EventInstance.objects.filter(event__camp=camp)
event_instances = list([x.serialize(user=message.user) for x in event_instances_query_set])
event_instances_query_set = EventInstance.objects.filter(
event__camp=camp
)
event_instances = list([
x.serialize(user=user)
for x in event_instances_query_set
])
event_locations_query_set = EventLocation.objects.filter(camp=camp)
event_locations = list([x.serialize() for x in event_locations_query_set])
event_locations_query_set = EventLocation.objects.filter(
camp=camp
)
event_locations = list([
x.serialize()
for x in event_locations_query_set
])
event_types_query_set = EventType.objects.filter()
event_types = list([x.serialize() for x in event_types_query_set])
event_types = list([
x.serialize()
for x in event_types_query_set
])
speakers_query_set = Speaker.objects.filter(camp=camp)
speakers = list([x.serialize() for x in speakers_query_set])
@ -67,18 +78,21 @@ class ScheduleConsumer(JsonWebsocketConsumer):
event_instance_id = content.get('event_instance_id')
event_instance = EventInstance.objects.get(id=event_instance_id)
Favorite.objects.create(
user=message.user,
user=user,
event_instance=event_instance
)
if action == 'unfavorite':
event_instance_id = content.get('event_instance_id')
event_instance = EventInstance.objects.get(id=event_instance_id)
favorite = Favorite.objects.get(event_instance=event_instance, user=message.user)
favorite = Favorite.objects.get(
event_instance=event_instance,
user=user
)
favorite.delete()
if data:
self.send(data)
self.send_json(data)
def disconnect(self, message, **kwargs):
pass

View File

@ -1,5 +1,5 @@
def current_order(request):
if request.user.is_authenticated():
if request.user.is_authenticated:
order = None
orders = request.user.orders.filter(open__isnull=False)