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 # start redirecting to the next camp instead of the previous camp after
# this much of the time between the camps has passed # 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 from program.consumers import ScheduleConsumer
channel_routing = [ application = ProtocolTypeRouter({
ScheduleConsumer.as_route(path=r"^/schedule/"), "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 camps.models import Camp
from .models import ( from .models import (
@ -12,13 +12,11 @@ from .models import (
class ScheduleConsumer(JsonWebsocketConsumer): class ScheduleConsumer(JsonWebsocketConsumer):
http_user = True groups = ['schedule_users']
def connection_groups(self, **kwargs): def receive(self, text_data, **kwargs):
return ['schedule_users'] user = self.scope['user']
content = self.decode_json(text_data)
def raw_receive(self, message, **kwargs):
content = self.decode_json(message['text'])
action = content.get('action') action = content.get('action')
data = {} data = {}
@ -39,14 +37,27 @@ class ScheduleConsumer(JsonWebsocketConsumer):
events_query_set = Event.objects.filter(camp=camp) events_query_set = Event.objects.filter(camp=camp)
events = list([x.serialize() for x in events_query_set]) events = list([x.serialize() for x in events_query_set])
event_instances_query_set = EventInstance.objects.filter(event__camp=camp) event_instances_query_set = EventInstance.objects.filter(
event_instances = list([x.serialize(user=message.user) for x in event_instances_query_set]) 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_query_set = EventLocation.objects.filter(
event_locations = list([x.serialize() for x in event_locations_query_set]) camp=camp
)
event_locations = list([
x.serialize()
for x in event_locations_query_set
])
event_types_query_set = EventType.objects.filter() 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_query_set = Speaker.objects.filter(camp=camp)
speakers = list([x.serialize() for x in speakers_query_set]) 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_id = content.get('event_instance_id')
event_instance = EventInstance.objects.get(id=event_instance_id) event_instance = EventInstance.objects.get(id=event_instance_id)
Favorite.objects.create( Favorite.objects.create(
user=message.user, user=user,
event_instance=event_instance event_instance=event_instance
) )
if action == 'unfavorite': if action == 'unfavorite':
event_instance_id = content.get('event_instance_id') event_instance_id = content.get('event_instance_id')
event_instance = EventInstance.objects.get(id=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() favorite.delete()
if data: if data:
self.send(data) self.send_json(data)
def disconnect(self, message, **kwargs): def disconnect(self, message, **kwargs):
pass pass

View file

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