2017-04-15 17:35:18 +00:00
|
|
|
from channels.generic.websockets import JsonWebsocketConsumer
|
|
|
|
|
2017-04-20 23:34:22 +00:00
|
|
|
from camps.models import Camp
|
2017-04-16 00:10:24 +00:00
|
|
|
from .models import EventInstance, Favorite
|
2017-04-15 17:35:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ScheduleConsumer(JsonWebsocketConsumer):
|
|
|
|
http_user = True
|
|
|
|
|
|
|
|
def connection_groups(self, **kwargs):
|
|
|
|
return ['schedule_users']
|
|
|
|
|
|
|
|
def connect(self, message, **kwargs):
|
2017-04-20 23:34:22 +00:00
|
|
|
camp_slug = message.http_session['campslug']
|
|
|
|
camp = Camp.objects.get(slug=camp_slug)
|
2017-04-26 22:23:03 +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')
|
|
|
|
},
|
|
|
|
camp.get_days('camp')
|
|
|
|
))
|
2017-04-20 23:34:22 +00:00
|
|
|
event_instances_query_set = EventInstance.objects.filter(event__camp=camp)
|
|
|
|
event_instances = list(map(lambda x: x.to_json(), event_instances_query_set))
|
|
|
|
self.send({
|
|
|
|
"accept": True,
|
|
|
|
"event_instances": event_instances,
|
|
|
|
"days": days,
|
|
|
|
"action": "init"
|
|
|
|
})
|
2017-04-15 17:35:18 +00:00
|
|
|
|
2017-04-16 00:10:24 +00:00
|
|
|
def raw_receive(self, message, **kwargs):
|
|
|
|
content = self.decode_json(message['text'])
|
2017-04-15 17:35:18 +00:00
|
|
|
action = content.get('action')
|
|
|
|
data = {}
|
|
|
|
|
|
|
|
if action == 'get_event_instance':
|
|
|
|
event_instance_id = content.get('event_instance_id')
|
|
|
|
event_instance = EventInstance.objects.get(id=event_instance_id)
|
|
|
|
data['action'] = 'event_instance'
|
2017-04-16 00:10:24 +00:00
|
|
|
data['event_instance'] = event_instance.to_json(user=message.user)
|
|
|
|
|
|
|
|
if action == 'favorite':
|
|
|
|
event_instance_id = content.get('event_instance_id')
|
|
|
|
event_instance = EventInstance.objects.get(id=event_instance_id)
|
|
|
|
Favorite.objects.create(
|
|
|
|
user=message.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.delete()
|
2017-04-15 17:35:18 +00:00
|
|
|
|
|
|
|
self.send(data)
|
|
|
|
|
|
|
|
def disconnect(self, message, **kwargs):
|
|
|
|
pass
|