diff --git a/src/bornhack/environment_settings.py.dist b/src/bornhack/environment_settings.py.dist index cff7578d..cf5fb10c 100644 --- a/src/bornhack/environment_settings.py.dist +++ b/src/bornhack/environment_settings.py.dist @@ -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 diff --git a/src/bornhack/routing.py b/src/bornhack/routing.py index 7776c0e7..d426b02c 100644 --- a/src/bornhack/routing.py +++ b/src/bornhack/routing.py @@ -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) + ]) + ) +}) diff --git a/src/program/consumers.py b/src/program/consumers.py index 06713545..d5ac9dc8 100644 --- a/src/program/consumers.py +++ b/src/program/consumers.py @@ -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 diff --git a/src/shop/context_processors.py b/src/shop/context_processors.py index 94796c89..ec572d80 100644 --- a/src/shop/context_processors.py +++ b/src/shop/context_processors.py @@ -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)