2018-04-03 18:52:39 +00:00
|
|
|
from channels.generic.websocket import JsonWebsocketConsumer
|
2017-04-15 17:35:18 +00:00
|
|
|
|
2017-04-20 23:34:22 +00:00
|
|
|
from camps.models import Camp
|
2018-03-04 14:48:57 +00:00
|
|
|
from .models import (
|
|
|
|
Event,
|
|
|
|
EventInstance,
|
|
|
|
Favorite,
|
|
|
|
EventLocation,
|
|
|
|
EventType,
|
2018-05-20 18:08:25 +00:00
|
|
|
EventTrack,
|
2019-06-16 12:32:24 +00:00
|
|
|
Speaker,
|
2018-03-04 14:48:57 +00:00
|
|
|
)
|
2017-04-15 17:35:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ScheduleConsumer(JsonWebsocketConsumer):
|
2019-06-16 12:32:24 +00:00
|
|
|
groups = ["schedule_users"]
|
2017-04-15 17:35:18 +00:00
|
|
|
|
2018-04-03 18:52:39 +00:00
|
|
|
def receive(self, text_data, **kwargs):
|
2019-06-16 12:32:24 +00:00
|
|
|
user = self.scope["user"]
|
2018-04-03 18:52:39 +00:00
|
|
|
content = self.decode_json(text_data)
|
2019-06-16 12:32:24 +00:00
|
|
|
action = content.get("action")
|
2017-04-15 17:35:18 +00:00
|
|
|
data = {}
|
|
|
|
|
2019-06-16 12:32:24 +00:00
|
|
|
if action == "init":
|
|
|
|
camp_slug = content.get("camp_slug")
|
2017-07-15 23:31:00 +00:00
|
|
|
try:
|
|
|
|
camp = Camp.objects.get(slug=camp_slug)
|
2019-06-16 12:32:24 +00:00
|
|
|
days = list(
|
|
|
|
map(
|
|
|
|
lambda day: {
|
|
|
|
"repr": day.lower.strftime("%A %Y-%m-%d"),
|
|
|
|
"iso": day.lower.strftime("%Y-%m-%d"),
|
|
|
|
"day_name": day.lower.strftime("%A"),
|
2017-07-15 23:31:00 +00:00
|
|
|
},
|
2019-06-16 12:32:24 +00:00
|
|
|
camp.get_days("camp"),
|
|
|
|
)
|
|
|
|
)
|
2017-07-18 09:26:56 +00:00
|
|
|
|
2018-05-20 17:32:51 +00:00
|
|
|
events_query_set = Event.objects.filter(track__camp=camp)
|
2017-07-18 09:26:56 +00:00
|
|
|
events = list([x.serialize() for x in events_query_set])
|
|
|
|
|
2018-04-03 18:52:39 +00:00
|
|
|
event_instances_query_set = EventInstance.objects.filter(
|
2018-05-20 17:32:51 +00:00
|
|
|
event__track__camp=camp
|
2018-04-03 18:52:39 +00:00
|
|
|
)
|
2019-06-16 12:32:24 +00:00
|
|
|
event_instances = list(
|
|
|
|
[x.serialize(user=user) for x in event_instances_query_set]
|
|
|
|
)
|
2018-04-03 18:52:39 +00:00
|
|
|
|
2019-06-16 12:32:24 +00:00
|
|
|
event_locations_query_set = EventLocation.objects.filter(camp=camp)
|
|
|
|
event_locations = list(
|
|
|
|
[x.serialize() for x in event_locations_query_set]
|
2018-04-03 18:52:39 +00:00
|
|
|
)
|
2017-07-15 23:31:00 +00:00
|
|
|
|
|
|
|
event_types_query_set = EventType.objects.filter()
|
2019-06-16 12:32:24 +00:00
|
|
|
event_types = list([x.serialize() for x in event_types_query_set])
|
2017-07-15 23:31:00 +00:00
|
|
|
|
2019-06-16 12:32:24 +00:00
|
|
|
event_tracks_query_set = EventTrack.objects.filter(camp=camp)
|
|
|
|
event_tracks = list([x.serialize() for x in event_tracks_query_set])
|
2018-05-20 18:08:25 +00:00
|
|
|
|
2017-08-02 20:20:38 +00:00
|
|
|
speakers_query_set = Speaker.objects.filter(camp=camp)
|
|
|
|
speakers = list([x.serialize() for x in speakers_query_set])
|
|
|
|
|
2017-07-15 23:31:00 +00:00
|
|
|
data = {
|
2017-07-17 09:25:57 +00:00
|
|
|
"action": "init",
|
2017-07-18 09:26:56 +00:00
|
|
|
"events": events,
|
|
|
|
"event_instances": event_instances,
|
2017-07-15 23:31:00 +00:00
|
|
|
"event_locations": event_locations,
|
|
|
|
"event_types": event_types,
|
2018-05-20 18:08:25 +00:00
|
|
|
"event_tracks": event_tracks,
|
2017-08-02 20:20:38 +00:00
|
|
|
"speakers": speakers,
|
2017-07-15 23:31:00 +00:00
|
|
|
"days": days,
|
|
|
|
}
|
|
|
|
except Camp.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
2019-06-16 12:32:24 +00:00
|
|
|
if action == "favorite":
|
|
|
|
event_instance_id = content.get("event_instance_id")
|
2017-04-16 00:10:24 +00:00
|
|
|
event_instance = EventInstance.objects.get(id=event_instance_id)
|
2019-06-16 12:32:24 +00:00
|
|
|
Favorite.objects.create(user=user, event_instance=event_instance)
|
2017-04-16 00:10:24 +00:00
|
|
|
|
2019-06-16 12:32:24 +00:00
|
|
|
if action == "unfavorite":
|
2018-04-15 17:44:14 +00:00
|
|
|
try:
|
2019-06-16 12:32:24 +00:00
|
|
|
event_instance_id = content.get("event_instance_id")
|
|
|
|
event_instance = EventInstance.objects.get(id=event_instance_id)
|
2018-04-15 17:44:14 +00:00
|
|
|
favorite = Favorite.objects.get(
|
2019-06-16 12:32:24 +00:00
|
|
|
event_instance=event_instance, user=user
|
2018-04-15 17:44:14 +00:00
|
|
|
)
|
|
|
|
favorite.delete()
|
2018-04-15 17:45:34 +00:00
|
|
|
except Favorite.DoesNotExist:
|
2018-04-15 17:44:14 +00:00
|
|
|
# We don't want to do anything.
|
|
|
|
return
|
2017-04-15 17:35:18 +00:00
|
|
|
|
2017-07-15 23:31:00 +00:00
|
|
|
if data:
|
2018-04-03 18:52:39 +00:00
|
|
|
self.send_json(data)
|
2017-04-15 17:35:18 +00:00
|
|
|
|
|
|
|
def disconnect(self, message, **kwargs):
|
|
|
|
pass
|